27 lines
664 B
TypeScript
27 lines
664 B
TypeScript
type UnhandledRejectionHandler = (reason: unknown) => boolean;
|
|
|
|
const handlers = new Set<UnhandledRejectionHandler>();
|
|
|
|
export function registerUnhandledRejectionHandler(
|
|
handler: UnhandledRejectionHandler,
|
|
): () => void {
|
|
handlers.add(handler);
|
|
return () => {
|
|
handlers.delete(handler);
|
|
};
|
|
}
|
|
|
|
export function isUnhandledRejectionHandled(reason: unknown): boolean {
|
|
for (const handler of handlers) {
|
|
try {
|
|
if (handler(reason)) return true;
|
|
} catch (err) {
|
|
console.error(
|
|
"[clawdbot] Unhandled rejection handler failed:",
|
|
err instanceof Error ? (err.stack ?? err.message) : err,
|
|
);
|
|
}
|
|
}
|
|
return false;
|
|
}
|