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

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 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 

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 if self.extra is None: 

44 Exception.__init__( 

45 self, 

46 f"Checksum mismatch: Expected {expected}, got {got}", 

47 ) 

48 else: 

49 Exception.__init__( 

50 self, 

51 f"Checksum mismatch: Expected {expected}, got {got}; {extra}", 

52 ) 

53 

54 

55class WrongObjectException(Exception): 

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

57 

58 Do not instantiate directly. 

59 

60 Subclasses should define a type_name attribute that indicates what 

61 was expected if they were raised. 

62 """ 

63 

64 type_name: str 

65 

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

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

68 

69 

70class NotCommitError(WrongObjectException): 

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

72 

73 type_name = "commit" 

74 

75 

76class NotTreeError(WrongObjectException): 

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

78 

79 type_name = "tree" 

80 

81 

82class NotTagError(WrongObjectException): 

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

84 

85 type_name = "tag" 

86 

87 

88class NotBlobError(WrongObjectException): 

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

90 

91 type_name = "blob" 

92 

93 

94class MissingCommitError(Exception): 

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

96 

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

98 self.sha = sha 

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

100 

101 

102class ObjectMissing(Exception): 

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

104 

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

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

107 

108 

109class ApplyDeltaError(Exception): 

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

111 

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

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

114 

115 

116class NotGitRepository(Exception): 

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

118 

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

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

121 

122 

123class GitProtocolError(Exception): 

124 """Git protocol exception.""" 

125 

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

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

128 

129 def __eq__(self, other): 

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

131 

132 

133class SendPackError(GitProtocolError): 

134 """An error occurred during send_pack.""" 

135 

136 

137class HangupException(GitProtocolError): 

138 """Hangup exception.""" 

139 

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

141 if stderr_lines: 

142 super().__init__( 

143 "\n".join( 

144 [line.decode("utf-8", "surrogateescape") for line in stderr_lines] 

145 ) 

146 ) 

147 else: 

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

149 self.stderr_lines = stderr_lines 

150 

151 def __eq__(self, other): 

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

153 

154 

155class UnexpectedCommandError(GitProtocolError): 

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

157 

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

159 if command is None: 

160 command = "flush-pkt" 

161 else: 

162 command = f"command {command}" 

163 super().__init__(f"Protocol got unexpected {command}") 

164 

165 

166class FileFormatException(Exception): 

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

168 

169 

170class PackedRefsException(FileFormatException): 

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

172 

173 

174class ObjectFormatException(FileFormatException): 

175 """Indicates an error parsing an object.""" 

176 

177 

178class NoIndexPresent(Exception): 

179 """No index is present.""" 

180 

181 

182class CommitError(Exception): 

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

184 

185 

186class RefFormatError(Exception): 

187 """Indicates an invalid ref name.""" 

188 

189 

190class HookError(Exception): 

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