Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
        ));
}

like image 386
Vadim Loboda Avatar asked Mar 06 '26 14:03

Vadim Loboda


1 Answers

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

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

like image 137
JB Nizet Avatar answered Mar 08 '26 20:03

JB Nizet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!