【问题标题】:How to prevent error logging in Angular?如何防止 Angular 中的错误日志记录?
【发布时间】:2015-02-17 19:03:50
【问题描述】:

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

GET http://api.xx.xx/accounts/82401(未经授权)

这是我的代码的简化版本:

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

要明确:我确实想拒绝承诺,因为应该取消一些活动进程。我只是不想要错误消息。

感谢您的帮助!

【问题讨论】:

    标签: angularjs angular-http-interceptors


    【解决方案1】:

    您可以使用 Angular 的装饰器“装饰”异常处理程序:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-08
      • 1970-01-01
      • 1970-01-01
      • 2016-02-26
      • 1970-01-01
      • 1970-01-01
      • 2016-12-29
      相关资源
      最近更新 更多