All files index2.js

100% Statements 18/18
100% Branches 8/8
100% Functions 5/5
100% Lines 16/16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 261x 18x 18x 90x   18x     72x   1x 18x   18x 18x 18x 18x 18x 18x       1x   1x  
const buildError = (message, opts) => {
  const error = new Error(message);
  for (const key in opts) {
    error[key] = opts[key] instanceof Function ? opts[key](opts) : opts[key];
  }
  return error;
};
 
const singleSlash = str => '/' + str.split('/').filter(one => one).join('/');
 
const ErrorFactory = function (namespace = '', defaults = {}) {
  defaults.namespace = defaults.namespace || namespace;
 
  return function ErrorInstance (code = '', options = {}) {
    options = Object.assign({}, ErrorFactory.options, defaults, options);
    options.code = singleSlash(options.namespace + '/' + code);
    options.id = options.code.toLowerCase().replace(/[^\w]+/g, '-').replace(/^-/, '');
    options.message = ErrorInstance[code];
    return buildError(options.message, options);
  };
};
 
ErrorFactory.options = { status: 500 };
 
module.exports = ErrorFactory;