index.js
function patchErrorBody(err, options) {
  // Gets the current toJSON to be extended.
  const json = err.toJSON();

  Object.keys(customOptions).forEach(optName => {
    let value = options[optName];
    if (value === undefined) {
      value = err.body[optName];
      if (value === '' || value === undefined) {
        value = customOptions[optName](
          err.body.code,
          err.statusCode,
          err.body.message
        );
      }
    }
    if (value !== undefined) {
      // Adds the option to the body of the error.
      err.body[optName] = value;
      // Adds the option to the json representation of the error.
      json[optName] = value;
    }
  });

  // Patchs the toJSON method.
  err.toJSON = () => json;
}
function patchErrorBody(err, options) {
  // Gets the current toJSON to be extended.
  const json = err.toJSON();

  Object.keys(customOptions).forEach(optName => {
    let value = options[optName];
    if (value === undefined) {
      value = err.body[optName];
      if (value === '' || value === undefined) {
        value = customOptions[optName](
          err.body.code,
          err.statusCode,
          err.body.message
        );
      }
    }
    if (value !== undefined) {
      // Adds the option to the body of the error.
      err.body[optName] = value;
      // Adds the option to the json representation of the error.
      json[optName] = value;
    }
  });

  // Patchs the toJSON method.
  err.toJSON = () => json;
}
function patchErrorBody ( err , options ) { // Gets the current toJSON to be extended. const json = err . toJSON ( ) ; Object . keys ( customOptions ) . forEach ( optName => { let value = options [ optName ] ; if ( value === undefined ) { value = err . body [ optName ] ; if ( value === '' || value === undefined ) { value = customOptions [ optName ] ( err . body . code , err . statusCode , err . body . message ) ; } } if ( value !== undefined ) { // Adds the option to the body of the error. err . body [ optName ] = value ; // Adds the option to the json representation of the error. json [ optName ] = value ; } } ) ; // Patchs the toJSON method. err . toJSON = ( ) => json ; }
Monkey patch the error object after his creation.
@param  {object} err Error to patch.
@param {object} options Options given by the user.
Monkey patch the error object after his creation .