2018-10-19 15:25:29 -04:00
|
|
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
2018-10-12 14:22:52 -04:00
|
|
|
import { PromiseRejectEvent } from "./libdeno";
|
|
|
|
|
|
|
|
/* tslint:disable-next-line:no-any */
|
|
|
|
const rejectMap = new Map<Promise<any>, string>();
|
|
|
|
// For uncaught promise rejection errors
|
|
|
|
|
|
|
|
/* tslint:disable-next-line:no-any */
|
|
|
|
const otherErrorMap = new Map<Promise<any>, string>();
|
|
|
|
// For reject after resolve / resolve after resolve errors
|
|
|
|
|
|
|
|
export function promiseRejectHandler(
|
|
|
|
error: Error | string,
|
|
|
|
event: PromiseRejectEvent,
|
|
|
|
/* tslint:disable-next-line:no-any */
|
|
|
|
promise: Promise<any>
|
|
|
|
) {
|
|
|
|
switch (event) {
|
|
|
|
case "RejectWithNoHandler":
|
|
|
|
rejectMap.set(promise, (error as Error).stack || "RejectWithNoHandler");
|
|
|
|
break;
|
|
|
|
case "HandlerAddedAfterReject":
|
|
|
|
rejectMap.delete(promise);
|
|
|
|
break;
|
2018-12-03 18:47:00 -05:00
|
|
|
case "ResolveAfterResolved":
|
|
|
|
// Should not warn. See #1272
|
|
|
|
break;
|
2018-10-12 14:22:52 -04:00
|
|
|
default:
|
|
|
|
// error is string here
|
|
|
|
otherErrorMap.set(promise, `Promise warning: ${error as string}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return true when continue, false to die on uncaught promise reject
|
|
|
|
export function promiseErrorExaminer(): boolean {
|
|
|
|
if (otherErrorMap.size > 0) {
|
|
|
|
for (const msg of otherErrorMap.values()) {
|
|
|
|
console.log(msg);
|
|
|
|
}
|
|
|
|
otherErrorMap.clear();
|
|
|
|
}
|
|
|
|
if (rejectMap.size > 0) {
|
|
|
|
for (const msg of rejectMap.values()) {
|
|
|
|
console.log(msg);
|
|
|
|
}
|
|
|
|
rejectMap.clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|