errors.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. 'use strict';
  2. /* istanbul ignore file: https://github.com/nodejs/node/blob/master/lib/internal/errors.js */
  3. const makeError = (Base, key, getMessage) => {
  4. module.exports[key] = class NodeError extends Base {
  5. constructor(...args) {
  6. super(typeof getMessage === 'string' ? getMessage : getMessage(args));
  7. this.name = `${super.name} [${key}]`;
  8. this.code = key;
  9. }
  10. };
  11. };
  12. makeError(TypeError, 'ERR_INVALID_ARG_TYPE', args => {
  13. const type = args[0].includes('.') ? 'property' : 'argument';
  14. let valid = args[1];
  15. const isManyTypes = Array.isArray(valid);
  16. if (isManyTypes) {
  17. valid = `${valid.slice(0, -1).join(', ')} or ${valid.slice(-1)}`;
  18. }
  19. return `The "${args[0]}" ${type} must be ${isManyTypes ? 'one of' : 'of'} type ${valid}. Received ${typeof args[2]}`;
  20. });
  21. makeError(TypeError, 'ERR_INVALID_PROTOCOL', args =>
  22. `Protocol "${args[0]}" not supported. Expected "${args[1]}"`
  23. );
  24. makeError(Error, 'ERR_HTTP_HEADERS_SENT', args =>
  25. `Cannot ${args[0]} headers after they are sent to the client`
  26. );
  27. makeError(TypeError, 'ERR_INVALID_HTTP_TOKEN', args =>
  28. `${args[0]} must be a valid HTTP token [${args[1]}]`
  29. );
  30. makeError(TypeError, 'ERR_HTTP_INVALID_HEADER_VALUE', args =>
  31. `Invalid value "${args[0]} for header "${args[1]}"`
  32. );
  33. makeError(TypeError, 'ERR_INVALID_CHAR', args =>
  34. `Invalid character in ${args[0]} [${args[1]}]`
  35. );
  36. makeError(
  37. Error,
  38. 'ERR_HTTP2_NO_SOCKET_MANIPULATION',
  39. 'HTTP/2 sockets should not be directly manipulated (e.g. read and written)'
  40. );