[WebService] Bugsnag API 使用筆記
使用版本 @
7.0.0
- JavaScript Integration Guide @ Bugsnag doc
- React integration guide @ Bugsnag doc
- Configuration options @ BugSnag doc
- Upgrading @ BugSnag Github
安裝(搭配 React)
$ npm install --save @bugsnag/js @bugsnag/plugin-react
載入:
// ES module-style import
import Bugsnag from '@bugsnag/js';
import BugsnagPluginReact from '@bugsnag/plugin-react';
使用:
Bugsnag.start({
apiKey: 'YOUR_API_KEY',
plugins: [new BugsnagPluginReact(React)],
otherOptions: value,
});
套用到 React:
const ErrorBoundary = Bugsnag.getPlugin('react');
回報 錯誤訊息(notify)
// Bugsnag.notify(err, onError, cb)
import Bugsnag from '@bugsnag/js';
Bugsnag.notify(new Error('Something broke!'));
// 使用 onError function
Bugsnag.notify(new Error('Bad, but not fatal'), (event) => {
// return false 就不會送出
if (event.getUser().id === '1') return false;
event.severity = 'info';
event.context = 'component-space-246';
event.setUser('1', 'bugs.nag@bugsnag.com', 'B. Nag');
event.addMetadata('hyperflopz', { count: 23 });
});
// 使用 callback
Bugsnag.notify(new Error('uh oh'), null, function (err, event) {
if (err) {
console.log('Failed to send report because of:\n' + err.stack);
} else {
console.log('Successfully sent report "' + event.errors[0].errorMessage + '"');
}
});