demo · v133

Promise rejection router

An unhandledrejection handler that picks the right log level by classifying the reason. Throw seven different things and watch the router demote duck-typed objects, escalate real Error instances, and recognise DOMException as the same family — all through Error.isError().

throw a:

errors will appear here, classified by the router.

the router

window.addEventListener("unhandledrejection", (e) => {
  const r = e.reason;
  if (Error.isError(r)) {
    // real Error or DOMException — ship to error-tracker
    track(r);
  } else if (r && typeof r.message === "string") {
    // duck-typed: log but don't page on-call
    log.warn(r.message);
  } else {
    // primitive: dev typo, swallow with debug
    log.debug(r);
  }
});

The pre-133 alternative was r instanceof Error, which (a) doesn't recognise DOMException, and (b) silently lies when the value comes from another realm: a fetch from a worker, a postMessage from an iframe, a Webview chain — all pass typeof r === "object" but flunk instanceof Error. Error.isError looks at the internal slot, so it crosses realms reliably.

see also