error.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // @ts-check
  2. /**
  3. * CommanderError class
  4. * @class
  5. */
  6. class CommanderError extends Error {
  7. /**
  8. * Constructs the CommanderError class
  9. * @param {number} exitCode suggested exit code which could be used with process.exit
  10. * @param {string} code an id string representing the error
  11. * @param {string} message human-readable description of the error
  12. * @constructor
  13. */
  14. constructor(exitCode, code, message) {
  15. super(message);
  16. // properly capture stack trace in Node.js
  17. Error.captureStackTrace(this, this.constructor);
  18. this.name = this.constructor.name;
  19. this.code = code;
  20. this.exitCode = exitCode;
  21. this.nestedError = undefined;
  22. }
  23. }
  24. /**
  25. * InvalidArgumentError class
  26. * @class
  27. */
  28. class InvalidArgumentError extends CommanderError {
  29. /**
  30. * Constructs the InvalidArgumentError class
  31. * @param {string} [message] explanation of why argument is invalid
  32. * @constructor
  33. */
  34. constructor(message) {
  35. super(1, 'commander.invalidArgument', message);
  36. // properly capture stack trace in Node.js
  37. Error.captureStackTrace(this, this.constructor);
  38. this.name = this.constructor.name;
  39. }
  40. }
  41. exports.CommanderError = CommanderError;
  42. exports.InvalidArgumentError = InvalidArgumentError;