Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 如何防止在Angular中记录错误?_Angularjs_Angular Http Interceptors - Fatal编程技术网

Angularjs 如何防止在Angular中记录错误?

Angularjs 如何防止在Angular中记录错误?,angularjs,angular-http-interceptors,Angularjs,Angular Http Interceptors,我正在使用httpResponseInterceptor检查过期的会话令牌。我能够检测到它并在结束会话时向我的用户发出一个良好的通知,但我仍在寻找一种方法,使控制台不输出请求错误: 获取401(未经授权) 以下是我的代码的简化版本: responseError: function(rejection) { if(rejection.status === 401 && rejection.data.error === "Token expired"){

我正在使用httpResponseInterceptor检查过期的会话令牌。我能够检测到它并在结束会话时向我的用户发出一个良好的通知,但我仍在寻找一种方法,使控制台不输出请求错误:

获取401(未经授权)

以下是我的代码的简化版本:

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
            }
        }

    }])
});