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 3.6KB

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