errors.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 public 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. class ChecksumMismatch(Exception):
  27. """A checksum didn't match the expected contents."""
  28. def __init__(self, expected, got, extra=None) -> None:
  29. if len(expected) == 20:
  30. expected = binascii.hexlify(expected)
  31. if len(got) == 20:
  32. got = binascii.hexlify(got)
  33. self.expected = expected
  34. self.got = got
  35. self.extra = extra
  36. if self.extra is None:
  37. Exception.__init__(
  38. self,
  39. f"Checksum mismatch: Expected {expected}, got {got}",
  40. )
  41. else:
  42. Exception.__init__(
  43. self,
  44. f"Checksum mismatch: Expected {expected}, got {got}; {extra}",
  45. )
  46. class WrongObjectException(Exception):
  47. """Baseclass for all the _ is not a _ exceptions on objects.
  48. Do not instantiate directly.
  49. Subclasses should define a type_name attribute that indicates what
  50. was expected if they were raised.
  51. """
  52. type_name: str
  53. def __init__(self, sha, *args, **kwargs) -> None:
  54. Exception.__init__(self, f"{sha} is not a {self.type_name}")
  55. class NotCommitError(WrongObjectException):
  56. """Indicates that the sha requested does not point to a commit."""
  57. type_name = "commit"
  58. class NotTreeError(WrongObjectException):
  59. """Indicates that the sha requested does not point to a tree."""
  60. type_name = "tree"
  61. class NotTagError(WrongObjectException):
  62. """Indicates that the sha requested does not point to a tag."""
  63. type_name = "tag"
  64. class NotBlobError(WrongObjectException):
  65. """Indicates that the sha requested does not point to a blob."""
  66. type_name = "blob"
  67. class MissingCommitError(Exception):
  68. """Indicates that a commit was not found in the repository."""
  69. def __init__(self, sha, *args, **kwargs) -> None:
  70. self.sha = sha
  71. Exception.__init__(self, f"{sha} is not in the revision store")
  72. class ObjectMissing(Exception):
  73. """Indicates that a requested object is missing."""
  74. def __init__(self, sha, *args, **kwargs) -> None:
  75. Exception.__init__(self, f"{sha} is not in the pack")
  76. class ApplyDeltaError(Exception):
  77. """Indicates that applying a delta failed."""
  78. def __init__(self, *args, **kwargs) -> None:
  79. Exception.__init__(self, *args, **kwargs)
  80. class NotGitRepository(Exception):
  81. """Indicates that no Git repository was found."""
  82. def __init__(self, *args, **kwargs) -> None:
  83. Exception.__init__(self, *args, **kwargs)
  84. class GitProtocolError(Exception):
  85. """Git protocol exception."""
  86. def __init__(self, *args, **kwargs) -> None:
  87. Exception.__init__(self, *args, **kwargs)
  88. def __eq__(self, other):
  89. return isinstance(self, type(other)) and self.args == other.args
  90. class SendPackError(GitProtocolError):
  91. """An error occurred during send_pack."""
  92. class HangupException(GitProtocolError):
  93. """Hangup exception."""
  94. def __init__(self, stderr_lines=None) -> None:
  95. if stderr_lines:
  96. super().__init__(
  97. "\n".join(
  98. [line.decode("utf-8", "surrogateescape") for line in stderr_lines]
  99. )
  100. )
  101. else:
  102. super().__init__("The remote server unexpectedly closed the connection.")
  103. self.stderr_lines = stderr_lines
  104. def __eq__(self, other):
  105. return isinstance(self, type(other)) and self.stderr_lines == other.stderr_lines
  106. class UnexpectedCommandError(GitProtocolError):
  107. """Unexpected command received in a proto line."""
  108. def __init__(self, command) -> None:
  109. if command is None:
  110. command = "flush-pkt"
  111. else:
  112. command = f"command {command}"
  113. super().__init__(f"Protocol got unexpected {command}")
  114. class FileFormatException(Exception):
  115. """Base class for exceptions relating to reading git file formats."""
  116. class PackedRefsException(FileFormatException):
  117. """Indicates an error parsing a packed-refs file."""
  118. class ObjectFormatException(FileFormatException):
  119. """Indicates an error parsing an object."""
  120. class NoIndexPresent(Exception):
  121. """No index is present."""
  122. class CommitError(Exception):
  123. """An error occurred while performing a commit."""
  124. class RefFormatError(Exception):
  125. """Indicates an invalid ref name."""
  126. class HookError(Exception):
  127. """An error occurred while executing a hook."""