safety-catch
Version:
Small module that makes sure your catch, caught an actual error and not a programming mistake or assertion
25 lines (21 loc) • 543 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' ||
err.name === 'AssertionError'
}
function throwErrorNT (err) {
queueMicrotask(() => { throw err })
}
function safetyCatch (err) {
if (isActuallyUncaught(err)) {
throwErrorNT(err)
throw err
}
}