Skip to content

Commit

Permalink
Report unhandled rejections and exceptions to the host process
Browse files Browse the repository at this point in the history
  • Loading branch information
d-gubert committed Nov 19, 2024
1 parent 162d1d2 commit 0857b39
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
33 changes: 33 additions & 0 deletions packages/apps-engine/deno-runtime/error-handlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as Messenger from './lib/messenger.ts';

export function unhandledRejectionListener(event: PromiseRejectionEvent) {
event.preventDefault();

const { type, reason } = event;

Messenger.sendNotification({
method: 'unhandledRejection',
params: [
{
type,
reason: reason instanceof Error ? reason.message : reason,
timestamp: new Date(),
},
],
});
}

export function unhandledExceptionListener(event: ErrorEvent) {
event.preventDefault();

const { type, message, filename, lineno, colno } = event;
Messenger.sendNotification({
method: 'unhandledException',
params: [{ type, message, filename, lineno, colno }],
});
}

export default function registerErrorListeners() {
addEventListener('unhandledrejection', unhandledRejectionListener);
addEventListener('error', unhandledExceptionListener);
}
3 changes: 3 additions & 0 deletions packages/apps-engine/deno-runtime/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import videoConferenceHandler from './handlers/videoconference-handler.ts';
import apiHandler from './handlers/api-handler.ts';
import handleApp from './handlers/app/handler.ts';
import handleScheduler from './handlers/scheduler-handler.ts';
import registerErrorListeners from './error-handlers.ts';

type Handlers = {
app: typeof handleApp;
Expand Down Expand Up @@ -126,4 +127,6 @@ async function main() {
}
}

registerErrorListeners();

main();
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ export class DenoRuntimeSubprocessController extends EventEmitter {
this.deno.stderr.on('data', this.parseError.bind(this));
this.deno.on('error', (err) => {
this.state = 'invalid';
console.error('Failed to startup Deno subprocess', err);
console.error(`Failed to startup Deno subprocess for app ${this.getAppId()}`, err);
});
this.once('ready', this.onReady.bind(this));
this.parseStdout(this.deno.stdout);
Expand Down Expand Up @@ -544,6 +544,10 @@ export class DenoRuntimeSubprocessController extends EventEmitter {
case 'log':
console.log('SUBPROCESS LOG', message);
break;
case 'unhandledRejection':
case 'unhandledException':
this.debug('Unhandled error of type "%s" caught in subprocess', method);
break;
default:
console.warn('Unrecognized method from sub process');
break;
Expand Down

0 comments on commit 0857b39

Please sign in to comment.