Angularjs 如何防止在Angular中记录错误?
我正在使用httpResponseInterceptor检查过期的会话令牌。我能够检测到它并在结束会话时向我的用户发出一个良好的通知,但我仍在寻找一种方法,使控制台不输出请求错误: 获取401(未经授权) 以下是我的代码的简化版本:Angularjs 如何防止在Angular中记录错误?,angularjs,angular-http-interceptors,Angularjs,Angular Http Interceptors,我正在使用httpResponseInterceptor检查过期的会话令牌。我能够检测到它并在结束会话时向我的用户发出一个良好的通知,但我仍在寻找一种方法,使控制台不输出请求错误: 获取401(未经授权) 以下是我的代码的简化版本: responseError: function(rejection) { if(rejection.status === 401 && rejection.data.error === "Token expired"){
responseError: function(rejection) {
if(rejection.status === 401 && rejection.data.error === "Token expired"){
//notify user of being logged out if they were logged in
[...]
//destroy session (will also unset cookies)
LoginService.destroy();
}
return $q.reject(rejection);
}
明确地说:我确实想拒绝这个承诺,因为一些积极的过程应该被取消。我只是不想看到错误信息
谢谢你的帮助 您可以使用angular的decorator“修饰”异常处理程序:
angular.module('yourapp').config(function ($provide) {
// catch exceptions in angular
$provide.decorator('$exceptionHandler', ['$delegate', function ($delegate) {
return function (exception, cause) {
var message = exception.message;
if (message != 'YOUR ERROR TO AVOID') {
$delegate(exception, cause); // If this does not execute - no log would be printed to console
}
}
}])
});