Home > OS >  Angular how continue method execution even if the http calls got error
Angular how continue method execution even if the http calls got error

Time:06-29

My http backend call may have exceptions, if it fails, my method doesn't continue with the rest of the instructions (//do something else). How to continue the execution even if there are exceptions?

  public updateStatus(status: string): void {
    this.userApiService.user.updateStatus(status, this.user.objectKey).subscribe((response) => {

      //do something else

    );
  }

CodePudding user response:

Add the catchError operator.

import { catchError } from 'rxjs/operators';

public updateStatus(status: string): void {
  this.userApiService.user.updateStatus(status,this.user.objectKey
    .pipe(
      catchError(e => /* handle error and return a handable error response */),
    ).subscribe((response) => {
      //do something else
    );
  }

CodePudding user response:

import { finalize } from 'rxjs/operators';
public updateStatus(status: string): void 
{
    this.userApiService.user.updateStatus(status,this.user.objectKey)
    .pipe(finalize(() => 
    {
       //do something even after exception
    }), 
    .subscribe((response) => 
    {

    }, error => console.error(error));
}
  • Related