Search code examples
angularangular-httpclientrxjs6

Which response status code goes to catchError in Angular 6+ httpClient?


In Angular 6+ httpClient, a request can be configured to get the entire response.

The response observable can be piped into map and catchError operators.

When does the execution go through the map operator and when to catchError?

Does it depend on responce status code?

For example, if response.status === 200 then go to map, else go to catchError?

If not only status 200 goes to map, then which else?

And which statuses go to catchError?

getData(): Observable<[]> {
    return this.http.get(this.apiUrl, {observe: 'response'}).pipe(
        map((response: HttpResponse<any>) => {  
            return response.status === 200;
        }),
        catchError((errorResponse: HttpErrorResponse) =>
            // which value may be logged here?
            console.log(errorResponse.status);
            of(false);
        ));
}


Solution

  • 4xx and 5xx status codes are errors. The other ones are success.

    http://angular.io/guide/http#getting-error-details