Skip to content

Commit 0f672d9

Browse files
committed
Added additional checks for unhandledRejection errors
1 parent 6a0eecf commit 0f672d9

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

example/browser/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ const registerEventHandlers = () => {
117117
.querySelector("#throw-promise-unhandled-rejection")
118118
.addEventListener("click", () => {
119119
const promiseFn = () => new Promise(function (_, reject) {
120-
switch (Math.floor(Math.random() * 4)) {
120+
switch (Math.floor(Math.random() * 5)) {
121121
case 0:
122122
reject(0);
123123
break;
@@ -130,6 +130,8 @@ const registerEventHandlers = () => {
130130
case 3:
131131
reject();
132132
break;
133+
case 4:
134+
throw new Error("Error thrown from promise");
133135
}
134136
});
135137

packages/browser/src/plugins/BrowserGlobalHandlerPlugin.ts

+18-6
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,24 @@ export class BrowserGlobalHandlerPlugin implements IEventPlugin {
2727

2828
window.addEventListener("unhandledrejection", event => {
2929
let reason: unknown = event.reason;
30-
try {
31-
const detailReason = (<{ detail?: { reason: string } }>event).detail?.reason;
32-
if (detailReason) {
33-
reason = detailReason;
34-
}
35-
} catch (ex) { /* empty */ }
30+
if (!(reason instanceof Error)) {
31+
try {
32+
// Check for reason in legacy CustomEvents (https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent)
33+
const detailReason = (<{ detail?: { reason: string } }>event).detail?.reason;
34+
if (detailReason) {
35+
reason = detailReason;
36+
}
37+
} catch (ex) { /* empty */ }
38+
}
39+
40+
if (!(reason instanceof Error)) {
41+
try {
42+
const error = event.reason.error;
43+
if (error) {
44+
reason = error;
45+
}
46+
} catch (ex) { /* empty */ }
47+
}
3648

3749
const error: Error = toError(reason, "Unhandled rejection")
3850
void this._client?.submitUnhandledException(error, "onunhandledrejection");

packages/node/src/plugins/NodeGlobalHandlerPlugin.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class NodeGlobalHandlerPlugin implements IEventPlugin {
2323
void this._client?.submitUnhandledException(error, "uncaughtException");
2424
});
2525

26-
process.addListener("unhandledRejection", (reason: unknown | null | undefined) => {
26+
process.addListener("unhandledRejection", (reason: unknown) => {
2727
const error: Error = toError(reason, "Unhandled rejection")
2828
void this._client?.submitUnhandledException(error, "unhandledRejection");
2929
});

0 commit comments

Comments
 (0)