~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/errors.py

  • Committer: Aaron Bentley
  • Date: 2005-10-19 19:01:03 UTC
  • mto: (1185.25.1)
  • mto: This revision was merged to the branch mainline in revision 1474.
  • Revision ID: abentley@panoramicfeedback.com-20051019190103-7592b0cfc94e4109
Included configobj in setup.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /usr/bin/env python
2
1
# -*- coding: UTF-8 -*-
3
2
 
4
3
# This program is free software; you can redistribute it and/or modify
19
18
__copyright__ = "Copyright (C) 2005 Canonical Ltd."
20
19
__author__ = "Martin Pool <mbp@canonical.com>"
21
20
 
 
21
# TODO: Change to a standard exception pattern: 
 
22
#
 
23
# - docstring of exceptions is a template for formatting the exception
 
24
#   so the __str__ method can be defined only in the superclass
 
25
# - the arguments to the exception are interpolated into this string
 
26
#
 
27
# when printing the exception we'd then require special handling only
 
28
# for built-in exceptions with no decent __str__ method, such as 
 
29
# ValueError and AssertionError.  See 
 
30
# scott@canonical.com--2005/hct--devel--0.10 util/errors.py
 
31
 
22
32
 
23
33
######################################################################
24
34
# exceptions 
25
35
class BzrError(StandardError):
26
36
    def __str__(self):
 
37
        # XXX: Should we show the exception class in 
 
38
        # exceptions that don't provide their own message?  
 
39
        # maybe it should be done at a higher level
 
40
        ## n = self.__class__.__name__ + ': '
 
41
        n = ''
27
42
        if len(self.args) == 1:
28
 
            return self.args[0]
 
43
            return str(self.args[0])
29
44
        elif len(self.args) == 2:
30
45
            # further explanation or suggestions
31
 
            return '\n  '.join([self.args[0]] + self.args[1])
 
46
            try:
 
47
                return n + '\n  '.join([self.args[0]] + self.args[1])
 
48
            except TypeError:
 
49
                return n + "%r" % self
32
50
        else:
33
 
            return `self.args`
 
51
            return n + `self.args`
34
52
 
35
53
 
36
54
class BzrCheckError(BzrError):
38
56
 
39
57
 
40
58
class InvalidRevisionNumber(BzrError):
41
 
    def __init__(self, revno):
42
 
        self.args = [revno]
43
 
        
44
59
    def __str__(self):
45
60
        return 'invalid revision number: %r' % self.args[0]
46
61
 
51
66
 
52
67
class BzrCommandError(BzrError):
53
68
    # Error from malformed user command
54
 
    pass
 
69
    def __str__(self):
 
70
        return self.args[0]
55
71
 
56
72
 
57
73
class NotBranchError(BzrError):
58
74
    """Specified path is not in a branch"""
59
 
    pass
 
75
    def __str__(self):
 
76
        return 'not a branch: %s' % self.args[0]
 
77
 
 
78
 
 
79
class UnsupportedFormatError(BzrError):
 
80
    """Specified path is a bzr branch that we cannot read."""
 
81
    def __str__(self):
 
82
        return 'unsupported branch format: %s' % self.args[0]
60
83
 
61
84
 
62
85
class NotVersionedError(BzrError):
88
111
            Exception.__init__(self)
89
112
 
90
113
 
 
114
class CommitNotPossible(LockError):
 
115
    """A commit was attempted but we do not have a write lock open."""
 
116
 
 
117
 
 
118
class AlreadyCommitted(LockError):
 
119
    """A rollback was requested, but is not able to be accomplished."""
 
120
 
 
121
 
 
122
class ReadOnlyError(LockError):
 
123
    """A write attempt was made in a read only transaction."""
 
124
 
 
125
 
91
126
class PointlessCommit(Exception):
92
127
    """Commit failed because nothing was changed."""
93
128
 
108
143
                          % (branch, object_type, object_id))
109
144
 
110
145
 
 
146
class DivergedBranches(BzrError):
 
147
    def __init__(self, branch1, branch2):
 
148
        BzrError.__init__(self, "These branches have diverged.")
 
149
        self.branch1 = branch1
 
150
        self.branch2 = branch2
 
151
 
 
152
 
111
153
class UnrelatedBranches(BzrCommandError):
112
154
    def __init__(self):
113
155
        msg = "Branches have no common ancestor, and no base revision"\
114
156
            " specified."
115
157
        BzrCommandError.__init__(self, msg)
116
158
 
 
159
class NoCommonAncestor(BzrError):
 
160
    def __init__(self, revision_a, revision_b):
 
161
        msg = "Revisions have no common ancestor: %s %s." \
 
162
            % (revision_a, revision_b) 
 
163
        BzrError.__init__(self, msg)
 
164
 
 
165
class NoCommonRoot(BzrError):
 
166
    def __init__(self, revision_a, revision_b):
 
167
        msg = "Revisions are not derived from the same root: %s %s." \
 
168
            % (revision_a, revision_b) 
 
169
        BzrError.__init__(self, msg)
 
170
 
 
171
class NotAncestor(BzrError):
 
172
    def __init__(self, rev_id, not_ancestor_id):
 
173
        msg = "Revision %s is not an ancestor of %s" % (not_ancestor_id, 
 
174
                                                        rev_id)
 
175
        BzrError.__init__(self, msg)
 
176
        self.rev_id = rev_id
 
177
        self.not_ancestor_id = not_ancestor_id
 
178
 
117
179
 
118
180
class NotAncestor(BzrError):
119
181
    def __init__(self, rev_id, not_ancestor_id):
126
188
 
127
189
class InstallFailed(BzrError):
128
190
    def __init__(self, revisions):
 
191
        msg = "Could not install revisions:\n%s" % " ,".join(revisions)
 
192
        BzrError.__init__(self, msg)
129
193
        self.revisions = revisions
130
 
        msg = "Could not install revisions:\n%s" % " ,".join(revisions)
131
 
        BzrError.__init__(self, msg)
132
194
 
133
195
 
134
196
class AmbiguousBase(BzrError):
138
200
        BzrError.__init__(self, msg)
139
201
        self.bases = bases
140
202
 
 
203
class NoCommits(BzrError):
 
204
    def __init__(self, branch):
 
205
        msg = "Branch %s has no commits." % branch
 
206
        BzrError.__init__(self, msg)
 
207
 
 
208
class UnlistableStore(BzrError):
 
209
    def __init__(self, store):
 
210
        BzrError.__init__(self, "Store %s is not listable" % store)
 
211
 
 
212
class UnlistableBranch(BzrError):
 
213
    def __init__(self, br):
 
214
        BzrError.__init__(self, "Stores for branch %s are not listable" % br)
 
215
 
 
216
 
 
217
from bzrlib.weave import WeaveError, WeaveParentMismatch
 
218
 
 
219
class TransportError(BzrError):
 
220
    """All errors thrown by Transport implementations should derive
 
221
    from this class.
 
222
    """
 
223
    def __init__(self, msg=None, orig_error=None):
 
224
        if msg is None and orig_error is not None:
 
225
            msg = str(orig_error)
 
226
        BzrError.__init__(self, msg)
 
227
        self.msg = msg
 
228
        self.orig_error = orig_error
 
229
 
 
230
# A set of semi-meaningful errors which can be thrown
 
231
class TransportNotPossible(TransportError):
 
232
    """This is for transports where a specific function is explicitly not
 
233
    possible. Such as pushing files to an HTTP server.
 
234
    """
 
235
    pass
 
236
 
 
237
class NonRelativePath(TransportError):
 
238
    """An absolute path was supplied, that could not be decoded into
 
239
    a relative path.
 
240
    """
 
241
    pass
 
242
 
 
243
class NoSuchFile(TransportError, IOError):
 
244
    """A get() was issued for a file that doesn't exist."""
 
245
 
 
246
    # XXX: Is multiple inheritance for exceptions really needed?
 
247
 
 
248
    def __str__(self):
 
249
        return 'no such file: ' + self.msg
 
250
 
 
251
    def __init__(self, msg=None, orig_error=None):
 
252
        import errno
 
253
        TransportError.__init__(self, msg=msg, orig_error=orig_error)
 
254
        IOError.__init__(self, errno.ENOENT, self.msg)
 
255
 
 
256
class FileExists(TransportError, OSError):
 
257
    """An operation was attempted, which would overwrite an entry,
 
258
    but overwritting is not supported.
 
259
 
 
260
    mkdir() can throw this, but put() just overwites existing files.
 
261
    """
 
262
    # XXX: Is multiple inheritance for exceptions really needed?
 
263
    def __init__(self, msg=None, orig_error=None):
 
264
        import errno
 
265
        TransportError.__init__(self, msg=msg, orig_error=orig_error)
 
266
        OSError.__init__(self, errno.EEXIST, self.msg)
 
267
 
 
268
class PermissionDenied(TransportError):
 
269
    """An operation cannot succeed because of a lack of permissions."""
 
270
    pass
 
271
 
 
272
class ConnectionReset(TransportError):
 
273
    """The connection has been closed."""
 
274
    pass
 
275
 
 
276
class ConflictsInTree(BzrError):
 
277
    def __init__(self):
 
278
        BzrError.__init__(self, "Working tree has conflicts.")
 
279
 
 
280
class ParseConfigError(BzrError):
 
281
    def __init__(self, errors, filename):
 
282
        if filename is None:
 
283
            filename = ""
 
284
        message = "Error(s) parsing config file %s:\n%s" % \
 
285
            (filename, ('\n'.join(e.message for e in errors)))
 
286
        BzrError.__init__(self, message)
 
287
 
 
288
class SigningFailed(BzrError):
 
289
    def __init__(self, command_line):
 
290
        BzrError.__init__(self, "Failed to gpg sign data with command '%s'"
 
291
                               % command_line)