~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/errors.py

  • Committer: Martin Pool
  • Date: 2005-04-28 07:24:55 UTC
  • Revision ID: mbp@sourcefrog.net-20050428072453-7b99afa993a1e549
todo

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/env python
1
2
# -*- coding: UTF-8 -*-
2
3
 
3
4
# This program is free software; you can redistribute it and/or modify
18
19
__copyright__ = "Copyright (C) 2005 Canonical Ltd."
19
20
__author__ = "Martin Pool <mbp@canonical.com>"
20
21
 
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
 
 
32
22
 
33
23
######################################################################
34
24
# exceptions 
35
25
class BzrError(StandardError):
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 = ''
42
 
        if len(self.args) == 1:
43
 
            return str(self.args[0])
44
 
        elif len(self.args) == 2:
45
 
            # further explanation or suggestions
46
 
            try:
47
 
                return n + '\n  '.join([self.args[0]] + self.args[1])
48
 
            except TypeError:
49
 
                return n + "%r" % self
50
 
        else:
51
 
            return n + `self.args`
52
 
 
 
26
    pass
53
27
 
54
28
class BzrCheckError(BzrError):
55
29
    pass
56
30
 
57
31
 
58
 
class InvalidRevisionNumber(BzrError):
59
 
    def __str__(self):
60
 
        return 'invalid revision number: %r' % self.args[0]
61
 
 
62
 
 
63
 
class InvalidRevisionId(BzrError):
64
 
    pass
65
 
 
66
 
 
67
 
class BzrCommandError(BzrError):
68
 
    # Error from malformed user command
69
 
    def __str__(self):
70
 
        return self.args[0]
71
 
 
72
 
 
73
 
class NotBranchError(BzrError):
74
 
    """Specified path is not in a branch"""
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]
83
 
 
84
 
 
85
 
class NotVersionedError(BzrError):
86
 
    """Specified object is not versioned."""
87
 
 
88
 
 
89
 
class BadFileKindError(BzrError):
90
 
    """Specified file is of a kind that cannot be added.
91
 
 
92
 
    (For example a symlink or device file.)"""
93
 
    pass
94
 
 
95
 
 
96
 
class ForbiddenFileError(BzrError):
97
 
    """Cannot operate on a file because it is a control file."""
98
 
    pass
99
 
 
100
 
 
101
 
class LockError(Exception):
102
 
    """All exceptions from the lock/unlock functions should be from
103
 
    this exception class.  They will be translated as necessary. The
104
 
    original exception is available as e.original_error
105
 
    """
106
 
    def __init__(self, e=None):
107
 
        self.original_error = e
108
 
        if e:
109
 
            Exception.__init__(self, e)
110
 
        else:
111
 
            Exception.__init__(self)
112
 
 
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
 
 
126
 
class PointlessCommit(Exception):
127
 
    """Commit failed because nothing was changed."""
128
 
 
129
 
 
130
 
class NoSuchRevision(BzrError):
131
 
    def __init__(self, branch, revision):
132
 
        self.branch = branch
133
 
        self.revision = revision
134
 
        msg = "Branch %s has no revision %s" % (branch, revision)
135
 
        BzrError.__init__(self, msg)
136
 
 
137
 
 
138
 
class HistoryMissing(BzrError):
139
 
    def __init__(self, branch, object_type, object_id):
140
 
        self.branch = branch
141
 
        BzrError.__init__(self,
142
 
                          '%s is missing %s {%s}'
143
 
                          % (branch, object_type, object_id))
144
 
 
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
 
 
153
 
class UnrelatedBranches(BzrCommandError):
154
 
    def __init__(self):
155
 
        msg = "Branches have no common ancestor, and no base revision"\
156
 
            " specified."
157
 
        BzrCommandError.__init__(self, msg)
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
 
 
179
 
 
180
 
class NotAncestor(BzrError):
181
 
    def __init__(self, rev_id, not_ancestor_id):
182
 
        self.rev_id = rev_id
183
 
        self.not_ancestor_id = not_ancestor_id
184
 
        msg = "Revision %s is not an ancestor of %s" % (not_ancestor_id, 
185
 
                                                        rev_id)
186
 
        BzrError.__init__(self, msg)
187
 
 
188
 
 
189
 
class InstallFailed(BzrError):
190
 
    def __init__(self, revisions):
191
 
        msg = "Could not install revisions:\n%s" % " ,".join(revisions)
192
 
        BzrError.__init__(self, msg)
193
 
        self.revisions = revisions
194
 
 
195
 
 
196
 
class AmbiguousBase(BzrError):
197
 
    def __init__(self, bases):
198
 
        msg = "The correct base is unclear, becase %s are all equally close" %\
199
 
            ", ".join(bases)
200
 
        BzrError.__init__(self, msg)
201
 
        self.bases = bases
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.")
 
32
 
 
33
 
 
34
def bailout(msg, explanation=[]):
 
35
    ex = BzrError(msg, explanation)
 
36
    import trace
 
37
    trace._tracefile.write('* raising %s\n' % ex)
 
38
    raise ex
 
39