5 function createErrorType(code, message, Base) {
10 function getMessage (arg1, arg2, arg3) {
11 if (typeof message === 'string') {
14 return message(arg1, arg2, arg3)
18 class NodeError extends Base {
19 constructor (arg1, arg2, arg3) {
20 super(getMessage(arg1, arg2, arg3));
24 NodeError.prototype.name = Base.name;
25 NodeError.prototype.code = code;
27 codes[code] = NodeError;
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));
36 return `one of ${thing} ${expected.slice(0, len - 1).join(', ')}, or ` +
38 } else if (len === 2) {
39 return `one of ${thing} ${expected[0]} or ${expected[1]}`;
41 return `of ${thing} ${expected[0]}`;
44 return `of ${thing} ${String(expected)}`;
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;
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;
58 return str.substring(this_len - search.length, this_len) === search;
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') {
67 if (start + search.length > str.length) {
70 return str.indexOf(search, start) !== -1;
74 createErrorType('ERR_INVALID_OPT_VALUE', function (name, value) {
75 return 'The value "' + value + '" is invalid for option "' + name + '"'
77 createErrorType('ERR_INVALID_ARG_TYPE', function (name, expected, actual) {
78 // determiner: 'must be' or 'must not be'
80 if (typeof expected === 'string' && startsWith(expected, 'not ')) {
81 determiner = 'must not be';
82 expected = expected.replace(/^not /, '');
84 determiner = 'must be';
88 if (endsWith(name, ' argument')) {
89 // For cases like 'first argument'
90 msg = `The ${name} ${determiner} ${oneOf(expected, 'type')}`;
92 const type = includes(name, '.') ? 'property' : 'argument';
93 msg = `The "${name}" ${type} ${determiner} ${oneOf(expected, 'type')}`;
96 msg += `. Received type ${typeof actual}`;
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'
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';
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
114 createErrorType('ERR_STREAM_UNSHIFT_AFTER_END_EVENT', 'stream.unshift() after end event');
116 module.exports.codes = codes;