safety-catch
Version:
Small module that makes sure your catch, caught an actual error and not a programming mistake or assertion
24 lines (20 loc) • 506 B
JavaScript
module.exports = safetyCatch
function isActuallyUncaught (err) {
if (!err) return false
return err instanceof TypeError ||
err instanceof SyntaxError ||
err instanceof ReferenceError ||
err instanceof EvalError ||
err instanceof RangeError ||
err instanceof URIError ||
err.code === 'ERR_ASSERTION'
}
function throwErrorNT (err) {
queueMicrotask(() => { throw err })
}
function safetyCatch (err) {
if (isActuallyUncaught(err)) {
throwErrorNT(err)
throw err
}
}