All files / server/src/error index.js

35.29% Statements 6/17
0% Branches 0/14
20% Functions 1/5
33.33% Lines 5/15
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 26 27 28 29 30 31 32 33 34 35 36 37 38 3912x 12x         53x                       12x                       12x                
const native = require('./errors.js');
const nicknames = {
  EADDRINUSE: 'server.native.portused'
};
 
// Create a new error from all the available ones and the argument passed
module.exports = errors => (name, opts) => {
  const err = errors && errors[name] ?
    (errors[name] instanceof Function ?
      errors[name](opts) :
      errors[name]
    ) : new Error(name);
 
  err.name = name;
  throw err;
};
 
// Same as above, but named "throw"
module.exports.throw = errors => (name, opts) => {
  const err = errors && errors[name] ?
    (errors[name] instanceof Function ?
      errors[name](opts) :
      errors[name]
    ) : new Error(name);
 
  err.name = name;
  throw err;
};
 
// Map the native error to the custom defined one through the nicknames
module.exports.native = err => {
  if (nicknames[err.code]) {
    return native(nicknames[err.code], err);
  }
  err.type = `server.native.${err.code}`;
 
  return err;
};