简体   繁体   English

Angular 5:HttpInterceptor可以替换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. 在阅读Angular 5中的错误处理时,我遇到了两种方法:1. HttpInterceptor和2. ErrorHandler。

  1. Is HttpInterceptor a better solution than ErrorHandler? HttpInterceptor是比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? 让HttpInterceptor处理HTTP调用错误并将Header添加到请求等中,然后通过使用Observable.throw(error)中继未处理的错误,以便ErrorHandler可以将其作为全局级别处理?

One traditional way of handling errors in Angular is to provide an ErrorHandler class. 在Angular中处理错误的一种传统方法是提供一个ErrorHandler类。 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. 通过在HttpClient或HttpInterceptor中实现错误处理,您可以直接处理应用程序中的所有HTTP请求,并具有转换请求,重试请求等功能。 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. 因此,ErrorHandler对于更通用的错误处理很有用,但是HttpInterceptor提供了一种更健壮的方式来处理与服务器和网络有关的错误。

Please go thru this link ,required details for your question is provided in detail in this article. 请通过此链接 ,本文中详细提供了您的问题所需的详细信息。

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? HttpInterceptor处理HTTP调用错误并将Header添加到请求等中-如果您对客户端的响应有错误,为什么还要添加标头以指示错误?

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. 我已经通过ErrorHandler亲自完成了服务,该服务返回可观察到的内容,然后使用其自己的errorService提供者等处理组件中的错误。所有个人喜好。

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 组件(呼叫服务)服务(返回组件的响应)组件通过Error来确定是Error还是Success,如果Error则通过Subscribe来确定,通过ErrorService组件中的errorService提供者来处理

My 2 cents. 我的2美分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM