Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/dulwich/errors.py: 60%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

67 statements  

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 

23"""Dulwich-related exception classes and utility functions.""" 

24 

25 

26# Please do not add more errors here, but instead add them close to the code 

27# that raises the error. 

28 

29import binascii 

30 

31 

32class ChecksumMismatch(Exception): 

33 """A checksum didn't match the expected contents.""" 

34 

35 def __init__(self, expected, got, extra=None) -> None: 

36 if len(expected) == 20: 

37 expected = binascii.hexlify(expected) 

38 if len(got) == 20: 

39 got = binascii.hexlify(got) 

40 self.expected = expected 

41 self.got = got 

42 self.extra = extra 

43 message = f"Checksum mismatch: Expected {expected}, got {got}" 

44 if self.extra is not None: 

45 message += f"; {extra}" 

46 Exception.__init__(self, message) 

47 

48 

49class WrongObjectException(Exception): 

50 """Baseclass for all the _ is not a _ exceptions on objects. 

51 

52 Do not instantiate directly. 

53 

54 Subclasses should define a type_name attribute that indicates what 

55 was expected if they were raised. 

56 """ 

57 

58 type_name: str 

59 

60 def __init__(self, sha, *args, **kwargs) -> None: 

61 Exception.__init__(self, f"{sha} is not a {self.type_name}") 

62 

63 

64class NotCommitError(WrongObjectException): 

65 """Indicates that the sha requested does not point to a commit.""" 

66 

67 type_name = "commit" 

68 

69 

70class NotTreeError(WrongObjectException): 

71 """Indicates that the sha requested does not point to a tree.""" 

72 

73 type_name = "tree" 

74 

75 

76class NotTagError(WrongObjectException): 

77 """Indicates that the sha requested does not point to a tag.""" 

78 

79 type_name = "tag" 

80 

81 

82class NotBlobError(WrongObjectException): 

83 """Indicates that the sha requested does not point to a blob.""" 

84 

85 type_name = "blob" 

86 

87 

88class MissingCommitError(Exception): 

89 """Indicates that a commit was not found in the repository.""" 

90 

91 def __init__(self, sha, *args, **kwargs) -> None: 

92 self.sha = sha 

93 Exception.__init__(self, f"{sha} is not in the revision store") 

94 

95 

96class ObjectMissing(Exception): 

97 """Indicates that a requested object is missing.""" 

98 

99 def __init__(self, sha, *args, **kwargs) -> None: 

100 Exception.__init__(self, f"{sha} is not in the pack") 

101 

102 

103class ApplyDeltaError(Exception): 

104 """Indicates that applying a delta failed.""" 

105 

106 def __init__(self, *args, **kwargs) -> None: 

107 Exception.__init__(self, *args, **kwargs) 

108 

109 

110class NotGitRepository(Exception): 

111 """Indicates that no Git repository was found.""" 

112 

113 def __init__(self, *args, **kwargs) -> None: 

114 Exception.__init__(self, *args, **kwargs) 

115 

116 

117class GitProtocolError(Exception): 

118 """Git protocol exception.""" 

119 

120 def __init__(self, *args, **kwargs) -> None: 

121 Exception.__init__(self, *args, **kwargs) 

122 

123 def __eq__(self, other): 

124 return isinstance(self, type(other)) and self.args == other.args 

125 

126 

127class SendPackError(GitProtocolError): 

128 """An error occurred during send_pack.""" 

129 

130 

131class HangupException(GitProtocolError): 

132 """Hangup exception.""" 

133 

134 def __init__(self, stderr_lines=None) -> None: 

135 if stderr_lines: 

136 super().__init__( 

137 "\n".join( 

138 line.decode("utf-8", "surrogateescape") for line in stderr_lines 

139 ) 

140 ) 

141 else: 

142 super().__init__("The remote server unexpectedly closed the connection.") 

143 self.stderr_lines = stderr_lines 

144 

145 def __eq__(self, other): 

146 return isinstance(self, type(other)) and self.stderr_lines == other.stderr_lines 

147 

148 

149class UnexpectedCommandError(GitProtocolError): 

150 """Unexpected command received in a proto line.""" 

151 

152 def __init__(self, command) -> None: 

153 command_str = "flush-pkt" if command is None else f"command {command}" 

154 super().__init__(f"Protocol got unexpected {command_str}") 

155 

156 

157class FileFormatException(Exception): 

158 """Base class for exceptions relating to reading git file formats.""" 

159 

160 

161class PackedRefsException(FileFormatException): 

162 """Indicates an error parsing a packed-refs file.""" 

163 

164 

165class ObjectFormatException(FileFormatException): 

166 """Indicates an error parsing an object.""" 

167 

168 

169class NoIndexPresent(Exception): 

170 """No index is present.""" 

171 

172 

173class CommitError(Exception): 

174 """An error occurred while performing a commit.""" 

175 

176 

177class RefFormatError(Exception): 

178 """Indicates an invalid ref name.""" 

179 

180 

181class HookError(Exception): 

182 """An error occurred while executing a hook.""" 

183 

184 

185class WorkingTreeModifiedError(Exception): 

186 """Indicates that the working tree has modifications that would be overwritten."""