How to use the keep-func-props.keepFuncProps function in keep-func-props

To help you get started, we’ve selected a few keep-func-props examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ehmicky / autoserver / src / errors / handler.js View on Github external
'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 }) {

keep-func-props

Wrap a function without changing its name and other properties

MIT
Latest version published 1 year ago

Package Health Score

59 / 100
Full package analysis

Popular keep-func-props functions