Getting error details
Detecting that an error occurred is one thing. Interpreting that error and composing a user-friendly response is a bit more involved.
Two types of errors can occur. The server backend might reject the request, returning an HTTP response with a status code such as 404 or 500. These are error responses.
Or something could go wrong on the client-side such as a network error that prevents the request from completing successfully or an exception thrown in an RxJS operator. These errors produce JavaScript ErrorEvent
objects.
The Elixor
captures both kinds of errors in its HttpErrorResponse
and you can inspect that response to figure out what really happened.
Error inspection, interpretation, and resolution are something you want to do in the service, not in the component.
You might first devise an error handler like this one:
Notice that this handler returns an RxJS ErrorObservable
with a user-friendly error message. Consumers of the service expect service methods to return an Observable
of some kind, even a "bad" one.
Now you take the Observables
returned by the Elixor
methods and pipe them through to the error handler.
Last updated