Avoid seek when loading pack index files.
[jelmer/dulwich-libgit2.git] / dulwich / errors.py
1 # errors.py -- errors for dulwich
2 # Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
3 # Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; version 2
8 # or (at your option) any later version of the License.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 # MA  02110-1301, USA.
19
20 """Dulwich-related exception classes and utility functions."""
21
22 class ChecksumMismatch(Exception):
23     """A checksum didn't match the expected contents."""
24
25     def __init__(self, expected, got, extra=None):
26         self.expected = expected
27         self.got = got
28         self.extra = extra
29         if self.extra is None:
30             Exception.__init__(self, 
31                 "Checksum mismatch: Expected %s, got %s" % (expected, got))
32         else:
33             Exception.__init__(self,
34                 "Checksum mismatch: Expected %s, got %s; %s" % 
35                 (expected, got, extra))
36
37
38 class WrongObjectException(Exception):
39     """Baseclass for all the _ is not a _ exceptions on objects.
40
41     Do not instantiate directly.
42
43     Subclasses should define a type_name attribute that indicates what
44     was expected if they were raised.
45     """
46
47     def __init__(self, sha, *args, **kwargs):
48         Exception.__init__(self, "%s is not a %s" % (sha, self.type_name))
49
50
51 class NotCommitError(WrongObjectException):
52     """Indicates that the sha requested does not point to a commit."""
53
54     type_name = 'commit'
55
56
57 class NotTreeError(WrongObjectException):
58     """Indicates that the sha requested does not point to a tree."""
59
60     type_name = 'tree'
61
62
63 class NotTagError(WrongObjectException):
64     """Indicates that the sha requested does not point to a tag."""
65
66     type_name = 'tag'
67
68
69 class NotBlobError(WrongObjectException):
70     """Indicates that the sha requested does not point to a blob."""
71
72     type_name = 'blob'
73
74
75 class MissingCommitError(Exception):
76     """Indicates that a commit was not found in the repository"""
77   
78     def __init__(self, sha, *args, **kwargs):
79         Exception.__init__(self, "%s is not in the revision store" % sha)
80
81
82 class ObjectMissing(Exception):
83     """Indicates that a requested object is missing."""
84   
85     def __init__(self, sha, *args, **kwargs):
86         Exception.__init__(self, "%s is not in the pack" % sha)
87
88
89 class ApplyDeltaError(Exception):
90     """Indicates that applying a delta failed."""
91     
92     def __init__(self, *args, **kwargs):
93         Exception.__init__(self, *args, **kwargs)
94
95
96 class NotGitRepository(Exception):
97     """Indicates that no Git repository was found."""
98
99     def __init__(self, *args, **kwargs):
100         Exception.__init__(self, *args, **kwargs)
101
102
103 class GitProtocolError(Exception):
104     """Git protocol exception."""
105     
106     def __init__(self, *args, **kwargs):
107         Exception.__init__(self, *args, **kwargs)
108
109
110 class HangupException(GitProtocolError):
111     """Hangup exception."""
112
113     def __init__(self):
114         Exception.__init__(self,
115             "The remote server unexpectedly closed the connection.")
116
117
118 class FileFormatException(Exception):
119     """Base class for exceptions relating to reading git file formats."""
120
121
122 class PackedRefsException(FileFormatException):
123     """Indicates an error parsing a packed-refs file."""
124
125
126 class NoIndexPresent(Exception):
127     """No index is present."""