Sergey Rudenko
Sergey Rudenko

Reputation: 9235

Angular http guide error handling - mistake?

I read this guide here: https://angular.io/guide/http#getting-error-details

And in the snippet the guide provides the code tries to differentiate: a) client side error (like no connection etc) and b) backend responses (see code in the guide or in the pic below).

I did implement this code for my case and surprisingly when I do offline mode in Chrome dev tools (set offline flag) - I catch those errors as "backend" errors while I thought it should be the client error.

See below what error I get and my code snippet (same as in the guide):

enter image description here

This is a snapshot of the console.log - where clearly this message "Backend returned..." should have been shown if there was in fact server side response.

What am i missing here? Is this code: err.error instanceof Error - even valid?

enter image description here

Upvotes: 1

Views: 631

Answers (1)

Max Koretskyi
Max Koretskyi

Reputation: 105547

The example seems to be a little outdated. HttpClient uses XHR backend under the hood and what the documentation refers to as client side error is XHR errors delivered through the error callback:

var oReq = new XMLHttpRequest();
oReq.error = handler()

According to the spec, XHR errors are instances of ProgressEvent interface, so the this check should be added:

if (e.error instanceof Error || e.error instanceof ProgressEvent) { }

Upvotes: 2

Related Questions