0

在我的角度应用程序中,我将服务定义为:

.factory('$exceptionHandler', function ($log) {
  airbrake = new airbrakeJs.Client({
    projectId: 1234,
    projectKey: 'xxxxxxxxxxxxx'
  });

  airbrake.addFilter(function (notice) {
    notice.context.environment = 'production';
    return notice;
  });

  return function (exception, cause) {
    $log.error(exception);
    airbrake.notify({error: exception, params: {angular_cause: cause}});
  };
});

这种方式工作正常,错误正在空气制动器中跟踪。但是所有由airbrake 跟踪的错误都以相同的错误类型出现。例如:

错误 1

Occurred at: Jan 04, 2016 13:27:13 -0200
First seen at: Jan 04, 2016 13:27:13 -0200
Occurrences: 2
URL http://localhost:8100/#/tasks
Error type: Error
Error message: Error: [$injector:unpr] Unknown provider: amMomentProvider <- amMoment http://errors.angularjs.org/1.4.3/$injector/unpr?p0=amMomentProvider%20%3C-%20amMoment
Remote address: xxx.xxx.xx.xxx
User agent: Chrome xx.x.xxxx.xxx

错误 2

Occurred at: Jan 04, 2016 13:28:15 -0200
First seen at: Jan 04, 2016 13:27:13 -0200
Occurrences: 2
URL http://localhost:8100/#/tasks
Error type: Error
Error message: Error: [$injector:undef] Provider '$exceptionHandler' must return a value from $get factory method. http://errors.angularjs.org/1.4.3/$injector/undef?p0=%24exceptionHandler
Remote address: xxx.xxx.xx.xxx
User agent: Chrome xx.x.xxxx.xxx

正如我们在上面看到的,这两个错误属于同一类型,因此被组合在一起,就好像它们是同一个错误一样,但不是。如何Error type为每个不同的错误定义不同的?

4

1 回答 1

0

这个解决方案对我有用。

.factory('$exceptionHandler', function ($log) {
  var airbrake = new airbrakeJs.Client({
    projectId: 1234,
    projectKey: 'xxxxxxxxxxxxx'
  });

  airbrake.addFilter(function (notice) {
    notice.context.environment = 'production';

    notice.errors.forEach(function (error) {
      var index  = error.message.indexOf('\n');
      error.type = error.message.substring(0, index);
    });

    return notice;
  });

  return function (exception, cause) {
    $log.error(exception);
    airbrake.notify({error: exception, params: {angular_cause: cause}});
  };
});
于 2016-04-22T12:59:57.923 回答