Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const intercept = (opts, cb = () => {}) => {
if (typeof opts === 'function') {
cb = opts
opts = {}
}
// capture a stacktrace in case a resulting error has nothing
const fallbackStack = getStack()
return (err, ...data) => {
if (err) {
// check if the stacktrace has no context, if so, if so append the frames we created earlier
if (err.stack) maybeUseFallbackStack(err, fallbackStack)
const report = createReportFromErr(err, {
severity: 'warning',
unhandled: false,
severityReason: { type: 'callbackErrorIntercept' }
})
client.notify(report, opts)
return
}
cb(...data) // eslint-disable-line
}
}
const contextualize = (fn, opts) => {
// capture a stacktrace in case a resulting error has nothing
const fallbackStack = getStack()
const dom = domain.create()
dom.on('error', err => {
// check if the stacktrace has no context, if so, if so append the frames we created earlier
if (err.stack) maybeUseFallbackStack(err, fallbackStack)
const report = createReportFromErr(err, {
severity: 'error',
unhandled: true,
severityReason: { type: 'unhandledException' }
})
client.notify(report, opts, (e, report) => {
if (e) client._logger.error('Failed to send report to Bugsnag')
client.config.onUncaughtException(err, report, client._logger)
})
})
process.nextTick(() => dom.run(fn))