/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/errors.py

  • Committer: Robert Collins
  • Date: 2006-01-02 22:37:32 UTC
  • mfrom: (1185.50.33 bzr-jam-integration)
  • Revision ID: robertc@robertcollins.net-20060102223732-d5221b37ff0f7888
Merge in John Meinels integration branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# (C) 2005 Canonical
 
2
 
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
 
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
 
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Exceptions for bzr, and reporting of them.
 
18
 
 
19
Exceptions are caught at a high level to report errors to the user, and
 
20
might also be caught inside the program.  Therefore it needs to be
 
21
possible to convert them to a meaningful string, and also for them to be
 
22
interrogated by the program.
 
23
 
 
24
Exceptions are defined such that the arguments given to the constructor
 
25
are stored in the object as properties of the same name.  When the
 
26
object is printed as a string, the doc string of the class is used as
 
27
a format string with the property dictionary available to it.
 
28
 
 
29
This means that exceptions can used like this:
 
30
 
 
31
>>> import sys
 
32
>>> try:
 
33
...   raise NotBranchError(path='/foo/bar')
 
34
... except:
 
35
...   print sys.exc_type
 
36
...   print sys.exc_value
 
37
...   if hasattr(sys.exc_value, 'path'):
 
38
...     print sys.exc_value.path
 
39
bzrlib.errors.NotBranchError
 
40
Not a branch: /foo/bar
 
41
/foo/bar
 
42
 
 
43
Therefore:
 
44
 
 
45
 * create a new exception class for any class of error that can be
 
46
   usefully distinguished.
 
47
 
 
48
 * the printable form of an exception is generated by the base class
 
49
   __str__ method
 
50
 
 
51
Exception strings should start with a capital letter and not have a final
 
52
fullstop.
 
53
"""
 
54
 
 
55
# based on Scott James Remnant's hct error classes
 
56
 
 
57
# TODO: is there any value in providing the .args field used by standard
 
58
# python exceptions?   A list of values with no names seems less useful 
 
59
# to me.
 
60
 
 
61
# TODO: Perhaps convert the exception to a string at the moment it's 
 
62
# constructed to make sure it will succeed.  But that says nothing about
 
63
# exceptions that are never raised.
 
64
 
 
65
# TODO: Convert all the other error classes here to BzrNewError, and eliminate
 
66
# the old one.
 
67
 
 
68
 
 
69
class BzrError(StandardError):
 
70
    def __str__(self):
 
71
        # XXX: Should we show the exception class in 
 
72
        # exceptions that don't provide their own message?  
 
73
        # maybe it should be done at a higher level
 
74
        ## n = self.__class__.__name__ + ': '
 
75
        n = ''
 
76
        if len(self.args) == 1:
 
77
            return str(self.args[0])
 
78
        elif len(self.args) == 2:
 
79
            # further explanation or suggestions
 
80
            try:
 
81
                return n + '\n  '.join([self.args[0]] + self.args[1])
 
82
            except TypeError:
 
83
                return n + "%r" % self
 
84
        else:
 
85
            return n + `self.args`
 
86
 
 
87
 
 
88
class BzrNewError(BzrError):
 
89
    """bzr error"""
 
90
    # base classes should override the docstring with their human-
 
91
    # readable explanation
 
92
 
 
93
    def __init__(self, **kwds):
 
94
        for key, value in kwds.items():
 
95
            setattr(self, key, value)
 
96
 
 
97
    def __str__(self):
 
98
        try:
 
99
            return self.__doc__ % self.__dict__
 
100
        except (NameError, ValueError, KeyError), e:
 
101
            return 'Unprintable exception %s: %s' \
 
102
                % (self.__class__.__name__, str(e))
 
103
 
 
104
 
 
105
class BzrCheckError(BzrNewError):
 
106
    """Internal check failed: %(message)s"""
 
107
    def __init__(self, message):
 
108
        BzrNewError.__init__(self)
 
109
        self.message = message
 
110
 
 
111
 
 
112
class InvalidEntryName(BzrNewError):
 
113
    """Invalid entry name: %(name)s"""
 
114
    def __init__(self, name):
 
115
        BzrNewError.__init__(self)
 
116
        self.name = name
 
117
 
 
118
 
 
119
class InvalidRevisionNumber(BzrNewError):
 
120
    """Invalid revision number %(revno)d"""
 
121
    def __init__(self, revno):
 
122
        BzrNewError.__init__(self)
 
123
        self.revno = revno
 
124
 
 
125
 
 
126
class InvalidRevisionId(BzrNewError):
 
127
    """Invalid revision-id {%(revision_id)s} in %(branch)s"""
 
128
    def __init__(self, revision_id, branch):
 
129
        BzrNewError.__init__(self)
 
130
        self.revision_id = revision_id
 
131
        self.branch = branch
 
132
 
 
133
 
 
134
class NoWorkingTree(BzrNewError):
 
135
    """No WorkingTree exists for %s(base)."""
 
136
    
 
137
    def __init__(self, base):
 
138
        BzrNewError.__init__(self)
 
139
        self.base = base
 
140
 
 
141
 
 
142
class BzrCommandError(BzrError):
 
143
    # Error from malformed user command
 
144
    # This is being misused as a generic exception
 
145
    # pleae subclass. RBC 20051030
 
146
    #
 
147
    # I think it's a waste of effort to differentiate between errors that
 
148
    # are not intended to be caught anyway.  UI code need not subclass
 
149
    # BzrCommandError, and non-UI code should not throw a subclass of
 
150
    # BzrCommandError.  ADHB 20051211
 
151
    def __str__(self):
 
152
        return self.args[0]
 
153
 
 
154
 
 
155
class BzrOptionError(BzrCommandError):
 
156
    """Some missing or otherwise incorrect option was supplied."""
 
157
 
 
158
    
 
159
class StrictCommitFailed(Exception):
 
160
    """Commit refused because there are unknowns in the tree."""
 
161
 
 
162
 
 
163
class PathError(BzrNewError):
 
164
    """Generic path error: %(path)r%(extra)s)"""
 
165
    def __init__(self, path, extra=None):
 
166
        BzrNewError.__init__(self)
 
167
        self.path = path
 
168
        if extra:
 
169
            self.extra = ': ' + str(extra)
 
170
        else:
 
171
            self.extra = ''
 
172
 
 
173
 
 
174
class NoSuchFile(PathError):
 
175
    """No such file: %(path)r%(extra)s"""
 
176
 
 
177
 
 
178
class FileExists(PathError):
 
179
    """File exists: %(path)r%(extra)s"""
 
180
 
 
181
 
 
182
class PermissionDenied(PathError):
 
183
    """Permission denied: %(path)r%(extra)s"""
 
184
 
 
185
 
 
186
class PathNotChild(BzrNewError):
 
187
    """Path %(path)r is not a child of path %(base)r%(extra)s"""
 
188
    def __init__(self, path, base, extra=None):
 
189
        BzrNewError.__init__(self)
 
190
        self.path = path
 
191
        self.base = base
 
192
        if extra:
 
193
            self.extra = ': ' + str(extra)
 
194
        else:
 
195
            self.extra = ''
 
196
 
 
197
 
 
198
class NotBranchError(BzrNewError):
 
199
    """Not a branch: %(path)s"""
 
200
    def __init__(self, path):
 
201
        BzrNewError.__init__(self)
 
202
        self.path = path
 
203
 
 
204
 
 
205
class FileInWrongBranch(BzrNewError):
 
206
    """File %(path)s in not in branch %(branch_base)s."""
 
207
    def __init__(self, branch, path):
 
208
        BzrNewError.__init__(self)
 
209
        self.branch = branch
 
210
        self.branch_base = branch.base
 
211
        self.path = path
 
212
 
 
213
 
 
214
class UnsupportedFormatError(BzrError):
 
215
    """Specified path is a bzr branch that we cannot read."""
 
216
    def __str__(self):
 
217
        return 'unsupported branch format: %s' % self.args[0]
 
218
 
 
219
 
 
220
class NotVersionedError(BzrNewError):
 
221
    """%(path)s is not versioned"""
 
222
    def __init__(self, path):
 
223
        BzrNewError.__init__(self)
 
224
        self.path = path
 
225
 
 
226
 
 
227
class BadFileKindError(BzrError):
 
228
    """Specified file is of a kind that cannot be added.
 
229
 
 
230
    (For example a symlink or device file.)"""
 
231
 
 
232
 
 
233
class ForbiddenFileError(BzrError):
 
234
    """Cannot operate on a file because it is a control file."""
 
235
 
 
236
 
 
237
class LockError(Exception):
 
238
    """Lock error"""
 
239
    # All exceptions from the lock/unlock functions should be from
 
240
    # this exception class.  They will be translated as necessary. The
 
241
    # original exception is available as e.original_error
 
242
 
 
243
 
 
244
class CommitNotPossible(LockError):
 
245
    """A commit was attempted but we do not have a write lock open."""
 
246
 
 
247
 
 
248
class AlreadyCommitted(LockError):
 
249
    """A rollback was requested, but is not able to be accomplished."""
 
250
 
 
251
 
 
252
class ReadOnlyError(LockError):
 
253
    """A write attempt was made in a read only transaction."""
 
254
 
 
255
 
 
256
class PointlessCommit(BzrNewError):
 
257
    """No changes to commit"""
 
258
 
 
259
class StrictCommitFailed(Exception):
 
260
    """Commit refused because there are unknowns in the tree."""
 
261
 
 
262
class NoSuchRevision(BzrError):
 
263
    def __init__(self, branch, revision):
 
264
        self.branch = branch
 
265
        self.revision = revision
 
266
        msg = "Branch %s has no revision %s" % (branch, revision)
 
267
        BzrError.__init__(self, msg)
 
268
 
 
269
 
 
270
class HistoryMissing(BzrError):
 
271
    def __init__(self, branch, object_type, object_id):
 
272
        self.branch = branch
 
273
        BzrError.__init__(self,
 
274
                          '%s is missing %s {%s}'
 
275
                          % (branch, object_type, object_id))
 
276
 
 
277
 
 
278
class DivergedBranches(BzrError):
 
279
    def __init__(self, branch1, branch2):
 
280
        BzrError.__init__(self, "These branches have diverged.  Try merge.")
 
281
        self.branch1 = branch1
 
282
        self.branch2 = branch2
 
283
 
 
284
 
 
285
class UnrelatedBranches(BzrCommandError):
 
286
    def __init__(self):
 
287
        msg = "Branches have no common ancestor, and no base revision"\
 
288
            " specified."
 
289
        BzrCommandError.__init__(self, msg)
 
290
 
 
291
class NoCommonAncestor(BzrError):
 
292
    def __init__(self, revision_a, revision_b):
 
293
        msg = "Revisions have no common ancestor: %s %s." \
 
294
            % (revision_a, revision_b) 
 
295
        BzrError.__init__(self, msg)
 
296
 
 
297
class NoCommonRoot(BzrError):
 
298
    def __init__(self, revision_a, revision_b):
 
299
        msg = "Revisions are not derived from the same root: %s %s." \
 
300
            % (revision_a, revision_b) 
 
301
        BzrError.__init__(self, msg)
 
302
 
 
303
class NotAncestor(BzrError):
 
304
    def __init__(self, rev_id, not_ancestor_id):
 
305
        msg = "Revision %s is not an ancestor of %s" % (not_ancestor_id, 
 
306
                                                        rev_id)
 
307
        BzrError.__init__(self, msg)
 
308
        self.rev_id = rev_id
 
309
        self.not_ancestor_id = not_ancestor_id
 
310
 
 
311
 
 
312
class InstallFailed(BzrError):
 
313
    def __init__(self, revisions):
 
314
        msg = "Could not install revisions:\n%s" % " ,".join(revisions)
 
315
        BzrError.__init__(self, msg)
 
316
        self.revisions = revisions
 
317
 
 
318
 
 
319
class AmbiguousBase(BzrError):
 
320
    def __init__(self, bases):
 
321
        msg = "The correct base is unclear, becase %s are all equally close" %\
 
322
            ", ".join(bases)
 
323
        BzrError.__init__(self, msg)
 
324
        self.bases = bases
 
325
 
 
326
class NoCommits(BzrError):
 
327
    def __init__(self, branch):
 
328
        msg = "Branch %s has no commits." % branch
 
329
        BzrError.__init__(self, msg)
 
330
 
 
331
class UnlistableStore(BzrError):
 
332
    def __init__(self, store):
 
333
        BzrError.__init__(self, "Store %s is not listable" % store)
 
334
 
 
335
class UnlistableBranch(BzrError):
 
336
    def __init__(self, br):
 
337
        BzrError.__init__(self, "Stores for branch %s are not listable" % br)
 
338
 
 
339
 
 
340
class WeaveError(BzrNewError):
 
341
    """Error in processing weave: %(message)s"""
 
342
    def __init__(self, message=None):
 
343
        BzrNewError.__init__(self)
 
344
        self.message = message
 
345
 
 
346
 
 
347
class WeaveRevisionAlreadyPresent(WeaveError):
 
348
    """Revision {%(revision_id)s} already present in %(weave)s"""
 
349
    def __init__(self, revision_id, weave):
 
350
        WeaveError.__init__(self)
 
351
        self.revision_id = revision_id
 
352
        self.weave = weave
 
353
 
 
354
 
 
355
class WeaveRevisionNotPresent(WeaveError):
 
356
    """Revision {%(revision_id)s} not present in %(weave)s"""
 
357
    def __init__(self, revision_id, weave):
 
358
        WeaveError.__init__(self)
 
359
        self.revision_id = revision_id
 
360
        self.weave = weave
 
361
 
 
362
 
 
363
class WeaveFormatError(WeaveError):
 
364
    """Weave invariant violated: %(what)s"""
 
365
    def __init__(self, what):
 
366
        WeaveError.__init__(self)
 
367
        self.what = what
 
368
 
 
369
 
 
370
class WeaveParentMismatch(WeaveError):
 
371
    """Parents are mismatched between two revisions."""
 
372
    
 
373
 
 
374
class WeaveInvalidChecksum(WeaveError):
 
375
    """Text did not match it's checksum: %(message)s"""
 
376
 
 
377
 
 
378
class NoSuchExportFormat(BzrNewError):
 
379
    """Export format %(format)r not supported"""
 
380
    def __init__(self, format):
 
381
        BzrNewError.__init__(self)
 
382
        self.format = format
 
383
 
 
384
 
 
385
class TransportError(BzrError):
 
386
    """All errors thrown by Transport implementations should derive
 
387
    from this class.
 
388
    """
 
389
    def __init__(self, msg=None, orig_error=None):
 
390
        if msg is None and orig_error is not None:
 
391
            msg = str(orig_error)
 
392
        BzrError.__init__(self, msg)
 
393
        self.msg = msg
 
394
        self.orig_error = orig_error
 
395
 
 
396
# A set of semi-meaningful errors which can be thrown
 
397
class TransportNotPossible(TransportError):
 
398
    """This is for transports where a specific function is explicitly not
 
399
    possible. Such as pushing files to an HTTP server.
 
400
    """
 
401
    pass
 
402
 
 
403
 
 
404
class ConnectionError(TransportError):
 
405
    """A connection problem prevents file retrieval.
 
406
    This does not indicate whether the file exists or not; it indicates that a
 
407
    precondition for requesting the file was not met.
 
408
    """
 
409
    def __init__(self, msg=None, orig_error=None):
 
410
        TransportError.__init__(self, msg=msg, orig_error=orig_error)
 
411
 
 
412
 
 
413
class ConnectionReset(TransportError):
 
414
    """The connection has been closed."""
 
415
    pass
 
416
 
 
417
class ConflictsInTree(BzrError):
 
418
    def __init__(self):
 
419
        BzrError.__init__(self, "Working tree has conflicts.")
 
420
 
 
421
class ParseConfigError(BzrError):
 
422
    def __init__(self, errors, filename):
 
423
        if filename is None:
 
424
            filename = ""
 
425
        message = "Error(s) parsing config file %s:\n%s" % \
 
426
            (filename, ('\n'.join(e.message for e in errors)))
 
427
        BzrError.__init__(self, message)
 
428
 
 
429
class SigningFailed(BzrError):
 
430
    def __init__(self, command_line):
 
431
        BzrError.__init__(self, "Failed to gpg sign data with command '%s'"
 
432
                               % command_line)
 
433
 
 
434
class WorkingTreeNotRevision(BzrError):
 
435
    def __init__(self, tree):
 
436
        BzrError.__init__(self, "The working tree for %s has changed since"
 
437
                          " last commit, but weave merge requires that it be"
 
438
                          " unchanged." % tree.basedir)
 
439
 
 
440
class CantReprocessAndShowBase(BzrNewError):
 
441
    """Can't reprocess and show base.
 
442
Reprocessing obscures relationship of conflicting lines to base."""
 
443
 
 
444
class GraphCycleError(BzrNewError):
 
445
    """Cycle in graph %(graph)r"""
 
446
    def __init__(self, graph):
 
447
        BzrNewError.__init__(self)
 
448
        self.graph = graph
 
449
 
 
450
class NotConflicted(BzrNewError):
 
451
    """File %(filename)s is not conflicted."""
 
452
    def __init__(self, filename):
 
453
        BzrNewError.__init__(self)
 
454
        self.filename = filename
 
455
 
 
456
class MustUseDecorated(Exception):
 
457
    """A decorating function has requested its original command be used.
 
458
    
 
459
    This should never escape bzr, so does not need to be printable.
 
460
    """
 
461
 
 
462
class MissingText(BzrNewError):
 
463
    """Branch %(base)s is missing revision %(text_revision)s of %(file_id)s"""
 
464
    def __init__(self, branch, text_revision, file_id):
 
465
        self.branch = branch
 
466
        self.base = branch.base
 
467
        self.text_revision = text_revision
 
468
        self.file_id = file_id