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

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

问题描述

有我们想要做一个随时随地的JavaScript异常被抛出一些额外的东西。

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

从上Docs $ exceptionHandler的

在角前pressions任何未捕获的异常被委托给此服务。默认实现只委托给$ log.error其中记录它在浏览器控制台。

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?

推荐答案

我发现这另一种选择是装饰 $ exceptionHandler的通过<一个HREF =htt​​ps://docs.angularjs.org/api/auto/service/$provide#decorator> $ provide.decorator 的功能。如果你想使用它作为您的自定义实现的一部分。这为您提供了原始的实现提供参考。所以,你可以做这样的事情:

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);
        };
    }]);
});

这会对原有的异常处理程序做了什么,以及自定义功能。看到这个更新拨弄

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

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