errors.js
  1  'use strict';
  2  
  3  const codes = {};
  4  
  5  function createErrorType(code, message, Base) {
  6    if (!Base) {
  7      Base = Error
  8    }
  9  
 10    function getMessage (arg1, arg2, arg3) {
 11      if (typeof message === 'string') {
 12        return message
 13      } else {
 14        return message(arg1, arg2, arg3)
 15      }
 16    }
 17  
 18    class NodeError extends Base {
 19      constructor (arg1, arg2, arg3) {
 20        super(getMessage(arg1, arg2, arg3));
 21      }
 22    }
 23  
 24    NodeError.prototype.name = Base.name;
 25    NodeError.prototype.code = code;
 26  
 27    codes[code] = NodeError;
 28  }
 29  
 30  // https://github.com/nodejs/node/blob/v10.8.0/lib/internal/errors.js
 31  function oneOf(expected, thing) {
 32    if (Array.isArray(expected)) {
 33      const len = expected.length;
 34      expected = expected.map((i) => String(i));
 35      if (len > 2) {
 36        return `one of ${thing} ${expected.slice(0, len - 1).join(', ')}, or ` +
 37               expected[len - 1];
 38      } else if (len === 2) {
 39        return `one of ${thing} ${expected[0]} or ${expected[1]}`;
 40      } else {
 41        return `of ${thing} ${expected[0]}`;
 42      }
 43    } else {
 44      return `of ${thing} ${String(expected)}`;
 45    }
 46  }
 47  
 48  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith
 49  function startsWith(str, search, pos) {
 50  	return str.substr(!pos || pos < 0 ? 0 : +pos, search.length) === search;
 51  }
 52  
 53  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith
 54  function endsWith(str, search, this_len) {
 55  	if (this_len === undefined || this_len > str.length) {
 56  		this_len = str.length;
 57  	}
 58  	return str.substring(this_len - search.length, this_len) === search;
 59  }
 60  
 61  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes
 62  function includes(str, search, start) {
 63    if (typeof start !== 'number') {
 64      start = 0;
 65    }
 66  
 67    if (start + search.length > str.length) {
 68      return false;
 69    } else {
 70      return str.indexOf(search, start) !== -1;
 71    }
 72  }
 73  
 74  createErrorType('ERR_INVALID_OPT_VALUE', function (name, value) {
 75    return 'The value "' + value + '" is invalid for option "' + name + '"'
 76  }, TypeError);
 77  createErrorType('ERR_INVALID_ARG_TYPE', function (name, expected, actual) {
 78    // determiner: 'must be' or 'must not be'
 79    let determiner;
 80    if (typeof expected === 'string' && startsWith(expected, 'not ')) {
 81      determiner = 'must not be';
 82      expected = expected.replace(/^not /, '');
 83    } else {
 84      determiner = 'must be';
 85    }
 86  
 87    let msg;
 88    if (endsWith(name, ' argument')) {
 89      // For cases like 'first argument'
 90      msg = `The ${name} ${determiner} ${oneOf(expected, 'type')}`;
 91    } else {
 92      const type = includes(name, '.') ? 'property' : 'argument';
 93      msg = `The "${name}" ${type} ${determiner} ${oneOf(expected, 'type')}`;
 94    }
 95  
 96    msg += `. Received type ${typeof actual}`;
 97    return msg;
 98  }, TypeError);
 99  createErrorType('ERR_STREAM_PUSH_AFTER_EOF', 'stream.push() after EOF');
100  createErrorType('ERR_METHOD_NOT_IMPLEMENTED', function (name) {
101    return 'The ' + name + ' method is not implemented'
102  });
103  createErrorType('ERR_STREAM_PREMATURE_CLOSE', 'Premature close');
104  createErrorType('ERR_STREAM_DESTROYED', function (name) {
105    return 'Cannot call ' + name + ' after a stream was destroyed';
106  });
107  createErrorType('ERR_MULTIPLE_CALLBACK', 'Callback called multiple times');
108  createErrorType('ERR_STREAM_CANNOT_PIPE', 'Cannot pipe, not readable');
109  createErrorType('ERR_STREAM_WRITE_AFTER_END', 'write after end');
110  createErrorType('ERR_STREAM_NULL_VALUES', 'May not write null values to stream', TypeError);
111  createErrorType('ERR_UNKNOWN_ENCODING', function (arg) {
112    return 'Unknown encoding: ' + arg
113  }, TypeError);
114  createErrorType('ERR_STREAM_UNSHIFT_AFTER_END_EVENT', 'stream.unshift() after end event');
115  
116  module.exports.codes = codes;