dejijaye
dejijaye

Reputation: 77

report $http error using airbrake in angular

I already configured airbrake to report error but it seems it doesn't report http errors. Hence I'm trying to connfigure it to do so. Here's my factory:

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

Then in the config:

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

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

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

It seem to work only that I get [object Object] as the error on airbrake, no additional error detail or Backtrace. Is there something else I'm missing?

Upvotes: 1

Views: 267

Answers (2)

dejijaye
dejijaye

Reputation: 77

I finally found out that the server response wasn't in the format airbrake wanted it so I passed an error message built from the response into the airbrake notifier.

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

It doesn't feel like the cleanest of solution but it sure works.

Upvotes: 1

mogramer
mogramer

Reputation: 602

Morgan from Airbrake here! Since you are using angular have you tried adding $exceptionHandler? The airbrake-js readme has an example in the angular section:

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

If you need any further help getting angular working feel free to email us at [email protected] or send us an in-app message!

From, Morgan

Upvotes: 0

Related Questions