errors.py 5.8 KB

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