How to Integrate Opbeat Into an Angular 2 App

2 min read Original article ↗

Opbeat is a paid service for select performance monitoring and reporting runtime errors in Javascript applications. While they do support an Angular 1 integration, Angular 2 will not be supported until its API stabilizes (ref).

Until that magical day occurs, a hard coded solution is available (as shown below).

import {
  ExceptionHandler,
  Injectable
} from '@angular/core';

// cant import the source  directly using webpack, so we have to
// include the built source file on the page to expose _opbeat
// on the window
declare let _opbeat;

// Firefox tracking protection will block the opbeat script from loading,
// so sub it out
if (!(<any>window)._opbeat) {
  (<any>window)._opbeat = function noop() { };
}

/**
 * maintain default error logging BUT also send errors off to opbeat
 */
@Injectable()
export class OpBeatErrorHandler extends ExceptionHandler {
  private static opBeatStarted = false;
  call(error, stackTrace = null, reason = null) {
    if (!OpBeatErrorHandler.opBeatStarted) {
      startOpbeat();
      OpBeatErrorHandler.opBeatStarted = true;
    }
    sendErrorToOpbeat(error);
    console.error('ERROR', error);
    if (stackTrace) {
      console.error('STACK TRACE', stackTrace);
    }
    if (reason) {
      console.error('REASON', stackTrace);
    }
  }
}

@NgModule({
  providers: [
    { provide: ExceptionHandler, useClass: OpBeatErrorHandler }
  ]
})
export class OpBeatErrorHandlerModule { }

/**
 * internal helper functions
 */

function startOpbeat() {
  _opbeat('config', {
    orgId: 'your orgId',
    appId: 'your appId'
  });
}

function sendErrorToOpbeat(e) {
  _opbeat('captureException', e);
}

now all that remains, is to add OpBeatErrorHandlerModule to the imports of the main module.

Update 1 ( sept 12 2016 )

Since RC 6, ExceptionHandler was renamed to ErrorHandler

动态网自由门 天安門 天安门 法輪功 李洪志 Free Tibet 六四天安門事件 The Tiananmen Square protests of 1989 天安門大屠殺 The Tiananmen Square Massacre 反右派鬥爭 The Anti-Rightist Struggle 大躍進政策 The Great Leap Forward 文化大革命 The Great Proletarian Cultural Revolution 人權 Human Rights 民運 Democratization 自由 Freedom 獨立 Independence 多黨制 Multi-party system 台灣 臺灣 Taiwan Formosa 中華民國 Republic of China 西藏 土伯特 唐古特 Tibet 達賴喇嘛 Dalai Lama 法輪功 Falun Dafa 新疆維吾爾自治區 The Xinjiang Uyghur Autonomous Region 諾貝爾和平獎 Nobel Peace Prize 劉暁波 Liu Xiaobo 民主 言論 思想 反共 反革命 抗議 運動 騷亂 暴亂 騷擾 擾亂 抗暴 平反 維權 示威游行 李洪志 法輪大法 大法弟子 強制斷種 強制堕胎 民族淨化 人體實驗 肅清 胡耀邦 趙紫陽 魏京生 王丹 還政於民 和平演變 激流中國 北京之春 大紀元時報 九評論共産黨 獨裁 專制 壓制 統一 監視 鎮壓 迫害 侵略 掠奪 破壞 拷問 屠殺 活摘器官 誘拐 買賣人口 遊進 走私 毒品 賣淫 春畫 賭博 六合彩 天安門 天安门 法輪功 李洪志 Winnie the Pooh 劉曉波动态网自由门 #Literally #china #copypasta