Javascript AngularJS$exceptionHandler控制器中的自定义回调
我正在尝试捕捉AngularJS应用程序中的异常。目前,我覆盖$exceptionHandler以捕获异常并将其发送到服务器进行日志记录:Javascript AngularJS$exceptionHandler控制器中的自定义回调,javascript,angularjs,exception-handling,Javascript,Angularjs,Exception Handling,我正在尝试捕捉AngularJS应用程序中的异常。目前,我覆盖$exceptionHandler以捕获异常并将其发送到服务器进行日志记录: angular.module('jsDebug', []).factory('$exceptionHandler', function () { return function errorCatcherHandler(exception, cause) { StackTrace.fromError(exception).then(function
angular.module('jsDebug', []).factory('$exceptionHandler', function () {
return function errorCatcherHandler(exception, cause) {
StackTrace.fromError(exception).then(function(stackframes) {
var stringifiedStack = stackframes.map(function(sf) {
return sf.toString();
}).join('\n');
// sendToServer does an Ajax call for sending the exception details to the server.
sendToServer(exception.toString() + "\nlocation=" + window.location.href + '\ncause=' + cause + '\n' + stringifiedStack, 'warning');
console.error(exception);
}).catch(function(err) {
console.error(err);
});
};
});
这对于日志记录非常有用。但是,我还希望在控制器上对此进行自定义回调,以便根据抛出错误的控制器来处理错误。我希望在控制器上有类似以下的代码,只要异常处理程序捕捉到错误,就会执行这些代码:
exceptionHandlerCallback: function () {
// insert code for handling errors here
}
有什么方法可以做到这一点吗?您可以始终使用
$rootScope.$emit('error',error)代码>