All files / error index.js

93.75% Statements 15/16
85.71% Branches 6/7
100% Functions 3/3
93.33% Lines 14/15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 211x 2x 7x 7x 7x     7x 7x 7x 37x   7x   2x     1x 1x 1x  
const factory = (defaults = {}) => {
  const error = function(code, opts){
    opts = Object.assign({}, module.exports.options, defaults, opts);
    opts.code = code;
    Iif (!error[code]) {
      throw new Error(`The error '${code}' does not exist so you cannot use it.`);
    }
    const message = error[code] instanceof Function ? error[code](opts) : error[code];
    const err = new Error(message);
    for (let key in opts) {
      err[key] = opts[key] instanceof Function ? opts[key](opts) : opts[key];
    }
    return err;
  };
  return error;
}
 
module.exports = factory();
module.exports.options = {};
module.exports.defaults = defaults => factory(defaults);