I'm trying to add error handling for HTTP request using the newer Angular's HTTP Client: https://angular.io/guide/http
In the docs, it says that error handling is done as follows:
return this.http.get('https://api.example.com/foo')
.pipe(
catchError(this.handleError)
);
private handleError(error: HttpErrorResponse) {
if (error.error instanceof ErrorEvent) {
// A client-side or network error occurred. Handle it accordingly.
console.log('Client side error:', error.error)
// TODO: how do I catch specific client side errors?
} else {
console.log('Back-end error:', error.error)
}
// return an observable with a user-facing error message
return throwError('Something bad happened; please try again later.');
};
Handling back-end errors is simple enough. However, I want to be able to catch specific client-side errors such as:
- Connection time out (
net::ERR_CONNECTION_TIMED_OUT
) - No internet connection
How can I catch these specific client side errors?
Update
In fact I've never been able to get error.error
to return an ErrorEvent
. Usually when the internet connection is gone, or connecting to an invalid domain, it returns a ProgressEvent
.