angularerror-handlingangular-http-interceptorsangular-errorhandler

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?


Solution

  • 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.