【问题标题】:report $http error using airbrake in angular使用 angular 中的 airbrake 报告 $http 错误
【发布时间】:2015-10-27 09:36:33
【问题描述】:

我已经将 airbrake 配置为报告错误,但它似乎没有报告 http 错误。因此,我正在尝试将其配置为这样做。这是我的工厂:

import AirbrakeClient from 'airbrake-js';

export let errorHttpInterceptor = ($q, CONSTANT) => {
  'use strict';

  let airbrake = new AirbrakeClient({
    projectId: CONSTANTS.AIRBRAKE_PROJECT_ID,,
    projectKey: CONSTANTS.AIRBRAKE_PROJECT_KEY
  });

  airbrake.addFilter((notice) => {
    console.log(notice);
    return notice;
  });

  return {
    requestError: (rejection) => {
      console.log(rejection);
      // do something on error
      airbrake.notify(rejection);
      return $q.reject(rejection);
    },

   responseError: (rejection) => {
     console.log(rejection);
     airbrake.notify(rejection);
     return $q.reject(rejection);
   }
  };
};

然后在配置中:

let httpAuthConfig = /*@ngInject*/ $httpProvider => {
  'use strict';

  let errorHttp = $injector => { return $injector.get('errorHttpInterceptor'); };

  $httpProvider.interceptors.push(['$injector', errorHttp]);
};

似乎只有我得到[object Object] 作为airbrake 上的错误,没有额外的错误细节或回溯。我还有什么遗漏的吗?

【问题讨论】:

    标签: angularjs http-error airbrake


    【解决方案1】:

    我终于发现服务器响应不是 airbrake 想要的格式,所以我将根据响应构建的错误消息传递给 airbrake 通知器。

    responseError: (response) => {
      console.log(response);
      airbrake.notify({
        error: {
          message: response.status + ': ' + response.data.error.general,
          name: 'HttpError: ',
          stack: ''
        }
      });
      return $q.reject(response);
    }
    

    感觉不是最干净的解决方案,但它确实有效。

    【讨论】:

      【解决方案2】:

      Airbrake 的摩根在这里! 由于您使用的是角度,您是否尝试添加$exceptionHandler? airbrake-js 自述文件在 angular 部分有一个示例:

      https://github.com/airbrake/airbrake-js#angular

      mod.factory('$exceptionHandler', function ($log, config) {
        airbrake = new airbrakeJs.Client({
          projectId: config.airbrake.projectId,
          projectKey: config.airbrake.key
        });
        airbrake.addFilter(function (notice) {
          notice.context.environment = config.envName;
          return notice;
        });
      
        return function (exception, cause) {
          $log.error(exception);
          airbrake.notify({error: exception, params: {angular_cause: cause}});
        };
      });
      

      如果您需要任何进一步的帮助来获得角度工作,请随时通过 support@airbrake.io 给我们发送电子邮件或向我们发送应用内消息!

      从, 摩根

      【讨论】:

      • 我已经为$exceptionHandler工厂做过了,但是似乎http错误没有报告给airbrake。
      • 知道了!感谢您的澄清!
      猜你喜欢
      • 1970-01-01
      • 2015-10-10
      • 2021-06-07
      • 2020-08-06
      • 1970-01-01
      • 1970-01-01
      • 2017-06-27
      • 1970-01-01
      • 2018-02-27
      相关资源
      最近更新 更多