UNPKG

12.6 kBJavaScriptView Raw
1"use strict";
2// The whole point behind this internal module is to allow Node.js to no
3// longer be forced to treat every error message change as a semver-major
4// change. The NodeError classes here all expose a `code` property whose
5// value statically and permanently identifies the error. While the error
6// message may change, the code should not.
7var __extends = (this && this.__extends) || (function () {
8 var extendStatics = function (d, b) {
9 extendStatics = Object.setPrototypeOf ||
10 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
11 function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
12 return extendStatics(d, b);
13 };
14 return function (d, b) {
15 if (typeof b !== "function" && b !== null)
16 throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
17 extendStatics(d, b);
18 function __() { this.constructor = d; }
19 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
20 };
21})();
22Object.defineProperty(exports, "__esModule", { value: true });
23exports.E = exports.AssertionError = exports.message = exports.RangeError = exports.TypeError = exports.Error = void 0;
24var assert = require("assert");
25var util = require("util");
26var kCode = typeof Symbol === 'undefined' ? '_kCode' : Symbol('code');
27var messages = {}; // new Map();
28function makeNodeError(Base) {
29 return /** @class */ (function (_super) {
30 __extends(NodeError, _super);
31 function NodeError(key) {
32 var args = [];
33 for (var _i = 1; _i < arguments.length; _i++) {
34 args[_i - 1] = arguments[_i];
35 }
36 var _this = _super.call(this, message(key, args)) || this;
37 _this.code = key;
38 _this[kCode] = key;
39 _this.name = "".concat(_super.prototype.name, " [").concat(_this[kCode], "]");
40 return _this;
41 }
42 return NodeError;
43 }(Base));
44}
45var g = typeof globalThis !== 'undefined' ? globalThis : global;
46var AssertionError = /** @class */ (function (_super) {
47 __extends(AssertionError, _super);
48 function AssertionError(options) {
49 var _this = this;
50 if (typeof options !== 'object' || options === null) {
51 throw new exports.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'object');
52 }
53 if (options.message) {
54 _this = _super.call(this, options.message) || this;
55 }
56 else {
57 _this = _super.call(this, "".concat(util.inspect(options.actual).slice(0, 128), " ") +
58 "".concat(options.operator, " ").concat(util.inspect(options.expected).slice(0, 128))) || this;
59 }
60 _this.generatedMessage = !options.message;
61 _this.name = 'AssertionError [ERR_ASSERTION]';
62 _this.code = 'ERR_ASSERTION';
63 _this.actual = options.actual;
64 _this.expected = options.expected;
65 _this.operator = options.operator;
66 exports.Error.captureStackTrace(_this, options.stackStartFunction);
67 return _this;
68 }
69 return AssertionError;
70}(g.Error));
71exports.AssertionError = AssertionError;
72function message(key, args) {
73 assert.strictEqual(typeof key, 'string');
74 // const msg = messages.get(key);
75 var msg = messages[key];
76 assert(msg, "An invalid error message key was used: ".concat(key, "."));
77 var fmt;
78 if (typeof msg === 'function') {
79 fmt = msg;
80 }
81 else {
82 fmt = util.format;
83 if (args === undefined || args.length === 0)
84 return msg;
85 args.unshift(msg);
86 }
87 return String(fmt.apply(null, args));
88}
89exports.message = message;
90// Utility function for registering the error codes. Only used here. Exported
91// *only* to allow for testing.
92function E(sym, val) {
93 messages[sym] = typeof val === 'function' ? val : String(val);
94}
95exports.E = E;
96exports.Error = makeNodeError(g.Error);
97exports.TypeError = makeNodeError(g.TypeError);
98exports.RangeError = makeNodeError(g.RangeError);
99// To declare an error message, use the E(sym, val) function above. The sym
100// must be an upper case string. The val can be either a function or a string.
101// The return value of the function must be a string.
102// Examples:
103// E('EXAMPLE_KEY1', 'This is the error value');
104// E('EXAMPLE_KEY2', (a, b) => return `${a} ${b}`);
105//
106// Once an error code has been assigned, the code itself MUST NOT change and
107// any given error code must never be reused to identify a different error.
108//
109// Any error code added here should also be added to the documentation
110//
111// Note: Please try to keep these in alphabetical order
112E('ERR_ARG_NOT_ITERABLE', '%s must be iterable');
113E('ERR_ASSERTION', '%s');
114E('ERR_BUFFER_OUT_OF_BOUNDS', bufferOutOfBounds);
115E('ERR_CHILD_CLOSED_BEFORE_REPLY', 'Child closed before reply received');
116E('ERR_CONSOLE_WRITABLE_STREAM', 'Console expects a writable stream instance for %s');
117E('ERR_CPU_USAGE', 'Unable to obtain cpu usage %s');
118E('ERR_DNS_SET_SERVERS_FAILED', function (err, servers) { return "c-ares failed to set servers: \"".concat(err, "\" [").concat(servers, "]"); });
119E('ERR_FALSY_VALUE_REJECTION', 'Promise was rejected with falsy value');
120E('ERR_ENCODING_NOT_SUPPORTED', function (enc) { return "The \"".concat(enc, "\" encoding is not supported"); });
121E('ERR_ENCODING_INVALID_ENCODED_DATA', function (enc) { return "The encoded data was not valid for encoding ".concat(enc); });
122E('ERR_HTTP_HEADERS_SENT', 'Cannot render headers after they are sent to the client');
123E('ERR_HTTP_INVALID_STATUS_CODE', 'Invalid status code: %s');
124E('ERR_HTTP_TRAILER_INVALID', 'Trailers are invalid with this transfer encoding');
125E('ERR_INDEX_OUT_OF_RANGE', 'Index out of range');
126E('ERR_INVALID_ARG_TYPE', invalidArgType);
127E('ERR_INVALID_ARRAY_LENGTH', function (name, len, actual) {
128 assert.strictEqual(typeof actual, 'number');
129 return "The array \"".concat(name, "\" (length ").concat(actual, ") must be of length ").concat(len, ".");
130});
131E('ERR_INVALID_BUFFER_SIZE', 'Buffer size must be a multiple of %s');
132E('ERR_INVALID_CALLBACK', 'Callback must be a function');
133E('ERR_INVALID_CHAR', 'Invalid character in %s');
134E('ERR_INVALID_CURSOR_POS', 'Cannot set cursor row without setting its column');
135E('ERR_INVALID_FD', '"fd" must be a positive integer: %s');
136E('ERR_INVALID_FILE_URL_HOST', 'File URL host must be "localhost" or empty on %s');
137E('ERR_INVALID_FILE_URL_PATH', 'File URL path %s');
138E('ERR_INVALID_HANDLE_TYPE', 'This handle type cannot be sent');
139E('ERR_INVALID_IP_ADDRESS', 'Invalid IP address: %s');
140E('ERR_INVALID_OPT_VALUE', function (name, value) {
141 return "The value \"".concat(String(value), "\" is invalid for option \"").concat(name, "\"");
142});
143E('ERR_INVALID_OPT_VALUE_ENCODING', function (value) { return "The value \"".concat(String(value), "\" is invalid for option \"encoding\""); });
144E('ERR_INVALID_REPL_EVAL_CONFIG', 'Cannot specify both "breakEvalOnSigint" and "eval" for REPL');
145E('ERR_INVALID_SYNC_FORK_INPUT', 'Asynchronous forks do not support Buffer, Uint8Array or string input: %s');
146E('ERR_INVALID_THIS', 'Value of "this" must be of type %s');
147E('ERR_INVALID_TUPLE', '%s must be an iterable %s tuple');
148E('ERR_INVALID_URL', 'Invalid URL: %s');
149E('ERR_INVALID_URL_SCHEME', function (expected) { return "The URL must be ".concat(oneOf(expected, 'scheme')); });
150E('ERR_IPC_CHANNEL_CLOSED', 'Channel closed');
151E('ERR_IPC_DISCONNECTED', 'IPC channel is already disconnected');
152E('ERR_IPC_ONE_PIPE', 'Child process can have only one IPC pipe');
153E('ERR_IPC_SYNC_FORK', 'IPC cannot be used with synchronous forks');
154E('ERR_MISSING_ARGS', missingArgs);
155E('ERR_MULTIPLE_CALLBACK', 'Callback called multiple times');
156E('ERR_NAPI_CONS_FUNCTION', 'Constructor must be a function');
157E('ERR_NAPI_CONS_PROTOTYPE_OBJECT', 'Constructor.prototype must be an object');
158E('ERR_NO_CRYPTO', 'Node.js is not compiled with OpenSSL crypto support');
159E('ERR_NO_LONGER_SUPPORTED', '%s is no longer supported');
160E('ERR_PARSE_HISTORY_DATA', 'Could not parse history data in %s');
161E('ERR_SOCKET_ALREADY_BOUND', 'Socket is already bound');
162E('ERR_SOCKET_BAD_PORT', 'Port should be > 0 and < 65536');
163E('ERR_SOCKET_BAD_TYPE', 'Bad socket type specified. Valid types are: udp4, udp6');
164E('ERR_SOCKET_CANNOT_SEND', 'Unable to send data');
165E('ERR_SOCKET_CLOSED', 'Socket is closed');
166E('ERR_SOCKET_DGRAM_NOT_RUNNING', 'Not running');
167E('ERR_STDERR_CLOSE', 'process.stderr cannot be closed');
168E('ERR_STDOUT_CLOSE', 'process.stdout cannot be closed');
169E('ERR_STREAM_WRAP', 'Stream has StringDecoder set or is in objectMode');
170E('ERR_TLS_CERT_ALTNAME_INVALID', "Hostname/IP does not match certificate's altnames: %s");
171E('ERR_TLS_DH_PARAM_SIZE', function (size) { return "DH parameter size ".concat(size, " is less than 2048"); });
172E('ERR_TLS_HANDSHAKE_TIMEOUT', 'TLS handshake timeout');
173E('ERR_TLS_RENEGOTIATION_FAILED', 'Failed to renegotiate');
174E('ERR_TLS_REQUIRED_SERVER_NAME', '"servername" is required parameter for Server.addContext');
175E('ERR_TLS_SESSION_ATTACK', 'TSL session renegotiation attack detected');
176E('ERR_TRANSFORM_ALREADY_TRANSFORMING', 'Calling transform done when still transforming');
177E('ERR_TRANSFORM_WITH_LENGTH_0', 'Calling transform done when writableState.length != 0');
178E('ERR_UNKNOWN_ENCODING', 'Unknown encoding: %s');
179E('ERR_UNKNOWN_SIGNAL', 'Unknown signal: %s');
180E('ERR_UNKNOWN_STDIN_TYPE', 'Unknown stdin file type');
181E('ERR_UNKNOWN_STREAM_TYPE', 'Unknown stream file type');
182E('ERR_V8BREAKITERATOR', 'Full ICU data not installed. ' + 'See https://github.com/nodejs/node/wiki/Intl');
183function invalidArgType(name, expected, actual) {
184 assert(name, 'name is required');
185 // determiner: 'must be' or 'must not be'
186 var determiner;
187 if (expected.includes('not ')) {
188 determiner = 'must not be';
189 expected = expected.split('not ')[1];
190 }
191 else {
192 determiner = 'must be';
193 }
194 var msg;
195 if (Array.isArray(name)) {
196 var names = name.map(function (val) { return "\"".concat(val, "\""); }).join(', ');
197 msg = "The ".concat(names, " arguments ").concat(determiner, " ").concat(oneOf(expected, 'type'));
198 }
199 else if (name.includes(' argument')) {
200 // for the case like 'first argument'
201 msg = "The ".concat(name, " ").concat(determiner, " ").concat(oneOf(expected, 'type'));
202 }
203 else {
204 var type = name.includes('.') ? 'property' : 'argument';
205 msg = "The \"".concat(name, "\" ").concat(type, " ").concat(determiner, " ").concat(oneOf(expected, 'type'));
206 }
207 // if actual value received, output it
208 if (arguments.length >= 3) {
209 msg += ". Received type ".concat(actual !== null ? typeof actual : 'null');
210 }
211 return msg;
212}
213function missingArgs() {
214 var args = [];
215 for (var _i = 0; _i < arguments.length; _i++) {
216 args[_i] = arguments[_i];
217 }
218 assert(args.length > 0, 'At least one arg needs to be specified');
219 var msg = 'The ';
220 var len = args.length;
221 args = args.map(function (a) { return "\"".concat(a, "\""); });
222 switch (len) {
223 case 1:
224 msg += "".concat(args[0], " argument");
225 break;
226 case 2:
227 msg += "".concat(args[0], " and ").concat(args[1], " arguments");
228 break;
229 default:
230 msg += args.slice(0, len - 1).join(', ');
231 msg += ", and ".concat(args[len - 1], " arguments");
232 break;
233 }
234 return "".concat(msg, " must be specified");
235}
236function oneOf(expected, thing) {
237 assert(expected, 'expected is required');
238 assert(typeof thing === 'string', 'thing is required');
239 if (Array.isArray(expected)) {
240 var len = expected.length;
241 assert(len > 0, 'At least one expected value needs to be specified');
242 // tslint:disable-next-line
243 expected = expected.map(function (i) { return String(i); });
244 if (len > 2) {
245 return "one of ".concat(thing, " ").concat(expected.slice(0, len - 1).join(', '), ", or ") + expected[len - 1];
246 }
247 else if (len === 2) {
248 return "one of ".concat(thing, " ").concat(expected[0], " or ").concat(expected[1]);
249 }
250 else {
251 return "of ".concat(thing, " ").concat(expected[0]);
252 }
253 }
254 else {
255 return "of ".concat(thing, " ").concat(String(expected));
256 }
257}
258function bufferOutOfBounds(name, isWriting) {
259 if (isWriting) {
260 return 'Attempt to write outside buffer bounds';
261 }
262 else {
263 return "\"".concat(name, "\" is outside of buffer bounds");
264 }
265}