errors.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * Base class for errors. In addition to a `code` and a human-friendly
  3. * `message`, may also includes the token `stem` as well as other fields.
  4. *
  5. * @public
  6. */
  7. export class NumberFormatError extends Error {
  8. /** @internal */
  9. constructor(code, msg) {
  10. super(msg);
  11. this.code = code;
  12. }
  13. }
  14. /** @internal */
  15. export class BadOptionError extends NumberFormatError {
  16. constructor(stem, opt) {
  17. super('BAD_OPTION', `Unknown ${stem} option: ${opt}`);
  18. this.stem = stem;
  19. this.option = opt;
  20. }
  21. }
  22. /** @internal */
  23. export class BadStemError extends NumberFormatError {
  24. constructor(stem) {
  25. super('BAD_STEM', `Unknown stem: ${stem}`);
  26. this.stem = stem;
  27. }
  28. }
  29. /** @internal */
  30. export class MaskedValueError extends NumberFormatError {
  31. constructor(type, prev) {
  32. super('MASKED_VALUE', `Value for ${type} is set multiple times`);
  33. this.type = type;
  34. this.prev = prev;
  35. }
  36. }
  37. /** @internal */
  38. export class MissingOptionError extends NumberFormatError {
  39. constructor(stem) {
  40. super('MISSING_OPTION', `Required option missing for ${stem}`);
  41. this.stem = stem;
  42. }
  43. }
  44. /** @internal */
  45. export class PatternError extends NumberFormatError {
  46. constructor(char, msg) {
  47. super('BAD_PATTERN', msg);
  48. this.char = char;
  49. }
  50. }
  51. /** @internal */
  52. export class TooManyOptionsError extends NumberFormatError {
  53. constructor(stem, options, maxOpt) {
  54. const maxOptStr = maxOpt > 1 ? `${maxOpt} options` : 'one option';
  55. super('TOO_MANY_OPTIONS', `Token ${stem} only supports ${maxOptStr} (got ${options.length})`);
  56. this.stem = stem;
  57. this.options = options;
  58. }
  59. }
  60. /** @internal */
  61. export class UnsupportedError extends NumberFormatError {
  62. constructor(stem, source) {
  63. super('UNSUPPORTED', `The stem ${stem} is not supported`);
  64. this.stem = stem;
  65. if (source) {
  66. this.message += ` with value ${source}`;
  67. this.source = source;
  68. }
  69. }
  70. }