rollbar / rollbar.js

Error tracking and logging from Javascript to Rollbar

Home Page:https://docs.rollbar.com/docs/javascript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to handle don’t report in Angular Rollbar Service

hebaahmedsaleh opened this issue · comments

I am using Angular v12 and implemented Rollbar according to docs I have a class for Rollbar to report issues


import { environment } from '@env';
import * as Rollbar from 'rollbar';
import { Injectable, Inject, InjectionToken, ErrorHandler } from '@angular/core';

const rollbarConfig = { // config }

export const RollbarService = new InjectionToken<Rollbar>('rollbar');

@Injectable()
export class RollbarErrorHandler implements ErrorHandler {
  constructor(@Inject(RollbarService) private rollbar: Rollbar) {}

  handleError(error: any): void {
    if (environment.production) {
      if (error.originalError || error.name != 'HttpErrorResponse') {
        console.error(error.originalError || error);
        this.rollbar.error(error.originalError || error);
      }
    }
  }
}

export function rollbarFactory() {
  return new Rollbar(rollbarConfig);
}

The problem is that the error received in handleerror is always the message so how I can receive it as an error object which I got from failure of api called for example ?

hey @hebaahmedsaleh. maybe too late for you but maybe useful for the next with the same problem. one thing you could try is to use the callback (described here: https://docs.rollbar.com/docs/javascript#usage):

Rollbar.error(e, function(err, data) {
  if (err) {
    console.log("Error while reporting error to Rollbar: ", e);
  } else {
    console.log("Error successfully reported to Rollbar. UUID:", data.result.uuid);
  }
});

It might be that your token has not the permission needed to log? I also had some struggles to properly log it with angular, since not everything returned is an error (e.g. #975)