November 12, 2019

Error tracking in Angular using Rollbar

About Us

Krunal Shah

We all want to run our request as smoothly as possible, but this is not always the case. Once the application is working, we need to know if and when failures or exceptions are being thrown. That's where the tools for monitoring and tracking errors come in. One of those tools is the Rollbar.

Table of Contents

Rollbar provides developers with real-time reporting of exceptions and ongoing deployment monitoring. It provides developers with real-time error monitoring and debugging tools. In this tutorial, we'll see how we can actually implement every project error tracking tool that is rollbar in our angular project.
Steps To Integrate Rollbar
  1. Blog-Detail ArrowCreate an account in Rollbar with https://rollbar.com/signup/ & choose your plan according to your project needs. We will choose a free plan for this demo.
Now login into your account and create a project as per the following images.
  • Blog-Detail ArrowCreate a Project from a landing page
  • Blog-Detail ArrowEnter Project Details
  • Blog-Detail ArrowChoose your project language or framework to quick setup
  • Blog-Detail ArrowClick “Continue” to get quick setup code.
Now we will integrate this code in our actual Angular project.
  • Blog-Detail ArrowFirst of all install npm package with npm install --save rollbar
  • Blog-Detail ArrowCopy the above screenshot code in your Angular project & it clearly says that this rollbar setup code needs to be integrated into your root module file which is app.module.ts so that we will cover all the errors throughout the project, not only single module or page.
  • Blog-Detail ArrowIt might be a chance that you will face some errors in your project while integrating this code like “Cannot find name 'Inject'”, “Cannot access 'RollbarService' before initialization at Module../src/app/app.module.ts”
To resolve these errors modify app.module.ts file like
import { BrowserModule } from "@angular/platform-browser"; import { Injectable, Inject, InjectionToken, NgModule, ErrorHandler } from "@angular/core"; import * as Rollbar from "rollbar"; import { AppRoutingModule } from "./app-routing.module"; import { AppComponent } from "./app.component"; const rollbarConfig = { accessToken: "ACCESS_TOKEN", /* You will find this from your quick setup code */ captureUncaught: true, captureUnhandledRejections: true }; export const RollbarService = new InjectionToken<Rollbar>("rollbar"); @Injectable() export class RollbarErrorHandler implements ErrorHandler { constructor(@Inject(RollbarService) private rollbar: Rollbar) {} handleError(err: any): void { this.rollbar.error(err.originalError || err); } } export function rollbarFactory() { return new Rollbar(rollbarConfig); } @NgModule({ declarations: [AppComponent], imports: [BrowserModule, AppRoutingModule], providers: [ { provide: ErrorHandler, useClass: RollbarErrorHandler }, { provide: RollbarService, useFactory: rollbarFactory } ], bootstrap: [AppComponent] }) export class AppModule {}
  • Blog-Detail ArrowThat’s it we have successfully integrated rollbar in our project. Now onwards if any occurs then we could see it in our rollbar account.
  • Blog-Detail ArrowTo test this code, Just write “window.onerror("TestError: Hello world", window.location.href)” in constructor of app.component.ts file. You will see a full error log in your rollbar account.
You can modify your rollbar account settings to use various modes like send this error to your team member’s emails, slack channel, create a JIRA ticket, bind source map to see exact line number where the error has occurred.
Deployments come and go, but there will be errors and exceptions. We can fix them and troubleshoot errors. Error tracking tools are a crucial part of the software life cycle, and everybody can bring an entirely new way to use our app, which we haven't thought about before.
There are many solutions and while they all focus on helping us to locate errors they each have their own benefits and drawbacks which may be crucial for our application. If feasible, we would recommend taking advantage of the free trials in your staging environment and checking these tools Change is not easy, but it may help you build a better application.
· · · ·
Third Rock Techkno is a leading IT services company. We are a top-ranked web, voice and mobile app development company with over 10 years of experience. Client success forms the core of our value system.
We have expertise in the latest technologies including angular, react native, iOs, Android and more. Third Rock Techkno has developed smart, scalable and innovative solutions for clients across a host of industries.
Our team of dedicated developers combine their knowledge and skills to develop and deliver web and mobile apps that boost business and increase output for our clients.

Found this blog useful? Don't forget to share it with your network

Already working together?

Log in to your account and connect with your teammates

Featured Insights

Team up with us to enhance and

achieve your business objectives

LET'S WORK

TLogoGETHER