17
17
"""Exceptions for bzr, and reporting of them.
19
There are 3 different classes of error:
21
* KeyboardInterrupt, and OSError with EPIPE - the program terminates
22
with an appropriate short message
24
* User errors, indicating a problem caused by the user such as a bad URL.
25
These are printed in a short form.
27
* Internal unexpected errors, including most Python builtin errors
28
and some raised from inside bzr. These are printed with a full
29
traceback and an invitation to report the bug.
19
31
Exceptions are caught at a high level to report errors to the user, and
20
32
might also be caught inside the program. Therefore it needs to be
21
33
possible to convert them to a meaningful string, and also for them to be
46
58
* create a new exception class for any class of error that can be
47
usefully distinguished.
49
* the printable form of an exception is generated by the base class
52
Exception strings should start with a capital letter and not have a final
59
usefully distinguished. If no callers are likely to want to catch
60
one but not another, don't worry about them.
62
* the __str__ method should generate something useful; BzrError provides
63
a good default implementation
65
Exception strings should start with a capital letter and should not have a
56
69
from warnings import warn
166
class BzrCommandError(BzrError):
187
class BzrCommandError(BzrNewError):
188
"""Error from user command"""
167
190
# Error from malformed user command
168
191
# This is being misused as a generic exception
169
192
# pleae subclass. RBC 20051030
172
195
# are not intended to be caught anyway. UI code need not subclass
173
196
# BzrCommandError, and non-UI code should not throw a subclass of
174
197
# BzrCommandError. ADHB 20051211
198
def __init__(self, msg):
175
201
def __str__(self):
179
class BzrOptionError(BzrCommandError):
180
"""Some missing or otherwise incorrect option was supplied."""
183
class StrictCommitFailed(Exception):
205
class StrictCommitFailed(BzrNewError):
184
206
"""Commit refused because there are unknowns in the tree."""
201
223
class NoSuchFile(PathError):
202
"""No such file: %(path)r%(extra)s"""
224
"""No such file: %(path)s%(extra)s"""
205
227
class FileExists(PathError):
206
"""File exists: %(path)r%(extra)s"""
228
"""File exists: %(path)s%(extra)s"""
209
231
class DirectoryNotEmpty(PathError):
210
"""Directory not empty: %(path)r%(extra)s"""
232
"""Directory not empty: %(path)s%(extra)s"""
213
235
class ResourceBusy(PathError):
214
"""Device or resource busy: %(path)r%(extra)s"""
236
"""Device or resource busy: %(path)s%(extra)s"""
217
239
class PermissionDenied(PathError):
218
"""Permission denied: %(path)r%(extra)s"""
240
"""Permission denied: %(path)s%(extra)s"""
221
243
class PathNotChild(BzrNewError):
222
"""Path %(path)r is not a child of path %(base)r%(extra)s"""
244
"""Path %(path)s is not a child of path %(base)s%(extra)s"""
246
is_user_error = False
223
248
def __init__(self, path, base, extra=None):
224
249
BzrNewError.__init__(self)
246
271
class NoRepositoryPresent(BzrNewError):
247
"""No repository present: %(path)r"""
272
"""No repository present: %(path)s"""
248
273
def __init__(self, bzrdir):
249
274
BzrNewError.__init__(self)
250
275
self.path = bzrdir.transport.clone('..').base
263
class UnsupportedFormatError(BzrError):
264
"""Specified path is a bzr branch that we recognize but cannot read."""
266
return 'unsupported branch format: %s' % self.args[0]
269
class UnknownFormatError(BzrError):
270
"""Specified path is a bzr branch whose format we do not recognize."""
272
return 'unknown branch format: %s' % self.args[0]
288
class UnsupportedFormatError(BzrNewError):
289
"""Unsupported branch format: %(format)s"""
292
class UnknownFormatError(BzrNewError):
293
"""Unknown branch format: %(format)s"""
275
296
class IncompatibleFormat(BzrNewError):
359
380
class ObjectNotLocked(LockError):
360
"""%(obj)r is not locked"""
381
"""%(obj)s is not locked"""
382
is_user_error = False
361
383
# this can indicate that any particular object is not locked; see also
362
384
# LockNotHeld which means that a particular *lock* object is not held by
363
385
# the caller -- perhaps they should be unified.
393
415
class LockBreakMismatch(LockError):
394
"""Lock was released and re-acquired before being broken: %(lock)s: held by %(holder)r, wanted to break %(target)r"""
416
"""Lock was released and re-acquired before being broken: %(lock)s: held by %(holder)s, wanted to break %(target)r"""
395
417
def __init__(self, lock, holder, target):
397
419
self.holder = holder
425
447
"""Commit refused because there are unknowns in the tree."""
428
class NoSuchRevision(BzrError):
450
class NoSuchRevision(BzrNewError):
451
"""Branch %(branch)s has no revision %(revision)s"""
453
is_user_error = False
429
455
def __init__(self, branch, revision):
430
456
self.branch = branch
431
457
self.revision = revision
432
msg = "Branch %s has no revision %s" % (branch, revision)
433
BzrError.__init__(self, msg)
436
class HistoryMissing(BzrError):
437
def __init__(self, branch, object_type, object_id):
439
BzrError.__init__(self,
440
'%s is missing %s {%s}'
441
% (branch, object_type, object_id))
444
class DivergedBranches(BzrError):
460
class DivergedBranches(BzrNewError):
461
"These branches have diverged. Use the merge command to reconcile them."""
446
465
def __init__(self, branch1, branch2):
447
BzrError.__init__(self, "These branches have diverged. Try merge.")
448
466
self.branch1 = branch1
449
467
self.branch2 = branch2
452
class UnrelatedBranches(BzrCommandError):
454
msg = "Branches have no common ancestor, and no base revision"\
456
BzrCommandError.__init__(self, msg)
459
class NoCommonAncestor(BzrError):
470
class UnrelatedBranches(BzrNewError):
471
"Branches have no common ancestor, and no merge base revision was specified."
476
class NoCommonAncestor(BzrNewError):
477
"Revisions have no common ancestor: %(revision_a)s %(revision_b)s"
460
479
def __init__(self, revision_a, revision_b):
461
msg = "Revisions have no common ancestor: %s %s." \
462
% (revision_a, revision_b)
463
BzrError.__init__(self, msg)
480
self.revision_a = revision_a
481
self.revision_b = revision_b
466
484
class NoCommonRoot(BzrError):