exceptions.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. from __future__ import annotations
  2. from typing import Collection
  3. class TOMLKitError(Exception):
  4. pass
  5. class ParseError(ValueError, TOMLKitError):
  6. """
  7. This error occurs when the parser encounters a syntax error
  8. in the TOML being parsed. The error references the line and
  9. location within the line where the error was encountered.
  10. """
  11. def __init__(self, line: int, col: int, message: str | None = None) -> None:
  12. self._line = line
  13. self._col = col
  14. if message is None:
  15. message = "TOML parse error"
  16. super().__init__(f"{message} at line {self._line} col {self._col}")
  17. @property
  18. def line(self):
  19. return self._line
  20. @property
  21. def col(self):
  22. return self._col
  23. class MixedArrayTypesError(ParseError):
  24. """
  25. An array was found that had two or more element types.
  26. """
  27. def __init__(self, line: int, col: int) -> None:
  28. message = "Mixed types found in array"
  29. super().__init__(line, col, message=message)
  30. class InvalidNumberError(ParseError):
  31. """
  32. A numeric field was improperly specified.
  33. """
  34. def __init__(self, line: int, col: int) -> None:
  35. message = "Invalid number"
  36. super().__init__(line, col, message=message)
  37. class InvalidDateTimeError(ParseError):
  38. """
  39. A datetime field was improperly specified.
  40. """
  41. def __init__(self, line: int, col: int) -> None:
  42. message = "Invalid datetime"
  43. super().__init__(line, col, message=message)
  44. class InvalidDateError(ParseError):
  45. """
  46. A date field was improperly specified.
  47. """
  48. def __init__(self, line: int, col: int) -> None:
  49. message = "Invalid date"
  50. super().__init__(line, col, message=message)
  51. class InvalidTimeError(ParseError):
  52. """
  53. A date field was improperly specified.
  54. """
  55. def __init__(self, line: int, col: int) -> None:
  56. message = "Invalid time"
  57. super().__init__(line, col, message=message)
  58. class InvalidNumberOrDateError(ParseError):
  59. """
  60. A numeric or date field was improperly specified.
  61. """
  62. def __init__(self, line: int, col: int) -> None:
  63. message = "Invalid number or date format"
  64. super().__init__(line, col, message=message)
  65. class InvalidUnicodeValueError(ParseError):
  66. """
  67. A unicode code was improperly specified.
  68. """
  69. def __init__(self, line: int, col: int) -> None:
  70. message = "Invalid unicode value"
  71. super().__init__(line, col, message=message)
  72. class UnexpectedCharError(ParseError):
  73. """
  74. An unexpected character was found during parsing.
  75. """
  76. def __init__(self, line: int, col: int, char: str) -> None:
  77. message = f"Unexpected character: {repr(char)}"
  78. super().__init__(line, col, message=message)
  79. class EmptyKeyError(ParseError):
  80. """
  81. An empty key was found during parsing.
  82. """
  83. def __init__(self, line: int, col: int) -> None:
  84. message = "Empty key"
  85. super().__init__(line, col, message=message)
  86. class EmptyTableNameError(ParseError):
  87. """
  88. An empty table name was found during parsing.
  89. """
  90. def __init__(self, line: int, col: int) -> None:
  91. message = "Empty table name"
  92. super().__init__(line, col, message=message)
  93. class InvalidCharInStringError(ParseError):
  94. """
  95. The string being parsed contains an invalid character.
  96. """
  97. def __init__(self, line: int, col: int, char: str) -> None:
  98. message = f"Invalid character {repr(char)} in string"
  99. super().__init__(line, col, message=message)
  100. class UnexpectedEofError(ParseError):
  101. """
  102. The TOML being parsed ended before the end of a statement.
  103. """
  104. def __init__(self, line: int, col: int) -> None:
  105. message = "Unexpected end of file"
  106. super().__init__(line, col, message=message)
  107. class InternalParserError(ParseError):
  108. """
  109. An error that indicates a bug in the parser.
  110. """
  111. def __init__(self, line: int, col: int, message: str | None = None) -> None:
  112. msg = "Internal parser error"
  113. if message:
  114. msg += f" ({message})"
  115. super().__init__(line, col, message=msg)
  116. class NonExistentKey(KeyError, TOMLKitError):
  117. """
  118. A non-existent key was used.
  119. """
  120. def __init__(self, key):
  121. message = f'Key "{key}" does not exist.'
  122. super().__init__(message)
  123. class KeyAlreadyPresent(TOMLKitError):
  124. """
  125. An already present key was used.
  126. """
  127. def __init__(self, key):
  128. key = getattr(key, "key", key)
  129. message = f'Key "{key}" already exists.'
  130. super().__init__(message)
  131. class InvalidControlChar(ParseError):
  132. def __init__(self, line: int, col: int, char: int, type: str) -> None:
  133. display_code = "\\u00"
  134. if char < 16:
  135. display_code += "0"
  136. display_code += hex(char)[2:]
  137. message = (
  138. "Control characters (codes less than 0x1f and 0x7f)"
  139. f" are not allowed in {type}, "
  140. f"use {display_code} instead"
  141. )
  142. super().__init__(line, col, message=message)
  143. class InvalidStringError(ValueError, TOMLKitError):
  144. def __init__(self, value: str, invalid_sequences: Collection[str], delimiter: str):
  145. repr_ = repr(value)[1:-1]
  146. super().__init__(
  147. f"Invalid string: {delimiter}{repr_}{delimiter}. "
  148. f"The character sequences {invalid_sequences} are invalid."
  149. )