errors.js 9.1 KB

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