errors.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. # errors.py -- errors for dulwich
  2. # Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
  3. # Copyright (C) 2009-2012 Jelmer Vernooij <jelmer@jelmer.uk>
  4. #
  5. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  7. # General Public License as published by the Free Software Foundation; version 2.0
  8. # or (at your option) any later version. You can redistribute it and/or
  9. # modify it under the terms of either of these two licenses.
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. # You should have received a copy of the licenses; if not, see
  18. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  19. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  20. # License, Version 2.0.
  21. #
  22. """Dulwich-related exception classes and utility functions."""
  23. # Please do not add more errors here, but instead add them close to the code
  24. # that raises the error.
  25. import binascii
  26. from typing import Optional, Union
  27. class ChecksumMismatch(Exception):
  28. """A checksum didn't match the expected contents."""
  29. def __init__(
  30. self,
  31. expected: Union[bytes, str],
  32. got: Union[bytes, str],
  33. extra: Optional[str] = None,
  34. ) -> None:
  35. if isinstance(expected, bytes) and len(expected) == 20:
  36. expected_str = binascii.hexlify(expected).decode("ascii")
  37. else:
  38. expected_str = (
  39. expected if isinstance(expected, str) else expected.decode("ascii")
  40. )
  41. if isinstance(got, bytes) and len(got) == 20:
  42. got_str = binascii.hexlify(got).decode("ascii")
  43. else:
  44. got_str = got if isinstance(got, str) else got.decode("ascii")
  45. self.expected = expected_str
  46. self.got = got_str
  47. self.extra = extra
  48. message = f"Checksum mismatch: Expected {expected_str}, got {got_str}"
  49. if self.extra is not None:
  50. message += f"; {extra}"
  51. Exception.__init__(self, message)
  52. class WrongObjectException(Exception):
  53. """Baseclass for all the _ is not a _ exceptions on objects.
  54. Do not instantiate directly.
  55. Subclasses should define a type_name attribute that indicates what
  56. was expected if they were raised.
  57. """
  58. type_name: str
  59. def __init__(self, sha: bytes, *args: object, **kwargs: object) -> None:
  60. Exception.__init__(self, f"{sha.decode('ascii')} is not a {self.type_name}")
  61. class NotCommitError(WrongObjectException):
  62. """Indicates that the sha requested does not point to a commit."""
  63. type_name = "commit"
  64. class NotTreeError(WrongObjectException):
  65. """Indicates that the sha requested does not point to a tree."""
  66. type_name = "tree"
  67. class NotTagError(WrongObjectException):
  68. """Indicates that the sha requested does not point to a tag."""
  69. type_name = "tag"
  70. class NotBlobError(WrongObjectException):
  71. """Indicates that the sha requested does not point to a blob."""
  72. type_name = "blob"
  73. class MissingCommitError(Exception):
  74. """Indicates that a commit was not found in the repository."""
  75. def __init__(self, sha: bytes, *args: object, **kwargs: object) -> None:
  76. self.sha = sha
  77. Exception.__init__(self, f"{sha.decode('ascii')} is not in the revision store")
  78. class ObjectMissing(Exception):
  79. """Indicates that a requested object is missing."""
  80. def __init__(self, sha: bytes, *args: object, **kwargs: object) -> None:
  81. Exception.__init__(self, f"{sha.decode('ascii')} is not in the pack")
  82. class ApplyDeltaError(Exception):
  83. """Indicates that applying a delta failed."""
  84. def __init__(self, *args: object, **kwargs: object) -> None:
  85. Exception.__init__(self, *args, **kwargs)
  86. class NotGitRepository(Exception):
  87. """Indicates that no Git repository was found."""
  88. def __init__(self, *args: object, **kwargs: object) -> None:
  89. Exception.__init__(self, *args, **kwargs)
  90. class GitProtocolError(Exception):
  91. """Git protocol exception."""
  92. def __init__(self, *args: object, **kwargs: object) -> None:
  93. Exception.__init__(self, *args, **kwargs)
  94. def __eq__(self, other: object) -> bool:
  95. return isinstance(other, GitProtocolError) and self.args == other.args
  96. class SendPackError(GitProtocolError):
  97. """An error occurred during send_pack."""
  98. class HangupException(GitProtocolError):
  99. """Hangup exception."""
  100. def __init__(self, stderr_lines: Optional[list[bytes]] = None) -> None:
  101. if stderr_lines:
  102. super().__init__(
  103. "\n".join(
  104. line.decode("utf-8", "surrogateescape") for line in stderr_lines
  105. )
  106. )
  107. else:
  108. super().__init__("The remote server unexpectedly closed the connection.")
  109. self.stderr_lines = stderr_lines
  110. def __eq__(self, other: object) -> bool:
  111. return (
  112. isinstance(other, HangupException)
  113. and self.stderr_lines == other.stderr_lines
  114. )
  115. class UnexpectedCommandError(GitProtocolError):
  116. """Unexpected command received in a proto line."""
  117. def __init__(self, command: Optional[str]) -> None:
  118. command_str = "flush-pkt" if command is None else f"command {command}"
  119. super().__init__(f"Protocol got unexpected {command_str}")
  120. class FileFormatException(Exception):
  121. """Base class for exceptions relating to reading git file formats."""
  122. class PackedRefsException(FileFormatException):
  123. """Indicates an error parsing a packed-refs file."""
  124. class ObjectFormatException(FileFormatException):
  125. """Indicates an error parsing an object."""
  126. class NoIndexPresent(Exception):
  127. """No index is present."""
  128. class CommitError(Exception):
  129. """An error occurred while performing a commit."""
  130. class RefFormatError(Exception):
  131. """Indicates an invalid ref name."""
  132. class HookError(Exception):
  133. """An error occurred while executing a hook."""
  134. class WorkingTreeModifiedError(Exception):
  135. """Indicates that the working tree has modifications that would be overwritten."""