Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

課題

 目下、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経由で入ってくる想定。
 他によいやり方があれば教えてください。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした