0
1

More than 5 years have passed since last update.

SPAからconsole.error() でBugsnagにエラー情報を送信する。

Last updated at Posted at 2018-08-24

課題

 目下、ReactによるSPA(Single Page Application)の開発に関わっており、その途上で以下の課題

が発生したので取り急ぎ以下のように解決した。

解決策

 console.error()の先頭パラメーターがErrorオブジェクトの場合、そのError情報がBugsnagに送信され、それ以外はデフォルトと同じ動作をするようにした。概略は以下。

yarn add bugsnag-js
import bugsnag from 'bugsnag-js';

const origConsoleError = console.error;

console.error = (...args) => {
  if (args.length > 0 && /Error$/.test(args[0].constructor.name)) {
    bugsnag(BUGSNAG_API_KEY).notify(args[0]);
  }
  origConsoleError(...args); // 既存の console.error を呼ぶ
};
console.error(new Error('This is an Error.'));  // sending Error info to Bugsnag

 なお上記でBUGSNAG_API_KEY には、環境変数から取得した API KEYがwebpack.DefinePlugin経由で入ってくる想定。
 他によいやり方があれば教えてください。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1