rohit12sh
rohit12sh

Reputation: 847

Angular 5: Can HttpInterceptor Replace ErrorHandler

While reading through the Error Handling in Angular 5, I came across 2 ways: 1. HttpInterceptor and 2. ErrorHandler.

  1. Is HttpInterceptor a better solution than ErrorHandler?
  2. Can you have both implementation in your App?

Does this scenario make sense? Having HttpInterceptor to handle HTTP call errors and add Header to the request etc, and then relay the unhandled error by using Observable.throw(error) so that ErrorHandler can handle it as the global level?

Upvotes: 2

Views: 1197

Answers (2)

Sameer Ahmed S
Sameer Ahmed S

Reputation: 1683

One traditional way of handling errors in Angular is to provide an ErrorHandler class. This class can be extended to create your own global error handler. This is also a useful way to handle all errors that occur, but is mostly useful for tracking error logs.

By implementing error handling in HttpClient or HttpInterceptor, you can work directly with all HTTP requests in your application, providing the ability to transform the request, retry it, and more. Therefore, ErrorHandler is useful for more generic error handling, but HttpInterceptor provides a much more robust way to handle errors related to the server and network.

Please go thru this link ,required details for your question is provided in detail in this article.

Upvotes: 2

Taranjit Kang
Taranjit Kang

Reputation: 2580

HttpInterceptor to handle HTTP call errors and add Header to the request etc -- if there is an error from your response to the client, why add a header indicating it was an error?

I've personally done via ErrorHandler, service returning the observable and then handling the error in the component with its own errorService providor etc.. All personal preference.

Component (calls service) Service (Returns response to component) Component determines if Error or Success via subscribe if Error, handle error via errorService providor in component

My 2 cents.

Upvotes: -1

Related Questions