errors-browser.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. 'use strict';
  2. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  3. var codes = {};
  4. function createErrorType(code, message, Base) {
  5. if (!Base) {
  6. Base = Error;
  7. }
  8. function getMessage(arg1, arg2, arg3) {
  9. if (typeof message === 'string') {
  10. return message;
  11. } else {
  12. return message(arg1, arg2, arg3);
  13. }
  14. }
  15. var NodeError =
  16. /*#__PURE__*/
  17. function (_Base) {
  18. _inheritsLoose(NodeError, _Base);
  19. function NodeError(arg1, arg2, arg3) {
  20. return _Base.call(this, getMessage(arg1, arg2, arg3)) || this;
  21. }
  22. return NodeError;
  23. }(Base);
  24. NodeError.prototype.name = Base.name;
  25. NodeError.prototype.code = code;
  26. codes[code] = NodeError;
  27. } // https://github.com/nodejs/node/blob/v10.8.0/lib/internal/errors.js
  28. function oneOf(expected, thing) {
  29. if (Array.isArray(expected)) {
  30. var len = expected.length;
  31. expected = expected.map(function (i) {
  32. return String(i);
  33. });
  34. if (len > 2) {
  35. return "one of ".concat(thing, " ").concat(expected.slice(0, len - 1).join(', '), ", or ") + expected[len - 1];
  36. } else if (len === 2) {
  37. return "one of ".concat(thing, " ").concat(expected[0], " or ").concat(expected[1]);
  38. } else {
  39. return "of ".concat(thing, " ").concat(expected[0]);
  40. }
  41. } else {
  42. return "of ".concat(thing, " ").concat(String(expected));
  43. }
  44. } // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith
  45. function startsWith(str, search, pos) {
  46. return str.substr(!pos || pos < 0 ? 0 : +pos, search.length) === search;
  47. } // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith
  48. function endsWith(str, search, this_len) {
  49. if (this_len === undefined || this_len > str.length) {
  50. this_len = str.length;
  51. }
  52. return str.substring(this_len - search.length, this_len) === search;
  53. } // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes
  54. function includes(str, search, start) {
  55. if (typeof start !== 'number') {
  56. start = 0;
  57. }
  58. if (start + search.length > str.length) {
  59. return false;
  60. } else {
  61. return str.indexOf(search, start) !== -1;
  62. }
  63. }
  64. createErrorType('ERR_INVALID_OPT_VALUE', function (name, value) {
  65. return 'The value "' + value + '" is invalid for option "' + name + '"';
  66. }, TypeError);
  67. createErrorType('ERR_INVALID_ARG_TYPE', function (name, expected, actual) {
  68. // determiner: 'must be' or 'must not be'
  69. var determiner;
  70. if (typeof expected === 'string' && startsWith(expected, 'not ')) {
  71. determiner = 'must not be';
  72. expected = expected.replace(/^not /, '');
  73. } else {
  74. determiner = 'must be';
  75. }
  76. var msg;
  77. if (endsWith(name, ' argument')) {
  78. // For cases like 'first argument'
  79. msg = "The ".concat(name, " ").concat(determiner, " ").concat(oneOf(expected, 'type'));
  80. } else {
  81. var type = includes(name, '.') ? 'property' : 'argument';
  82. msg = "The \"".concat(name, "\" ").concat(type, " ").concat(determiner, " ").concat(oneOf(expected, 'type'));
  83. }
  84. msg += ". Received type ".concat(typeof actual);
  85. return msg;
  86. }, TypeError);
  87. createErrorType('ERR_STREAM_PUSH_AFTER_EOF', 'stream.push() after EOF');
  88. createErrorType('ERR_METHOD_NOT_IMPLEMENTED', function (name) {
  89. return 'The ' + name + ' method is not implemented';
  90. });
  91. createErrorType('ERR_STREAM_PREMATURE_CLOSE', 'Premature close');
  92. createErrorType('ERR_STREAM_DESTROYED', function (name) {
  93. return 'Cannot call ' + name + ' after a stream was destroyed';
  94. });
  95. createErrorType('ERR_MULTIPLE_CALLBACK', 'Callback called multiple times');
  96. createErrorType('ERR_STREAM_CANNOT_PIPE', 'Cannot pipe, not readable');
  97. createErrorType('ERR_STREAM_WRITE_AFTER_END', 'write after end');
  98. createErrorType('ERR_STREAM_NULL_VALUES', 'May not write null values to stream', TypeError);
  99. createErrorType('ERR_UNKNOWN_ENCODING', function (arg) {
  100. return 'Unknown encoding: ' + arg;
  101. }, TypeError);
  102. createErrorType('ERR_STREAM_UNSHIFT_AFTER_END_EVENT', 'stream.unshift() after end event');
  103. module.exports.codes = codes;