角度2中的错误处理程序和拦截器有什么区别? [英] what is the difference between error handler and interceptor in angular 2?

查看:104
本文介绍了角度2中的错误处理程序和拦截器有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于实时Web应用程序,ng2中前端错误处理的最佳解决方案是什么?

And also what would be the best solution for front end error handling in ng2 for real-time web app?

使用"Http拦截器"进行前端错误处理是否可以?请解释这些事情.

Is it fine to use 'Http interceptor' for front end error handling? Please explain about these things.

推荐答案

角度2中的错误处理程序和拦截器有什么区别?:

基于Angular文档,这是它们的定义方式:

Based on the Angular documentation, this is how they are defined:

ErrorHandler :ErrorHandler的默认实现将错误消息打印到控制台.

ErrorHandler: The default implementation of ErrorHandler prints error messages to the console.


HttpInterceptor :通常,拦截器会转换传出的请求,然后返回next.handle(transformedReq).通过在next.handle()返回的流上应用其他Rx运算符,拦截器也可以选择转换响应事件流.

HttpInterceptor: Typically an interceptor will transform the outgoing request before returning next.handle(transformedReq). An interceptor may choose to transform the response event stream as well, by applying additional Rx operators on the stream returned by next.handle().

更罕见的是,拦截器可能会选择完全处理请求本身,并编写一个新的事件流,而不是调用next.handle().这是可以接受的行为,但是请记住,其他拦截器将被完全跳过.

More rarely, an interceptor may choose to completely handle the request itself, and compose a new event stream instead of invoking next.handle(). This is acceptable behavior, but keep in mind further interceptors will be skipped entirely.

拦截器为单个请求返回事件流上的多个响应也是很少见但有效的方法.

It is also rare but valid for an interceptor to return multiple responses on the event stream for a single request.

这使我相信ErrorHandler的唯一目的是确定错误将如何持续存在,以便开发人员认识到应用程序中已发生错误.默认行为是将错误保留在控制台中,开发人员可以在其中轻松确定应用程序是否引发了错误.一个简单的用例是,如果您想将错误记录在某种类型的持久性存储中,以便开发人员可以梳理日志中的任何客户端错误.

This leads me to believe that the sole purpose of ErrorHandler is to determine how the error will be persisted in order for the developer to realize that an error has occurred in the application. The default behavior is to persist the error in the console where a developer can easily determine if errors are being thrown by the app. A simple use case would be if you wanted to log the errors in some type of persistent storage so that a developer can comb through the logs for any client side errors.

现在,如果要在将请求发送到服务器之前对其进行操作,请使用HttpInterceptor.一个简单的用例是,如果您想向所有请求添加标头,操纵正文,将请求URL小写,等等.

Now, an HttpInterceptor is used when you want to manipulate a request before it's sent to the server. A simple use case would be if you wanted to add a header to all request, manipulate the body, lower-case the request URL, etc.

实时网络应用ng2中前端错误处理的最佳解决方案是什么?:

如果您只想在角度应用程序中记录任何错误,请编写自己的自定义ErrorHandler到Intercept错误处理.在自定义错误处理程序中,使用服务将错误记录到持久性存储中.

If you just want to log any error in your angular app, write your own custom ErrorHandler to Intercept error handling. Inside your custom error handler use a service to log the error to persistent storage.

使用"Http拦截器"进行前端错误处理是否可以? 这完全取决于您的用例.假设您想通过 SnackBar 通知客户,则可以编写一个拦截器来捕获请求,检查响应是否为500,然后显示红色SnackBar并重新抛出错误,以便进行调用的服务可以执行其他一些错误处理.

Is it fine to use 'Http interceptor' for front end error handling? It all depends on your use case. Say you want to notify the client via a SnackBar when a 500 occurs, you can write an interceptor that catches the request, inspects the response for a 500, then shows a red SnackBar and rethrows the error so that the service that made the call can do some additional error handling.

希望这会有所帮助:)

这篇关于角度2中的错误处理程序和拦截器有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆