Path: blob/master/node_modules/assert/build/internal/errors.js
1126 views
// Currently in sync with Node.js lib/internal/errors.js1// https://github.com/nodejs/node/commit/3b044962c48fe313905877a96b5d0894a5404f6f23/* eslint node-core/documented-errors: "error" */45/* eslint node-core/alphabetize-errors: "error" */67/* eslint node-core/prefer-util-format-errors: "error" */8'use strict'; // The whole point behind this internal module is to allow Node.js to no9// longer be forced to treat every error message change as a semver-major10// change. The NodeError classes here all expose a `code` property whose11// value statically and permanently identifies the error. While the error12// message may change, the code should not.1314function _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); }1516function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }1718function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }1920function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }2122function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }2324function _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); }2526function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }2728var codes = {}; // Lazy loaded2930var assert;31var util;3233function createErrorType(code, message, Base) {34if (!Base) {35Base = Error;36}3738function getMessage(arg1, arg2, arg3) {39if (typeof message === 'string') {40return message;41} else {42return message(arg1, arg2, arg3);43}44}4546var NodeError =47/*#__PURE__*/48function (_Base) {49_inherits(NodeError, _Base);5051function NodeError(arg1, arg2, arg3) {52var _this;5354_classCallCheck(this, NodeError);5556_this = _possibleConstructorReturn(this, _getPrototypeOf(NodeError).call(this, getMessage(arg1, arg2, arg3)));57_this.code = code;58return _this;59}6061return NodeError;62}(Base);6364codes[code] = NodeError;65} // https://github.com/nodejs/node/blob/v10.8.0/lib/internal/errors.js666768function oneOf(expected, thing) {69if (Array.isArray(expected)) {70var len = expected.length;71expected = expected.map(function (i) {72return String(i);73});7475if (len > 2) {76return "one of ".concat(thing, " ").concat(expected.slice(0, len - 1).join(', '), ", or ") + expected[len - 1];77} else if (len === 2) {78return "one of ".concat(thing, " ").concat(expected[0], " or ").concat(expected[1]);79} else {80return "of ".concat(thing, " ").concat(expected[0]);81}82} else {83return "of ".concat(thing, " ").concat(String(expected));84}85} // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith868788function startsWith(str, search, pos) {89return str.substr(!pos || pos < 0 ? 0 : +pos, search.length) === search;90} // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith919293function endsWith(str, search, this_len) {94if (this_len === undefined || this_len > str.length) {95this_len = str.length;96}9798return str.substring(this_len - search.length, this_len) === search;99} // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes100101102function includes(str, search, start) {103if (typeof start !== 'number') {104start = 0;105}106107if (start + search.length > str.length) {108return false;109} else {110return str.indexOf(search, start) !== -1;111}112}113114createErrorType('ERR_AMBIGUOUS_ARGUMENT', 'The "%s" argument is ambiguous. %s', TypeError);115createErrorType('ERR_INVALID_ARG_TYPE', function (name, expected, actual) {116if (assert === undefined) assert = require('../assert');117assert(typeof name === 'string', "'name' must be a string"); // determiner: 'must be' or 'must not be'118119var determiner;120121if (typeof expected === 'string' && startsWith(expected, 'not ')) {122determiner = 'must not be';123expected = expected.replace(/^not /, '');124} else {125determiner = 'must be';126}127128var msg;129130if (endsWith(name, ' argument')) {131// For cases like 'first argument'132msg = "The ".concat(name, " ").concat(determiner, " ").concat(oneOf(expected, 'type'));133} else {134var type = includes(name, '.') ? 'property' : 'argument';135msg = "The \"".concat(name, "\" ").concat(type, " ").concat(determiner, " ").concat(oneOf(expected, 'type'));136} // TODO(BridgeAR): Improve the output by showing `null` and similar.137138139msg += ". Received type ".concat(_typeof(actual));140return msg;141}, TypeError);142createErrorType('ERR_INVALID_ARG_VALUE', function (name, value) {143var reason = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'is invalid';144if (util === undefined) util = require('util/');145var inspected = util.inspect(value);146147if (inspected.length > 128) {148inspected = "".concat(inspected.slice(0, 128), "...");149}150151return "The argument '".concat(name, "' ").concat(reason, ". Received ").concat(inspected);152}, TypeError, RangeError);153createErrorType('ERR_INVALID_RETURN_VALUE', function (input, name, value) {154var type;155156if (value && value.constructor && value.constructor.name) {157type = "instance of ".concat(value.constructor.name);158} else {159type = "type ".concat(_typeof(value));160}161162return "Expected ".concat(input, " to be returned from the \"").concat(name, "\"") + " function but got ".concat(type, ".");163}, TypeError);164createErrorType('ERR_MISSING_ARGS', function () {165for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {166args[_key] = arguments[_key];167}168169if (assert === undefined) assert = require('../assert');170assert(args.length > 0, 'At least one arg needs to be specified');171var msg = 'The ';172var len = args.length;173args = args.map(function (a) {174return "\"".concat(a, "\"");175});176177switch (len) {178case 1:179msg += "".concat(args[0], " argument");180break;181182case 2:183msg += "".concat(args[0], " and ").concat(args[1], " arguments");184break;185186default:187msg += args.slice(0, len - 1).join(', ');188msg += ", and ".concat(args[len - 1], " arguments");189break;190}191192return "".concat(msg, " must be specified");193}, TypeError);194module.exports.codes = codes;195196