1
1
# Copyright (C) 2005, 2006 Canonical
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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
71
from bzrlib.patches import (PatchSyntax,
58
78
# based on Scott James Remnant's hct error classes
60
80
# TODO: is there any value in providing the .args field used by standard
100
127
def __str__(self):
102
return self.__doc__ % self.__dict__
103
except (NameError, ValueError, KeyError), e:
104
return 'Unprintable exception %s: %s' \
105
% (self.__class__.__name__, str(e))
129
# __str__() should always return a 'str' object
130
# never a 'unicode' object.
131
s = self.__doc__ % self.__dict__
132
if isinstance(s, unicode):
133
return s.encode('utf8')
135
except (TypeError, NameError, ValueError, KeyError), e:
136
return 'Unprintable exception %s(%r): %s' \
137
% (self.__class__.__name__,
138
self.__dict__, str(e))
141
class AlreadyBuilding(BzrNewError):
142
"""The tree builder is already building a tree."""
108
145
class BzrCheckError(BzrNewError):
109
146
"""Internal check failed: %(message)s"""
148
is_user_error = False
111
150
def __init__(self, message):
112
151
BzrNewError.__init__(self)
113
152
self.message = message
130
172
class InvalidRevisionId(BzrNewError):
131
173
"""Invalid revision-id {%(revision_id)s} in %(branch)s"""
132
175
def __init__(self, revision_id, branch):
176
# branch can be any string or object with __str__ defined
133
177
BzrNewError.__init__(self)
134
178
self.revision_id = revision_id
135
179
self.branch = branch
182
class NoSuchId(BzrNewError):
183
"""The file id %(file_id)s is not present in the tree %(tree)s."""
185
def __init__(self, tree, file_id):
186
BzrNewError.__init__(self)
187
self.file_id = file_id
138
191
class NoWorkingTree(BzrNewError):
139
"""No WorkingTree exists for %s(base)."""
192
"""No WorkingTree exists for %(base)s."""
141
194
def __init__(self, base):
142
195
BzrNewError.__init__(self)
199
class NotBuilding(BzrNewError):
200
"""Not currently building a tree."""
146
203
class NotLocalUrl(BzrNewError):
147
"""%s(url) is not a local path."""
204
"""%(url)s is not a local path."""
149
206
def __init__(self, url):
150
207
BzrNewError.__init__(self)
154
class BzrCommandError(BzrError):
155
# Error from malformed user command
156
# This is being misused as a generic exception
157
# pleae subclass. RBC 20051030
211
class BzrCommandError(BzrNewError):
212
"""Error from user command"""
216
# Error from malformed user command; please avoid raising this as a
217
# generic exception not caused by user input.
159
219
# I think it's a waste of effort to differentiate between errors that
160
220
# are not intended to be caught anyway. UI code need not subclass
161
221
# BzrCommandError, and non-UI code should not throw a subclass of
162
222
# BzrCommandError. ADHB 20051211
223
def __init__(self, msg):
224
# Object.__str__() must return a real string
225
# returning a Unicode string is a python error.
226
if isinstance(msg, unicode):
227
self.msg = msg.encode('utf8')
163
231
def __str__(self):
167
235
class BzrOptionError(BzrCommandError):
168
"""Some missing or otherwise incorrect option was supplied."""
236
"""Error in command line options"""
171
class StrictCommitFailed(Exception):
172
"""Commit refused because there are unknowns in the tree."""
239
class StrictCommitFailed(BzrNewError):
240
"""Commit refused because there are unknown files in the tree"""
243
# XXX: Should be unified with TransportError; they seem to represent the
175
245
class PathError(BzrNewError):
176
246
"""Generic path error: %(path)r%(extra)s)"""
204
274
"""Permission denied: %(path)r%(extra)s"""
277
class InvalidURL(PathError):
278
"""Invalid url supplied to transport: %(path)r%(extra)s"""
281
class InvalidURLJoin(PathError):
282
"""Invalid URL join request: %(args)s%(extra)s"""
284
def __init__(self, msg, base, args):
285
PathError.__init__(self, base, msg)
287
self.args.extend(args)
290
class UnsupportedProtocol(PathError):
291
"""Unsupported protocol for url "%(path)s"%(extra)s"""
293
def __init__(self, url, extra):
294
PathError.__init__(self, url, extra=extra)
297
class ShortReadvError(PathError):
298
"""readv() read %(actual)s bytes rather than %(length)s bytes at %(offset)s for %(path)s%(extra)s"""
300
is_user_error = False
302
def __init__(self, path, offset, length, actual, extra=None):
303
PathError.__init__(self, path, extra=extra)
207
309
class PathNotChild(BzrNewError):
208
310
"""Path %(path)r is not a child of path %(base)r%(extra)s"""
312
is_user_error = False
209
314
def __init__(self, path, base, extra=None):
210
315
BzrNewError.__init__(self)
324
class InvalidNormalization(PathError):
325
"""Path %(path)r is not unicode normalized"""
328
# TODO: This is given a URL; we try to unescape it but doing that from inside
329
# the exception object is a bit undesirable.
330
# TODO: Probably this behavior of should be a common superclass
219
331
class NotBranchError(PathError):
220
332
"""Not a branch: %(path)s"""
334
def __init__(self, path):
335
import bzrlib.urlutils as urlutils
336
self.path = urlutils.unescape_for_display(path, 'ascii')
223
339
class AlreadyBranchError(PathError):
224
"""Already a branch: %(path)s. Use `bzr checkout` to build a working tree."""
340
"""Already a branch: %(path)s."""
343
class BranchExistsWithoutWorkingTree(PathError):
344
"""Directory contains a branch, but no working tree \
345
(use bzr checkout if you wish to build a working tree): %(path)s"""
348
class AtomicFileAlreadyClosed(PathError):
349
"""'%(function)s' called on an AtomicFile after it was closed: %(path)s"""
351
def __init__(self, path, function):
352
PathError.__init__(self, path=path, extra=None)
353
self.function = function
356
class InaccessibleParent(PathError):
357
"""Parent not accessible given base %(base)s and relative path %(path)s"""
359
def __init__(self, path, base):
360
PathError.__init__(self, path)
227
364
class NoRepositoryPresent(BzrNewError):
228
"""Not repository present: %(path)r"""
365
"""No repository present: %(path)r"""
229
366
def __init__(self, bzrdir):
230
367
BzrNewError.__init__(self)
231
368
self.path = bzrdir.transport.clone('..').base
280
413
self.paths_as_string = ' '.join([quotefn(p) for p in paths])
283
class BadFileKindError(BzrError):
284
"""Specified file is of a kind that cannot be added.
286
(For example a symlink or device file.)"""
289
class ForbiddenFileError(BzrError):
290
"""Cannot operate on a file because it is a control file."""
416
class PathsDoNotExist(BzrNewError):
417
"""Path(s) do not exist: %(paths_as_string)s"""
419
# used when reporting that paths are neither versioned nor in the working
422
def __init__(self, paths):
424
from bzrlib.osutils import quotefn
425
BzrNewError.__init__(self)
427
self.paths_as_string = ' '.join([quotefn(p) for p in paths])
430
class BadFileKindError(BzrNewError):
431
"""Cannot operate on %(filename)s of unsupported kind %(kind)s"""
434
class ForbiddenControlFileError(BzrNewError):
435
"""Cannot operate on %(filename)s because it is a control file"""
293
438
class LockError(BzrNewError):
387
535
self.format = format
391
538
class StrictCommitFailed(Exception):
392
539
"""Commit refused because there are unknowns in the tree."""
395
class NoSuchRevision(BzrError):
542
class NoSuchRevision(BzrNewError):
543
"""Branch %(branch)s has no revision %(revision)s"""
545
is_user_error = False
396
547
def __init__(self, branch, revision):
398
self.revision = revision
399
msg = "Branch %s has no revision %s" % (branch, revision)
400
BzrError.__init__(self, msg)
548
BzrNewError.__init__(self, branch=branch, revision=revision)
551
class NoSuchRevisionSpec(BzrNewError):
552
"""No namespace registered for string: %(spec)r"""
554
def __init__(self, spec):
555
BzrNewError.__init__(self, spec=spec)
558
class InvalidRevisionSpec(BzrNewError):
559
"""Requested revision: '%(spec)s' does not exist in branch:
560
%(branch)s%(extra)s"""
562
def __init__(self, spec, branch, extra=None):
563
BzrNewError.__init__(self, branch=branch, spec=spec)
565
self.extra = '\n' + str(extra)
403
570
class HistoryMissing(BzrError):
408
575
% (branch, object_type, object_id))
411
class DivergedBranches(BzrError):
578
class DivergedBranches(BzrNewError):
579
"These branches have diverged. Use the merge command to reconcile them."""
413
583
def __init__(self, branch1, branch2):
414
BzrError.__init__(self, "These branches have diverged. Try merge.")
415
584
self.branch1 = branch1
416
585
self.branch2 = branch2
419
class UnrelatedBranches(BzrCommandError):
421
msg = "Branches have no common ancestor, and no base revision"\
423
BzrCommandError.__init__(self, msg)
426
class NoCommonAncestor(BzrError):
588
class UnrelatedBranches(BzrNewError):
589
"Branches have no common ancestor, and no merge base revision was specified."
594
class NoCommonAncestor(BzrNewError):
595
"Revisions have no common ancestor: %(revision_a)s %(revision_b)s"
427
597
def __init__(self, revision_a, revision_b):
428
msg = "Revisions have no common ancestor: %s %s." \
429
% (revision_a, revision_b)
430
BzrError.__init__(self, msg)
598
self.revision_a = revision_a
599
self.revision_b = revision_b
433
602
class NoCommonRoot(BzrError):
458
627
def __init__(self, bases):
459
628
warn("BzrError AmbiguousBase has been deprecated as of bzrlib 0.8.",
460
629
DeprecationWarning)
461
msg = "The correct base is unclear, becase %s are all equally close" %\
630
msg = "The correct base is unclear, because %s are all equally close" %\
463
632
BzrError.__init__(self, msg)
464
633
self.bases = bases
467
class NoCommits(BzrError):
636
class NoCommits(BzrNewError):
637
"""Branch %(branch)s has no commits."""
468
639
def __init__(self, branch):
469
msg = "Branch %s has no commits." % branch
470
BzrError.__init__(self, msg)
640
BzrNewError.__init__(self, branch=branch)
473
643
class UnlistableStore(BzrError):
626
796
self.format = format
629
class TransportError(BzrError):
630
"""All errors thrown by Transport implementations should derive
799
class TransportError(BzrNewError):
800
"""Transport error: %(msg)s %(orig_error)s"""
633
802
def __init__(self, msg=None, orig_error=None):
634
803
if msg is None and orig_error is not None:
635
804
msg = str(orig_error)
636
BzrError.__init__(self, msg)
805
if orig_error is None:
638
810
self.orig_error = orig_error
811
BzrNewError.__init__(self)
814
class SmartProtocolError(TransportError):
815
"""Generic bzr smart protocol error: %(details)s"""
817
def __init__(self, details):
818
self.details = details
641
821
# A set of semi-meaningful errors which can be thrown
642
822
class TransportNotPossible(TransportError):
643
"""This is for transports where a specific function is explicitly not
644
possible. Such as pushing files to an HTTP server.
823
"""Transport operation not possible: %(msg)s %(orig_error)%"""
649
826
class ConnectionError(TransportError):
650
"""A connection problem prevents file retrieval.
651
This does not indicate whether the file exists or not; it indicates that a
652
precondition for requesting the file was not met.
654
def __init__(self, msg=None, orig_error=None):
655
TransportError.__init__(self, msg=msg, orig_error=orig_error)
827
"""Connection error: %(msg)s %(orig_error)s"""
658
830
class ConnectionReset(TransportError):
659
"""The connection has been closed."""
831
"""Connection closed: %(msg)s %(orig_error)s"""
834
class InvalidRange(TransportError):
835
"""Invalid range access in %(path)s at %(offset)s."""
837
def __init__(self, path, offset):
838
TransportError.__init__(self, ("Invalid range access in %s at %d"
844
class InvalidHttpResponse(TransportError):
845
"""Invalid http response for %(path)s: %(msg)s"""
847
def __init__(self, path, msg, orig_error=None):
849
TransportError.__init__(self, msg, orig_error=orig_error)
852
class InvalidHttpRange(InvalidHttpResponse):
853
"""Invalid http range "%(range)s" for %(path)s: %(msg)s"""
855
def __init__(self, path, range, msg):
857
InvalidHttpResponse.__init__(self, path, msg)
860
class InvalidHttpContentType(InvalidHttpResponse):
861
"""Invalid http Content-type "%(ctype)s" for %(path)s: %(msg)s"""
863
def __init__(self, path, ctype, msg):
865
InvalidHttpResponse.__init__(self, path, msg)
663
868
class ConflictsInTree(BzrError):
851
1099
"""A nested progress bar was not 'finished' correctly."""
1102
class InvalidProgressBarType(BzrNewError):
1103
"""Environment variable BZR_PROGRESS_BAR='%(bar_type)s is not a supported type
1104
Select one of: %(valid_types)s"""
1106
def __init__(self, bar_type, valid_types):
1107
BzrNewError.__init__(self, bar_type=bar_type, valid_types=valid_types)
854
1110
class UnsupportedOperation(BzrNewError):
855
1111
"""The method %(mname)s is not supported on objects of type %(tname)s."""
856
1112
def __init__(self, method, method_self):
857
1113
self.method = method
858
1114
self.mname = method.__name__
859
1115
self.tname = type(method_self).__name__
1118
class BinaryFile(BzrNewError):
1119
"""File is binary but should be text."""
1122
class IllegalPath(BzrNewError):
1123
"""The path %(path)s is not permitted on this platform"""
1125
def __init__(self, path):
1126
BzrNewError.__init__(self)
1130
class TestamentMismatch(BzrNewError):
1131
"""Testament did not match expected value.
1132
For revision_id {%(revision_id)s}, expected {%(expected)s}, measured
1135
def __init__(self, revision_id, expected, measured):
1136
self.revision_id = revision_id
1137
self.expected = expected
1138
self.measured = measured
1141
class NotABundle(BzrNewError):
1142
"""Not a bzr revision-bundle: %(text)r"""
1144
def __init__(self, text):
1145
BzrNewError.__init__(self)
1149
class BadBundle(BzrNewError):
1150
"""Bad bzr revision-bundle: %(text)r"""
1152
def __init__(self, text):
1153
BzrNewError.__init__(self)
1157
class MalformedHeader(BadBundle):
1158
"""Malformed bzr revision-bundle header: %(text)r"""
1160
def __init__(self, text):
1161
BzrNewError.__init__(self)
1165
class MalformedPatches(BadBundle):
1166
"""Malformed patches in bzr revision-bundle: %(text)r"""
1168
def __init__(self, text):
1169
BzrNewError.__init__(self)
1173
class MalformedFooter(BadBundle):
1174
"""Malformed footer in bzr revision-bundle: %(text)r"""
1176
def __init__(self, text):
1177
BzrNewError.__init__(self)
1181
class UnsupportedEOLMarker(BadBundle):
1182
"""End of line marker was not \\n in bzr revision-bundle"""
1185
BzrNewError.__init__(self)
1188
class BadInventoryFormat(BzrNewError):
1189
"""Root class for inventory serialization errors"""
1192
class UnexpectedInventoryFormat(BadInventoryFormat):
1193
"""The inventory was not in the expected format:\n %(msg)s"""
1195
def __init__(self, msg):
1196
BadInventoryFormat.__init__(self, msg=msg)
1199
class NoSmartServer(NotBranchError):
1200
"""No smart server available at %(url)s"""
1202
def __init__(self, url):
1206
class UnknownSSH(BzrNewError):
1207
"""Unrecognised value for BZR_SSH environment variable: %(vendor)s"""
1209
def __init__(self, vendor):
1210
BzrNewError.__init__(self)
1211
self.vendor = vendor
1214
class GhostRevisionUnusableHere(BzrNewError):
1215
"""Ghost revision {%(revision_id)s} cannot be used here."""
1217
def __init__(self, revision_id):
1218
BzrNewError.__init__(self)
1219
self.revision_id = revision_id
1222
class IllegalUseOfScopeReplacer(BzrNewError):
1223
"""ScopeReplacer object %(name)r was used incorrectly: %(msg)s%(extra)s"""
1225
is_user_error = False
1227
def __init__(self, name, msg, extra=None):
1228
BzrNewError.__init__(self)
1232
self.extra = ': ' + str(extra)
1237
class InvalidImportLine(BzrNewError):
1238
"""Not a valid import statement: %(msg)\n%(text)s"""
1240
is_user_error = False
1242
def __init__(self, text, msg):
1243
BzrNewError.__init__(self)
1248
class ImportNameCollision(BzrNewError):
1249
"""Tried to import an object to the same name as an existing object. %(name)s"""
1251
is_user_error = False
1253
def __init__(self, name):
1254
BzrNewError.__init__(self)