Skip to content

Commit

Permalink
feat: add uncaught rejections to support logs
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanalvizo committed May 12, 2023
1 parent 02215b6 commit 4a7c3d2
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/log/src/LogProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ export class LogProxy {
);
};

private handleUncaughtRejection = (event: PromiseRejectionEvent) => {
const messages: string[] = [];
if (event.promise != null) {
messages.push(event.promise.toString());
}
if (event.reason as string) {
messages.push(event.reason as string);
}
this.eventTarget.dispatchEvent(
makeEvent(LOG_PROXY_TYPE.UNCAUGHT_ERROR, messages)
);
};

constructor() {
this.debug = console.debug;
this.log = console.log;
Expand Down Expand Up @@ -83,6 +96,7 @@ export class LogProxy {
};

window.addEventListener('error', this.handleUncaughtError);
window.addEventListener('unhandledrejection', this.handleUncaughtRejection);

// This forces logger to update its reference to the overriding functions instead of the original
Log.setLogLevel(Log.level);
Expand All @@ -98,6 +112,10 @@ export class LogProxy {
console.warn = this.warn;
console.error = this.error;
window.removeEventListener('error', this.handleUncaughtError);
window.removeEventListener(
'unhandledrejection',
this.handleUncaughtRejection
);
Log.setLogLevel(Log.level);
this.isEnabled = false;
}
Expand Down

0 comments on commit 4a7c3d2

Please sign in to comment.