Spaces:
Sleeping
Sleeping
| // Currently in sync with Node.js lib/internal/errors.js | |
| // https://github.com/nodejs/node/commit/3b044962c48fe313905877a96b5d0894a5404f6f | |
| /* eslint node-core/documented-errors: "error" */ | |
| /* eslint node-core/alphabetize-errors: "error" */ | |
| /* eslint node-core/prefer-util-format-errors: "error" */ | |
| ; | |
| // The whole point behind this internal module is to allow Node.js to no | |
| // longer be forced to treat every error message change as a semver-major | |
| // change. The NodeError classes here all expose a `code` property whose | |
| // value statically and permanently identifies the error. While the error | |
| // message may change, the code should not. | |
| 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); } | |
| 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); } } | |
| function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } | |
| function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); } | |
| 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); } | |
| function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | |
| 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); } | |
| function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } | |
| 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); }; } | |
| 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); } | |
| function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | |
| 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; } } | |
| function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } | |
| var codes = {}; | |
| // Lazy loaded | |
| var assert; | |
| var util; | |
| function createErrorType(code, message, Base) { | |
| if (!Base) { | |
| Base = Error; | |
| } | |
| function getMessage(arg1, arg2, arg3) { | |
| if (typeof message === 'string') { | |
| return message; | |
| } else { | |
| return message(arg1, arg2, arg3); | |
| } | |
| } | |
| var NodeError = /*#__PURE__*/function (_Base) { | |
| _inherits(NodeError, _Base); | |
| var _super = _createSuper(NodeError); | |
| function NodeError(arg1, arg2, arg3) { | |
| var _this; | |
| _classCallCheck(this, NodeError); | |
| _this = _super.call(this, getMessage(arg1, arg2, arg3)); | |
| _this.code = code; | |
| return _this; | |
| } | |
| return _createClass(NodeError); | |
| }(Base); | |
| codes[code] = NodeError; | |
| } | |
| // https://github.com/nodejs/node/blob/v10.8.0/lib/internal/errors.js | |
| function oneOf(expected, thing) { | |
| if (Array.isArray(expected)) { | |
| var len = expected.length; | |
| expected = expected.map(function (i) { | |
| return String(i); | |
| }); | |
| if (len > 2) { | |
| return "one of ".concat(thing, " ").concat(expected.slice(0, len - 1).join(', '), ", or ") + expected[len - 1]; | |
| } else if (len === 2) { | |
| return "one of ".concat(thing, " ").concat(expected[0], " or ").concat(expected[1]); | |
| } else { | |
| return "of ".concat(thing, " ").concat(expected[0]); | |
| } | |
| } else { | |
| return "of ".concat(thing, " ").concat(String(expected)); | |
| } | |
| } | |
| // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith | |
| function startsWith(str, search, pos) { | |
| return str.substr(!pos || pos < 0 ? 0 : +pos, search.length) === search; | |
| } | |
| // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith | |
| function endsWith(str, search, this_len) { | |
| if (this_len === undefined || this_len > str.length) { | |
| this_len = str.length; | |
| } | |
| return str.substring(this_len - search.length, this_len) === search; | |
| } | |
| // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes | |
| function includes(str, search, start) { | |
| if (typeof start !== 'number') { | |
| start = 0; | |
| } | |
| if (start + search.length > str.length) { | |
| return false; | |
| } else { | |
| return str.indexOf(search, start) !== -1; | |
| } | |
| } | |
| createErrorType('ERR_AMBIGUOUS_ARGUMENT', 'The "%s" argument is ambiguous. %s', TypeError); | |
| createErrorType('ERR_INVALID_ARG_TYPE', function (name, expected, actual) { | |
| if (assert === undefined) assert = require('../assert'); | |
| assert(typeof name === 'string', "'name' must be a string"); | |
| // determiner: 'must be' or 'must not be' | |
| var determiner; | |
| if (typeof expected === 'string' && startsWith(expected, 'not ')) { | |
| determiner = 'must not be'; | |
| expected = expected.replace(/^not /, ''); | |
| } else { | |
| determiner = 'must be'; | |
| } | |
| var msg; | |
| if (endsWith(name, ' argument')) { | |
| // For cases like 'first argument' | |
| msg = "The ".concat(name, " ").concat(determiner, " ").concat(oneOf(expected, 'type')); | |
| } else { | |
| var type = includes(name, '.') ? 'property' : 'argument'; | |
| msg = "The \"".concat(name, "\" ").concat(type, " ").concat(determiner, " ").concat(oneOf(expected, 'type')); | |
| } | |
| // TODO(BridgeAR): Improve the output by showing `null` and similar. | |
| msg += ". Received type ".concat(_typeof(actual)); | |
| return msg; | |
| }, TypeError); | |
| createErrorType('ERR_INVALID_ARG_VALUE', function (name, value) { | |
| var reason = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'is invalid'; | |
| if (util === undefined) util = require('util/'); | |
| var inspected = util.inspect(value); | |
| if (inspected.length > 128) { | |
| inspected = "".concat(inspected.slice(0, 128), "..."); | |
| } | |
| return "The argument '".concat(name, "' ").concat(reason, ". Received ").concat(inspected); | |
| }, TypeError, RangeError); | |
| createErrorType('ERR_INVALID_RETURN_VALUE', function (input, name, value) { | |
| var type; | |
| if (value && value.constructor && value.constructor.name) { | |
| type = "instance of ".concat(value.constructor.name); | |
| } else { | |
| type = "type ".concat(_typeof(value)); | |
| } | |
| return "Expected ".concat(input, " to be returned from the \"").concat(name, "\"") + " function but got ".concat(type, "."); | |
| }, TypeError); | |
| createErrorType('ERR_MISSING_ARGS', function () { | |
| for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | |
| args[_key] = arguments[_key]; | |
| } | |
| if (assert === undefined) assert = require('../assert'); | |
| assert(args.length > 0, 'At least one arg needs to be specified'); | |
| var msg = 'The '; | |
| var len = args.length; | |
| args = args.map(function (a) { | |
| return "\"".concat(a, "\""); | |
| }); | |
| switch (len) { | |
| case 1: | |
| msg += "".concat(args[0], " argument"); | |
| break; | |
| case 2: | |
| msg += "".concat(args[0], " and ").concat(args[1], " arguments"); | |
| break; | |
| default: | |
| msg += args.slice(0, len - 1).join(', '); | |
| msg += ", and ".concat(args[len - 1], " arguments"); | |
| break; | |
| } | |
| return "".concat(msg, " must be specified"); | |
| }, TypeError); | |
| module.exports.codes = codes; |