Skip to content

Commit

Permalink
[b] notifier: factor out unhandledrejection into an option
Browse files Browse the repository at this point in the history
Fixes #915
(Allow disabling handling of unhandledrejection)
  • Loading branch information
kyrylo committed Jan 11, 2023
1 parent f64d13b commit 11abeee
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 36 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
- Added support for error reporting of "falsey" errors such as `null`, `NaN`,
`undefined`, `false`, `""`
([#1345](https://github.com/airbrake/airbrake-js/pull/1345))
- Added the `instrumentation.unhandledrejection` option, which enables/disables
the Airbrake handler for the `unhandledrejection` event
([#1356](https://github.com/airbrake/airbrake-js/pull/1356))

### [2.1.8] (December 6, 2022)

Expand Down
16 changes: 16 additions & 0 deletions packages/browser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,21 @@ const airbrake = new Notifier({
});
```

#### unhandledrejection

Instruments the [unhandledrejection][unhandledrejection] event and sends
performance statistics to Airbrake. You can disable that behavior using the
`instrumentation` option:

```js
const airbrake = new Notifier({
// ...
instrumentation: {
unhandledrejection: false,
},
});
```

### APM

#### Routes
Expand Down Expand Up @@ -372,3 +387,4 @@ this.airbrake.queues.notify(queueInfo);
[fetch]: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
[onerror]: https://developer.mozilla.org/en-US/docs/Web/API/Window/error_event
[xhr]: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
[unhandledrejection]: https://developer.mozilla.org/en-US/docs/Web/API/Window/unhandledrejection_event
33 changes: 33 additions & 0 deletions packages/browser/src/instrumentation/unhandledrejection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Notifier } from '../notifier';

export function instrumentUnhandledrejection(notifier: Notifier): void {
const handler = onUnhandledrejection.bind(notifier);

window.addEventListener('unhandledrejection', handler);
notifier._onClose.push(() => {
window.removeEventListener('unhandledrejection', handler);
});
}

function onUnhandledrejection(e: any): void {
// Handle native or bluebird Promise rejections
// https://developer.mozilla.org/en-US/docs/Web/Events/unhandledrejection
// http://bluebirdjs.com/docs/api/error-management-configuration.html
let reason = e.reason || (e.detail && e.detail.reason);
if (!reason) return;

let msg = reason.message || String(reason);
if (msg.indexOf && msg.indexOf('airbrake: ') === 0) return;

if (typeof reason !== 'object' || reason.error === undefined) {
this.notify({
error: reason,
context: {
unhandledRejection: true,
},
});
return;
}

this.notify({ ...reason, context: { unhandledRejection: true } });
}
43 changes: 7 additions & 36 deletions packages/browser/src/notifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { instrumentDOM } from './instrumentation/dom';
import { instrumentFetch } from './instrumentation/fetch';
import { instrumentLocation } from './instrumentation/location';
import { instrumentXHR } from './instrumentation/xhr';
import { instrumentUnhandledrejection } from './instrumentation/unhandledrejection';
import { INotice } from './notice';
import { IInstrumentationOptions, IOptions } from './options';

Expand Down Expand Up @@ -37,16 +38,9 @@ export class Notifier extends BaseNotifier {
this.onOffline = this.onOffline.bind(this);
window.addEventListener('offline', this.onOffline);

this.onUnhandledrejection = this.onUnhandledrejection.bind(this);
window.addEventListener('unhandledrejection', this.onUnhandledrejection);

this._onClose.push(() => {
window.removeEventListener('online', this.onOnline);
window.removeEventListener('offline', this.onOffline);
window.removeEventListener(
'unhandledrejection',
this.onUnhandledrejection
);
});
}

Expand Down Expand Up @@ -83,6 +77,12 @@ export class Notifier extends BaseNotifier {
if (enabled(opt.xhr) && typeof XMLHttpRequest !== 'undefined') {
instrumentXHR(this);
}
if (
enabled(opt.unhandledrejection) &&
typeof addEventListener === 'function'
) {
instrumentUnhandledrejection(this);
}
}

public notify(err: any): Promise<INotice> {
Expand Down Expand Up @@ -123,35 +123,6 @@ export class Notifier extends BaseNotifier {
this.offline = true;
}

protected onUnhandledrejection(e: any): void {
// Handle native or bluebird Promise rejections
// https://developer.mozilla.org/en-US/docs/Web/Events/unhandledrejection
// http://bluebirdjs.com/docs/api/error-management-configuration.html
let reason = e.reason || (e.detail && e.detail.reason);
if (!reason) {
return;
}
let msg = reason.message || String(reason);
if (msg.indexOf && msg.indexOf('airbrake: ') === 0) {
return;
}
if (typeof reason !== 'object' || reason.error === undefined) {
this.notify({
error: reason,
context: {
unhandledRejection: true,
},
});
return;
}
this.notify({
...reason,
context: {
unhandledRejection: true,
},
});
}

onerror(
message: string,
filename?: string,
Expand Down
1 change: 1 addition & 0 deletions packages/browser/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface IInstrumentationOptions {
history?: boolean;
console?: boolean;
xhr?: boolean;
unhandledrejection?: boolean;
}

export interface IOptions {
Expand Down

0 comments on commit 11abeee

Please sign in to comment.