Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/assert/build/internal/errors.js
1126 views
1
// Currently in sync with Node.js lib/internal/errors.js
2
// https://github.com/nodejs/node/commit/3b044962c48fe313905877a96b5d0894a5404f6f
3
4
/* eslint node-core/documented-errors: "error" */
5
6
/* eslint node-core/alphabetize-errors: "error" */
7
8
/* eslint node-core/prefer-util-format-errors: "error" */
9
'use strict'; // The whole point behind this internal module is to allow Node.js to no
10
// longer be forced to treat every error message change as a semver-major
11
// change. The NodeError classes here all expose a `code` property whose
12
// value statically and permanently identifies the error. While the error
13
// message may change, the code should not.
14
15
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
16
17
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
18
19
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
20
21
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
22
23
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
24
25
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
26
27
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
28
29
var codes = {}; // Lazy loaded
30
31
var assert;
32
var util;
33
34
function createErrorType(code, message, Base) {
35
if (!Base) {
36
Base = Error;
37
}
38
39
function getMessage(arg1, arg2, arg3) {
40
if (typeof message === 'string') {
41
return message;
42
} else {
43
return message(arg1, arg2, arg3);
44
}
45
}
46
47
var NodeError =
48
/*#__PURE__*/
49
function (_Base) {
50
_inherits(NodeError, _Base);
51
52
function NodeError(arg1, arg2, arg3) {
53
var _this;
54
55
_classCallCheck(this, NodeError);
56
57
_this = _possibleConstructorReturn(this, _getPrototypeOf(NodeError).call(this, getMessage(arg1, arg2, arg3)));
58
_this.code = code;
59
return _this;
60
}
61
62
return NodeError;
63
}(Base);
64
65
codes[code] = NodeError;
66
} // https://github.com/nodejs/node/blob/v10.8.0/lib/internal/errors.js
67
68
69
function oneOf(expected, thing) {
70
if (Array.isArray(expected)) {
71
var len = expected.length;
72
expected = expected.map(function (i) {
73
return String(i);
74
});
75
76
if (len > 2) {
77
return "one of ".concat(thing, " ").concat(expected.slice(0, len - 1).join(', '), ", or ") + expected[len - 1];
78
} else if (len === 2) {
79
return "one of ".concat(thing, " ").concat(expected[0], " or ").concat(expected[1]);
80
} else {
81
return "of ".concat(thing, " ").concat(expected[0]);
82
}
83
} else {
84
return "of ".concat(thing, " ").concat(String(expected));
85
}
86
} // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith
87
88
89
function startsWith(str, search, pos) {
90
return str.substr(!pos || pos < 0 ? 0 : +pos, search.length) === search;
91
} // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith
92
93
94
function endsWith(str, search, this_len) {
95
if (this_len === undefined || this_len > str.length) {
96
this_len = str.length;
97
}
98
99
return str.substring(this_len - search.length, this_len) === search;
100
} // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes
101
102
103
function includes(str, search, start) {
104
if (typeof start !== 'number') {
105
start = 0;
106
}
107
108
if (start + search.length > str.length) {
109
return false;
110
} else {
111
return str.indexOf(search, start) !== -1;
112
}
113
}
114
115
createErrorType('ERR_AMBIGUOUS_ARGUMENT', 'The "%s" argument is ambiguous. %s', TypeError);
116
createErrorType('ERR_INVALID_ARG_TYPE', function (name, expected, actual) {
117
if (assert === undefined) assert = require('../assert');
118
assert(typeof name === 'string', "'name' must be a string"); // determiner: 'must be' or 'must not be'
119
120
var determiner;
121
122
if (typeof expected === 'string' && startsWith(expected, 'not ')) {
123
determiner = 'must not be';
124
expected = expected.replace(/^not /, '');
125
} else {
126
determiner = 'must be';
127
}
128
129
var msg;
130
131
if (endsWith(name, ' argument')) {
132
// For cases like 'first argument'
133
msg = "The ".concat(name, " ").concat(determiner, " ").concat(oneOf(expected, 'type'));
134
} else {
135
var type = includes(name, '.') ? 'property' : 'argument';
136
msg = "The \"".concat(name, "\" ").concat(type, " ").concat(determiner, " ").concat(oneOf(expected, 'type'));
137
} // TODO(BridgeAR): Improve the output by showing `null` and similar.
138
139
140
msg += ". Received type ".concat(_typeof(actual));
141
return msg;
142
}, TypeError);
143
createErrorType('ERR_INVALID_ARG_VALUE', function (name, value) {
144
var reason = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'is invalid';
145
if (util === undefined) util = require('util/');
146
var inspected = util.inspect(value);
147
148
if (inspected.length > 128) {
149
inspected = "".concat(inspected.slice(0, 128), "...");
150
}
151
152
return "The argument '".concat(name, "' ").concat(reason, ". Received ").concat(inspected);
153
}, TypeError, RangeError);
154
createErrorType('ERR_INVALID_RETURN_VALUE', function (input, name, value) {
155
var type;
156
157
if (value && value.constructor && value.constructor.name) {
158
type = "instance of ".concat(value.constructor.name);
159
} else {
160
type = "type ".concat(_typeof(value));
161
}
162
163
return "Expected ".concat(input, " to be returned from the \"").concat(name, "\"") + " function but got ".concat(type, ".");
164
}, TypeError);
165
createErrorType('ERR_MISSING_ARGS', function () {
166
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
167
args[_key] = arguments[_key];
168
}
169
170
if (assert === undefined) assert = require('../assert');
171
assert(args.length > 0, 'At least one arg needs to be specified');
172
var msg = 'The ';
173
var len = args.length;
174
args = args.map(function (a) {
175
return "\"".concat(a, "\"");
176
});
177
178
switch (len) {
179
case 1:
180
msg += "".concat(args[0], " argument");
181
break;
182
183
case 2:
184
msg += "".concat(args[0], " and ").concat(args[1], " arguments");
185
break;
186
187
default:
188
msg += args.slice(0, len - 1).join(', ');
189
msg += ", and ".concat(args[len - 1], " arguments");
190
break;
191
}
192
193
return "".concat(msg, " must be specified");
194
}, TypeError);
195
module.exports.codes = codes;
196