UNPKG

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