errors.py 5.5 KB

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