# All files / server/error index.js

100% Statements 18/18
100% Branches 8/8
100% Functions 5/5
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    2630x
    50x
    50x
    308x

    50x

    222x

    30x
    102x

    102x
    50x
    50x
    50x
    50x
    50x

    30x

    30x
     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;