You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

errors.js 7.1KB

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