Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'use strict'
const { keepFuncProps } = require('keep-func-props')
const { result } = require('../utils')
const { throwError, normalizeError, isError } = require('./main')
const { throwPb } = require('./props')
// Wrap a function with a error handler
// Allow passing an empty error handler, i.e. ignoring any error thrown
const addErrorHandler = function(func, errorHandler = () => undefined) {
return errorHandledFunc.bind(null, func, errorHandler)
}
const kAddErrorHandler = keepFuncProps(addErrorHandler)
const errorHandledFunc = function(func, errorHandler, ...args) {
try {
const retVal = func(...args)
// eslint-disable-next-line promise/prefer-await-to-then
return retVal && typeof retVal.then === 'function'
? retVal.catch(error => errorHandler(error, ...args))
: retVal
} catch (error) {
return errorHandler(error, ...args)
}
}
// Use `addErrorHandler()` with a generic error handler that rethrows
const addGenErrorHandler = function(func, { message, reason, extra }) {