errors.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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.
  7. var __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. })();
  22. Object.defineProperty(exports, "__esModule", { value: true });
  23. exports.E = exports.AssertionError = exports.message = exports.RangeError = exports.TypeError = exports.Error = void 0;
  24. var assert = require("assert");
  25. var util = require("util");
  26. var kCode = typeof Symbol === 'undefined' ? '_kCode' : Symbol('code');
  27. var messages = {}; // new Map();
  28. function 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. }
  45. var g = typeof globalThis !== 'undefined' ? globalThis : global;
  46. var 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));
  71. exports.AssertionError = AssertionError;
  72. function 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. }
  89. exports.message = message;
  90. // Utility function for registering the error codes. Only used here. Exported
  91. // *only* to allow for testing.
  92. function E(sym, val) {
  93. messages[sym] = typeof val === 'function' ? val : String(val);
  94. }
  95. exports.E = E;
  96. exports.Error = makeNodeError(g.Error);
  97. exports.TypeError = makeNodeError(g.TypeError);
  98. exports.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
  112. E('ERR_ARG_NOT_ITERABLE', '%s must be iterable');
  113. E('ERR_ASSERTION', '%s');
  114. E('ERR_BUFFER_OUT_OF_BOUNDS', bufferOutOfBounds);
  115. E('ERR_CHILD_CLOSED_BEFORE_REPLY', 'Child closed before reply received');
  116. E('ERR_CONSOLE_WRITABLE_STREAM', 'Console expects a writable stream instance for %s');
  117. E('ERR_CPU_USAGE', 'Unable to obtain cpu usage %s');
  118. E('ERR_DNS_SET_SERVERS_FAILED', function (err, servers) { return "c-ares failed to set servers: \"".concat(err, "\" [").concat(servers, "]"); });
  119. E('ERR_FALSY_VALUE_REJECTION', 'Promise was rejected with falsy value');
  120. E('ERR_ENCODING_NOT_SUPPORTED', function (enc) { return "The \"".concat(enc, "\" encoding is not supported"); });
  121. E('ERR_ENCODING_INVALID_ENCODED_DATA', function (enc) { return "The encoded data was not valid for encoding ".concat(enc); });
  122. E('ERR_HTTP_HEADERS_SENT', 'Cannot render headers after they are sent to the client');
  123. E('ERR_HTTP_INVALID_STATUS_CODE', 'Invalid status code: %s');
  124. E('ERR_HTTP_TRAILER_INVALID', 'Trailers are invalid with this transfer encoding');
  125. E('ERR_INDEX_OUT_OF_RANGE', 'Index out of range');
  126. E('ERR_INVALID_ARG_TYPE', invalidArgType);
  127. E('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. });
  131. E('ERR_INVALID_BUFFER_SIZE', 'Buffer size must be a multiple of %s');
  132. E('ERR_INVALID_CALLBACK', 'Callback must be a function');
  133. E('ERR_INVALID_CHAR', 'Invalid character in %s');
  134. E('ERR_INVALID_CURSOR_POS', 'Cannot set cursor row without setting its column');
  135. E('ERR_INVALID_FD', '"fd" must be a positive integer: %s');
  136. E('ERR_INVALID_FILE_URL_HOST', 'File URL host must be "localhost" or empty on %s');
  137. E('ERR_INVALID_FILE_URL_PATH', 'File URL path %s');
  138. E('ERR_INVALID_HANDLE_TYPE', 'This handle type cannot be sent');
  139. E('ERR_INVALID_IP_ADDRESS', 'Invalid IP address: %s');
  140. E('ERR_INVALID_OPT_VALUE', function (name, value) {
  141. return "The value \"".concat(String(value), "\" is invalid for option \"").concat(name, "\"");
  142. });
  143. E('ERR_INVALID_OPT_VALUE_ENCODING', function (value) { return "The value \"".concat(String(value), "\" is invalid for option \"encoding\""); });
  144. E('ERR_INVALID_REPL_EVAL_CONFIG', 'Cannot specify both "breakEvalOnSigint" and "eval" for REPL');
  145. E('ERR_INVALID_SYNC_FORK_INPUT', 'Asynchronous forks do not support Buffer, Uint8Array or string input: %s');
  146. E('ERR_INVALID_THIS', 'Value of "this" must be of type %s');
  147. E('ERR_INVALID_TUPLE', '%s must be an iterable %s tuple');
  148. E('ERR_INVALID_URL', 'Invalid URL: %s');
  149. E('ERR_INVALID_URL_SCHEME', function (expected) { return "The URL must be ".concat(oneOf(expected, 'scheme')); });
  150. E('ERR_IPC_CHANNEL_CLOSED', 'Channel closed');
  151. E('ERR_IPC_DISCONNECTED', 'IPC channel is already disconnected');
  152. E('ERR_IPC_ONE_PIPE', 'Child process can have only one IPC pipe');
  153. E('ERR_IPC_SYNC_FORK', 'IPC cannot be used with synchronous forks');
  154. E('ERR_MISSING_ARGS', missingArgs);
  155. E('ERR_MULTIPLE_CALLBACK', 'Callback called multiple times');
  156. E('ERR_NAPI_CONS_FUNCTION', 'Constructor must be a function');
  157. E('ERR_NAPI_CONS_PROTOTYPE_OBJECT', 'Constructor.prototype must be an object');
  158. E('ERR_NO_CRYPTO', 'Node.js is not compiled with OpenSSL crypto support');
  159. E('ERR_NO_LONGER_SUPPORTED', '%s is no longer supported');
  160. E('ERR_PARSE_HISTORY_DATA', 'Could not parse history data in %s');
  161. E('ERR_SOCKET_ALREADY_BOUND', 'Socket is already bound');
  162. E('ERR_SOCKET_BAD_PORT', 'Port should be > 0 and < 65536');
  163. E('ERR_SOCKET_BAD_TYPE', 'Bad socket type specified. Valid types are: udp4, udp6');
  164. E('ERR_SOCKET_CANNOT_SEND', 'Unable to send data');
  165. E('ERR_SOCKET_CLOSED', 'Socket is closed');
  166. E('ERR_SOCKET_DGRAM_NOT_RUNNING', 'Not running');
  167. E('ERR_STDERR_CLOSE', 'process.stderr cannot be closed');
  168. E('ERR_STDOUT_CLOSE', 'process.stdout cannot be closed');
  169. E('ERR_STREAM_WRAP', 'Stream has StringDecoder set or is in objectMode');
  170. E('ERR_TLS_CERT_ALTNAME_INVALID', "Hostname/IP does not match certificate's altnames: %s");
  171. E('ERR_TLS_DH_PARAM_SIZE', function (size) { return "DH parameter size ".concat(size, " is less than 2048"); });
  172. E('ERR_TLS_HANDSHAKE_TIMEOUT', 'TLS handshake timeout');
  173. E('ERR_TLS_RENEGOTIATION_FAILED', 'Failed to renegotiate');
  174. E('ERR_TLS_REQUIRED_SERVER_NAME', '"servername" is required parameter for Server.addContext');
  175. E('ERR_TLS_SESSION_ATTACK', 'TSL session renegotiation attack detected');
  176. E('ERR_TRANSFORM_ALREADY_TRANSFORMING', 'Calling transform done when still transforming');
  177. E('ERR_TRANSFORM_WITH_LENGTH_0', 'Calling transform done when writableState.length != 0');
  178. E('ERR_UNKNOWN_ENCODING', 'Unknown encoding: %s');
  179. E('ERR_UNKNOWN_SIGNAL', 'Unknown signal: %s');
  180. E('ERR_UNKNOWN_STDIN_TYPE', 'Unknown stdin file type');
  181. E('ERR_UNKNOWN_STREAM_TYPE', 'Unknown stream file type');
  182. E('ERR_V8BREAKITERATOR', 'Full ICU data not installed. ' + 'See https://github.com/nodejs/node/wiki/Intl');
  183. function 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. }
  213. function 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. }
  236. function 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. }
  258. function 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. }