Skip to content

Commit 4782b04

Browse files
committed
Use modern error handlers and WIP on processing string errors.
1 parent af003c5 commit 4782b04

File tree

1 file changed

+22
-32
lines changed

1 file changed

+22
-32
lines changed

packages/browser/src/plugins/BrowserGlobalHandlerPlugin.ts

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,24 @@ export class BrowserGlobalHandlerPlugin implements IEventPlugin {
2121
Error.stackTraceLimit = 50;
2222

2323
// TODO: Discus if we want to unwire this handler in suspend?
24-
const originalOnError: OnErrorEventHandler = globalThis.onerror;
25-
globalThis.onerror = (event: Event | string, source?: string, lineno?: number, colno?: number, error?: Error) => {
26-
// TODO: Handle async
27-
void this._client.createUnhandledException(error || this.buildError(event, source, lineno, colno), "onerror")
28-
.setSource(source)
29-
.submit();
24+
window.addEventListener("error", async event => {
25+
await this._client.submitUnhandledException(this.getError(event), "onerror");
26+
});
3027

31-
// eslint-disable-next-line prefer-rest-params
32-
return originalOnError ? originalOnError.apply(this, ...arguments) : false;
33-
};
34-
35-
const originalOnunhandledrejection = globalThis.onunhandledrejection;
36-
globalThis.onunhandledrejection = (pre: PromiseRejectionEvent) => {
37-
let error = pre.reason;
28+
window.addEventListener("unhandledrejection", async event => {
29+
let error = event.reason;
3830
try {
39-
const reason = (<any>pre).detail?.reason;
31+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
32+
const reason = (<any>event).detail?.reason;
4033
if (reason) {
4134
error = reason;
4235
}
4336
// eslint-disable-next-line no-empty
4437
} catch (ex) { }
4538

46-
// TODO: Handle async
47-
void this._client.submitUnhandledException(error, "onunhandledrejection");
39+
await this._client.submitUnhandledException(error, "onunhandledrejection");
40+
});
4841

49-
// eslint-disable-next-line prefer-rest-params
50-
return originalOnunhandledrejection ? originalOnunhandledrejection.apply(this, ...arguments) : false;
51-
};
5242

5343
if (typeof $ !== "undefined" && $(document)) {
5444
$(document).ajaxError((event: Event, xhr: { responseText: string, status: number }, settings: { data: unknown, url: string }, error: string) => {
@@ -70,28 +60,28 @@ export class BrowserGlobalHandlerPlugin implements IEventPlugin {
7060
return Promise.resolve();
7161
}
7262

73-
private buildError(event: Event | string, source?: string, lineno?: number, colno?: number): Error {
74-
if (Object.prototype.toString.call(event) === "[object ErrorEvent]") {
75-
// TODO: See if this is the error event.
76-
return (<ErrorEvent>event).error;
63+
private getError(event: ErrorEvent): Error {
64+
const { error, message, filename, lineno, colno } = event;
65+
if (typeof error === "object") {
66+
return error;
7767
}
7868

7969
let name: string = "Error";
80-
let message: string = Object.prototype.toString.call(event) === '[object ErrorEvent]' ? (<ErrorEvent>event).error : null;
81-
if (message) {
82-
const errorNameRegex: RegExp = /^(?:[Uu]ncaught (?:exception: )?)?(?:((?:Aggregate|Eval|Internal|Range|Reference|Syntax|Type|URI|)Error): )?(.*)$)/i;
83-
const [_, errorName, errorMessage] = errorNameRegex.exec(message);
70+
let msg: string = message || event.error;
71+
if (msg) {
72+
const errorNameRegex: RegExp = /^(?:[Uu]ncaught (?:exception: )?)?(?:((?:Aggregate|Eval|Internal|Range|Reference|Syntax|Type|URI|)Error): )?(.*)$/i;
73+
const [_, errorName, errorMessage] = errorNameRegex.exec(msg);
8474
if (errorName) {
8575
name = errorName;
8676
}
8777
if (errorMessage) {
88-
message = errorMessage;
78+
msg = errorMessage;
8979
}
9080
}
9181

92-
const error = new Error(message || "Script error.");
93-
error.name = name;
94-
error.stack = `at ${source || ""}:${!isNaN(lineno) ? lineno : 0}${!isNaN(colno) ? ":" + colno : ""}`;
95-
return error;
82+
const ex = new Error(msg || "Script error.");
83+
ex.name = name;
84+
ex.stack = `at ${filename || ""}:${!isNaN(lineno) ? lineno : 0}${!isNaN(colno) ? ":" + colno : ""}`;
85+
return ex;
9686
}
9787
}

0 commit comments

Comments
 (0)