feat(core): intercept unhandled promise rejections (#12910)

Provide a programmatic means of intercepting rejected promises without a
.catch() handler. Needed for Node compat mode.

Also do a first pass at uncaughtException support because they're
closely intertwined in Node. It's like that Frank Sinatra song:
you can't have one without the other.

Stepping stone for #7013.
This commit is contained in:
Ben Noordhuis 2021-11-28 00:46:12 +01:00 committed by GitHub
parent 993a1dd41a
commit 2d830c263b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 271 additions and 37 deletions

View file

@ -115,5 +115,31 @@ declare namespace Deno {
function setMacrotaskCallback(
cb: () => bool,
): void;
/**
* Set a callback that will be called when a promise without a .catch
* handler is rejected. Returns the old handler or undefined.
*/
function setPromiseRejectCallback(
cb: PromiseRejectCallback,
): undefined | PromiseRejectCallback;
export type PromiseRejectCallback = (
type: number,
promise: Promise,
reason: any,
) => void;
/**
* Set a callback that will be called when an exception isn't caught
* by any try/catch handlers. Currently only invoked when the callback
* to setPromiseRejectCallback() throws an exception but that is expected
* to change in the future. Returns the old handler or undefined.
*/
function setUncaughtExceptionCallback(
cb: UncaughtExceptionCallback,
): undefined | UncaughtExceptionCallback;
export type UncaughtExceptionCallback = (err: any) => void;
}
}