Skip to content

Fixes #32 Addressed different promise rejection error types #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion example/browser/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ <h1>Error Submission</h1>
<button id="throw-string-error">Throw string error</button>
<button id="throw-ignored-error">Throw ignored error</button>
<button onclick="throwReferenceError('function-does-exist')">Throw uncaught reference error</button>
<button id="throw-jquery-ajax-error">Throw jquery ajax error</button>
<button id="throw-jquery-ajax-error">Throw jQuery ajax error</button>
<button id="throw-promise-unhandled-rejection">Throw promise unhandled rejection</button>

<h1>Log Submission</h1>
<button class="submit-log">Submit log event</button>
Expand Down
23 changes: 23 additions & 0 deletions example/browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,29 @@ document.addEventListener("DOMContentLoaded", () => {
});
});

document
.querySelector("#throw-promise-unhandled-rejection")
.addEventListener("click", () => {
const promiseFn = () => new Promise(function (_, reject) {
switch (Math.floor(Math.random() * 4)) {
case 0:
reject(0);
break;
case 1:
reject(new Error("Promise rejected error"));
break;
case 2:
reject("Promise rejected string");
break;
case 3:
reject();
break;
}
});

promiseFn();
});

document
.querySelector("#config-settings-log")
.addEventListener("click", () => {
Expand Down
14 changes: 8 additions & 6 deletions packages/browser/src/plugins/BrowserGlobalHandlerPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {
ExceptionlessClient,
IEventPlugin,
PluginContext
PluginContext,
toError
} from "@exceptionless/core";

declare let $: (document: Document) => { ajaxError: { (document: (event: Event, xhr: { responseText: string; status: number; }, settings: { data: unknown; url: string; }, error: string) => void): void; }; };
Expand All @@ -25,15 +26,16 @@ export class BrowserGlobalHandlerPlugin implements IEventPlugin {
});

window.addEventListener("unhandledrejection", event => {
let error = event.reason;
let reason: unknown = event.reason;
try {
const reason = (<{ detail?: { reason: string} }>event).detail?.reason;
if (reason) {
error = reason;
const detailReason = (<{ detail?: { reason: string } }>event).detail?.reason;
if (detailReason) {
reason = detailReason;
}
} catch (ex) { /* empty */ }

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


Expand Down
16 changes: 16 additions & 0 deletions packages/core/src/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,19 @@ export function toBoolean(input: unknown, defaultValue: boolean = false): boolea

return defaultValue;
}

export function toError(errorOrMessage: unknown, defaultMessage = "Unknown Error"): Error {
if (errorOrMessage === null || errorOrMessage === undefined) {
return new Error(defaultMessage);
}

if (errorOrMessage instanceof Error) {
return errorOrMessage;
}

if (typeof errorOrMessage === "string") {
return new Error(errorOrMessage);
}

return new Error(JSON.stringify(errorOrMessage) || defaultMessage);
}
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,5 @@ export {
startsWith,
stringify,
toBoolean,
toError,
} from "./Utils.js";
6 changes: 4 additions & 2 deletions packages/node/src/plugins/NodeGlobalHandlerPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {
ExceptionlessClient,
IEventPlugin,
PluginContext
PluginContext,
toError
} from "@exceptionless/core";

export class NodeGlobalHandlerPlugin implements IEventPlugin {
Expand All @@ -23,7 +24,8 @@ export class NodeGlobalHandlerPlugin implements IEventPlugin {
});

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

return Promise.resolve();
Expand Down