如何覆盖$ exceptionHandler实现 [英] How to override $exceptionHandler implementation

查看:112
本文介绍了如何覆盖$ exceptionHandler实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



$ exceptionHandler


角度表达式中的任何未捕获的异常都被委派给此服务。默认实现只是将$ log.error委托给浏览器控制台。


它表示默认实现让我认为有一种方法可以为服务提供自己的实现,并在抛出异常时执行我们想要的东西。我的问题是,你怎么做的?我们如何保持所有例外情况,但是提供我们想要发生的功能?

解决方案

另一个选项发现这是通过 $ exceptionHandler 装饰器> $ provide.decorator 功能。如果您希望将其用作自定义实现的一部分,这将为您提供对原始实现的引用。所以,你可以这样做:

  mod.config(function($ provide){
$ provide。装饰器($ exceptionHandler,['$ delegate',function($ delegate){
return function(exception,cause){
$ delegate(exception,cause);
alert .message);
};
}]);
});

它将执行原始异常处理程序,以及自定义功能。



请参阅更新小提琴


There are some extra things we want to do anytime a javascript exception is thrown.

From the docs on $exceptionHandler:

Any uncaught exception in angular expressions is delegated to this service. The default implementation simply delegates to $log.error which logs it into the browser console.

The fact that it says "default implementation" makes me think there's a way we can provide our own implementation for the service, and do the stuff we want when an exception is thrown. My question is, how do you do this? How can we keep all exceptions going to this service, but then provide functionality we want to happen?

解决方案

Another option I've found for this is to "decorate" the $exceptionHandler through the $provide.decorator function. This provides you with a reference to the original implementation if you want to use it as part of your custom implementation. So, you can do something like this:

mod.config(function($provide) {
    $provide.decorator("$exceptionHandler", ['$delegate', function($delegate) {
        return function(exception, cause) {
            $delegate(exception, cause);
            alert(exception.message);
        };
    }]);
});

It will do what the original exception handler does, plus custom functionality.

See this updated fiddle.

这篇关于如何覆盖$ exceptionHandler实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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