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))
108
141
class BzrCheckError(BzrNewError):
109
142
"""Internal check failed: %(message)s"""
144
is_user_error = False
111
146
def __init__(self, message):
112
147
BzrNewError.__init__(self)
113
148
self.message = message
130
168
class InvalidRevisionId(BzrNewError):
131
169
"""Invalid revision-id {%(revision_id)s} in %(branch)s"""
132
171
def __init__(self, revision_id, branch):
172
# branch can be any string or object with __str__ defined
133
173
BzrNewError.__init__(self)
134
174
self.revision_id = revision_id
135
175
self.branch = branch
178
class NoSuchId(BzrNewError):
179
"""The file id %(file_id)s is not present in the tree %(tree)s."""
181
def __init__(self, tree, file_id):
182
BzrNewError.__init__(self)
183
self.file_id = file_id
138
187
class NoWorkingTree(BzrNewError):
139
"""No WorkingTree exists for %s(base)."""
188
"""No WorkingTree exists for %(base)s."""
141
190
def __init__(self, base):
142
191
BzrNewError.__init__(self)
146
195
class NotLocalUrl(BzrNewError):
147
"""%s(url) is not a local path."""
196
"""%(url)s is not a local path."""
149
198
def __init__(self, url):
150
199
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
203
class BzrCommandError(BzrNewError):
204
"""Error from user command"""
208
# Error from malformed user command; please avoid raising this as a
209
# generic exception not caused by user input.
159
211
# I think it's a waste of effort to differentiate between errors that
160
212
# are not intended to be caught anyway. UI code need not subclass
161
213
# BzrCommandError, and non-UI code should not throw a subclass of
162
214
# BzrCommandError. ADHB 20051211
215
def __init__(self, msg):
216
# Object.__str__() must return a real string
217
# returning a Unicode string is a python error.
218
if isinstance(msg, unicode):
219
self.msg = msg.encode('utf8')
163
223
def __str__(self):
167
227
class BzrOptionError(BzrCommandError):
168
"""Some missing or otherwise incorrect option was supplied."""
228
"""Error in command line options"""
171
class StrictCommitFailed(Exception):
172
"""Commit refused because there are unknowns in the tree."""
231
class StrictCommitFailed(BzrNewError):
232
"""Commit refused because there are unknown files in the tree"""
235
# XXX: Should be unified with TransportError; they seem to represent the
175
237
class PathError(BzrNewError):
176
238
"""Generic path error: %(path)r%(extra)s)"""
204
266
"""Permission denied: %(path)r%(extra)s"""
269
class InvalidURL(PathError):
270
"""Invalid url supplied to transport: %(path)r%(extra)s"""
273
class InvalidURLJoin(PathError):
274
"""Invalid URL join request: %(args)s%(extra)s"""
276
def __init__(self, msg, base, args):
277
PathError.__init__(self, base, msg)
279
self.args.extend(args)
282
class UnsupportedProtocol(PathError):
283
"""Unsupported protocol for url "%(path)s"%(extra)s"""
285
def __init__(self, url, extra):
286
PathError.__init__(self, url, extra=extra)
207
289
class PathNotChild(BzrNewError):
208
290
"""Path %(path)r is not a child of path %(base)r%(extra)s"""
292
is_user_error = False
209
294
def __init__(self, path, base, extra=None):
210
295
BzrNewError.__init__(self)
304
class InvalidNormalization(PathError):
305
"""Path %(path)r is not unicode normalized"""
308
# TODO: This is given a URL; we try to unescape it but doing that from inside
309
# the exception object is a bit undesirable.
310
# TODO: Probably this behavior of should be a common superclass
219
311
class NotBranchError(PathError):
220
312
"""Not a branch: %(path)s"""
314
def __init__(self, path):
315
import bzrlib.urlutils as urlutils
316
self.path = urlutils.unescape_for_display(path, 'ascii')
223
319
class AlreadyBranchError(PathError):
224
"""Already a branch: %(path)s. Use `bzr checkout` to build a working tree."""
320
"""Already a branch: %(path)s."""
323
class BranchExistsWithoutWorkingTree(PathError):
324
"""Directory contains a branch, but no working tree \
325
(use bzr checkout if you wish to build a working tree): %(path)s"""
328
class AtomicFileAlreadyClosed(PathError):
329
"""'%(function)s' called on an AtomicFile after it was closed: %(path)s"""
331
def __init__(self, path, function):
332
PathError.__init__(self, path=path, extra=None)
333
self.function = function
336
class InaccessibleParent(PathError):
337
"""Parent not accessible given base %(base)s and relative path %(path)s"""
339
def __init__(self, path, base):
340
PathError.__init__(self, path)
227
344
class NoRepositoryPresent(BzrNewError):
228
"""Not repository present: %(path)r"""
345
"""No repository present: %(path)r"""
229
346
def __init__(self, bzrdir):
230
347
BzrNewError.__init__(self)
231
348
self.path = bzrdir.transport.clone('..').base
280
393
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."""
396
class PathsDoNotExist(BzrNewError):
397
"""Path(s) do not exist: %(paths_as_string)s"""
399
# used when reporting that paths are neither versioned nor in the working
402
def __init__(self, paths):
404
from bzrlib.osutils import quotefn
405
BzrNewError.__init__(self)
407
self.paths_as_string = ' '.join([quotefn(p) for p in paths])
410
class BadFileKindError(BzrNewError):
411
"""Cannot operate on %(filename)s of unsupported kind %(kind)s"""
414
class ForbiddenControlFileError(BzrNewError):
415
"""Cannot operate on %(filename)s because it is a control file"""
293
418
class LockError(BzrNewError):
387
515
self.format = format
391
518
class StrictCommitFailed(Exception):
392
519
"""Commit refused because there are unknowns in the tree."""
395
class NoSuchRevision(BzrError):
522
class NoSuchRevision(BzrNewError):
523
"""Branch %(branch)s has no revision %(revision)s"""
525
is_user_error = False
396
527
def __init__(self, branch, revision):
398
self.revision = revision
399
msg = "Branch %s has no revision %s" % (branch, revision)
400
BzrError.__init__(self, msg)
528
BzrNewError.__init__(self, branch=branch, revision=revision)
531
class NoSuchRevisionSpec(BzrNewError):
532
"""No namespace registered for string: %(spec)r"""
534
def __init__(self, spec):
535
BzrNewError.__init__(self, spec=spec)
538
class InvalidRevisionSpec(BzrNewError):
539
"""Requested revision: '%(spec)s' does not exist in branch:
540
%(branch)s%(extra)s"""
542
def __init__(self, spec, branch, extra=None):
543
BzrNewError.__init__(self, branch=branch, spec=spec)
545
self.extra = '\n' + str(extra)
403
550
class HistoryMissing(BzrError):
408
555
% (branch, object_type, object_id))
411
class DivergedBranches(BzrError):
558
class DivergedBranches(BzrNewError):
559
"These branches have diverged. Use the merge command to reconcile them."""
413
563
def __init__(self, branch1, branch2):
414
BzrError.__init__(self, "These branches have diverged. Try merge.")
415
564
self.branch1 = branch1
416
565
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):
568
class UnrelatedBranches(BzrNewError):
569
"Branches have no common ancestor, and no merge base revision was specified."
574
class NoCommonAncestor(BzrNewError):
575
"Revisions have no common ancestor: %(revision_a)s %(revision_b)s"
427
577
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)
578
self.revision_a = revision_a
579
self.revision_b = revision_b
433
582
class NoCommonRoot(BzrError):
458
607
def __init__(self, bases):
459
608
warn("BzrError AmbiguousBase has been deprecated as of bzrlib 0.8.",
460
609
DeprecationWarning)
461
msg = "The correct base is unclear, becase %s are all equally close" %\
610
msg = "The correct base is unclear, because %s are all equally close" %\
463
612
BzrError.__init__(self, msg)
464
613
self.bases = bases
467
class NoCommits(BzrError):
616
class NoCommits(BzrNewError):
617
"""Branch %(branch)s has no commits."""
468
619
def __init__(self, branch):
469
msg = "Branch %s has no commits." % branch
470
BzrError.__init__(self, msg)
620
BzrNewError.__init__(self, branch=branch)
473
623
class UnlistableStore(BzrError):
626
776
self.format = format
629
class TransportError(BzrError):
630
"""All errors thrown by Transport implementations should derive
779
class TransportError(BzrNewError):
780
"""Transport error: %(msg)s %(orig_error)s"""
633
782
def __init__(self, msg=None, orig_error=None):
634
783
if msg is None and orig_error is not None:
635
784
msg = str(orig_error)
636
BzrError.__init__(self, msg)
785
if orig_error is None:
638
790
self.orig_error = orig_error
791
BzrNewError.__init__(self)
641
794
# A set of semi-meaningful errors which can be thrown
642
795
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.
796
"""Transport operation not possible: %(msg)s %(orig_error)%"""
649
799
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)
800
"""Connection error: %(msg)s %(orig_error)s"""
658
803
class ConnectionReset(TransportError):
659
"""The connection has been closed."""
804
"""Connection closed: %(msg)s %(orig_error)s"""
807
class InvalidRange(TransportError):
808
"""Invalid range access in %(path)s at %(offset)s."""
810
def __init__(self, path, offset):
811
TransportError.__init__(self, ("Invalid range access in %s at %d"
817
class InvalidHttpResponse(TransportError):
818
"""Invalid http response for %(path)s: %(msg)s"""
820
def __init__(self, path, msg, orig_error=None):
822
TransportError.__init__(self, msg, orig_error=orig_error)
825
class InvalidHttpRange(InvalidHttpResponse):
826
"""Invalid http range "%(range)s" for %(path)s: %(msg)s"""
828
def __init__(self, path, range, msg):
830
InvalidHttpResponse.__init__(self, path, msg)
833
class InvalidHttpContentType(InvalidHttpResponse):
834
"""Invalid http Content-type "%(ctype)s" for %(path)s: %(msg)s"""
836
def __init__(self, path, ctype, msg):
838
InvalidHttpResponse.__init__(self, path, msg)
663
841
class ConflictsInTree(BzrError):
851
1072
"""A nested progress bar was not 'finished' correctly."""
1075
class InvalidProgressBarType(BzrNewError):
1076
"""Environment variable BZR_PROGRESS_BAR='%(bar_type)s is not a supported type
1077
Select one of: %(valid_types)s"""
1079
def __init__(self, bar_type, valid_types):
1080
BzrNewError.__init__(self, bar_type=bar_type, valid_types=valid_types)
854
1083
class UnsupportedOperation(BzrNewError):
855
1084
"""The method %(mname)s is not supported on objects of type %(tname)s."""
856
1085
def __init__(self, method, method_self):
857
1086
self.method = method
858
1087
self.mname = method.__name__
859
1088
self.tname = type(method_self).__name__
1091
class BinaryFile(BzrNewError):
1092
"""File is binary but should be text."""
1095
class IllegalPath(BzrNewError):
1096
"""The path %(path)s is not permitted on this platform"""
1098
def __init__(self, path):
1099
BzrNewError.__init__(self)
1103
class TestamentMismatch(BzrNewError):
1104
"""Testament did not match expected value.
1105
For revision_id {%(revision_id)s}, expected {%(expected)s}, measured
1108
def __init__(self, revision_id, expected, measured):
1109
self.revision_id = revision_id
1110
self.expected = expected
1111
self.measured = measured
1114
class NotABundle(BzrNewError):
1115
"""Not a bzr revision-bundle: %(text)r"""
1117
def __init__(self, text):
1118
BzrNewError.__init__(self)
1122
class BadBundle(BzrNewError):
1123
"""Bad bzr revision-bundle: %(text)r"""
1125
def __init__(self, text):
1126
BzrNewError.__init__(self)
1130
class MalformedHeader(BadBundle):
1131
"""Malformed bzr revision-bundle header: %(text)r"""
1133
def __init__(self, text):
1134
BzrNewError.__init__(self)
1138
class MalformedPatches(BadBundle):
1139
"""Malformed patches in bzr revision-bundle: %(text)r"""
1141
def __init__(self, text):
1142
BzrNewError.__init__(self)
1146
class MalformedFooter(BadBundle):
1147
"""Malformed footer in bzr revision-bundle: %(text)r"""
1149
def __init__(self, text):
1150
BzrNewError.__init__(self)
1154
class UnsupportedEOLMarker(BadBundle):
1155
"""End of line marker was not \\n in bzr revision-bundle"""
1158
BzrNewError.__init__(self)
1161
class BadInventoryFormat(BzrNewError):
1162
"""Root class for inventory serialization errors"""
1165
class UnexpectedInventoryFormat(BadInventoryFormat):
1166
"""The inventory was not in the expected format:\n %(msg)s"""
1168
def __init__(self, msg):
1169
BadInventoryFormat.__init__(self, msg=msg)
1172
class UnknownSSH(BzrNewError):
1173
"""Unrecognised value for BZR_SSH environment variable: %(vendor)s"""
1175
def __init__(self, vendor):
1176
BzrNewError.__init__(self)
1177
self.vendor = vendor
1180
class GhostRevisionUnusableHere(BzrNewError):
1181
"""Ghost revision {%(revision_id)s} cannot be used here."""
1183
def __init__(self, revision_id):
1184
BzrNewError.__init__(self)
1185
self.revision_id = revision_id
1188
class IllegalUseOfScopeReplacer(BzrNewError):
1189
"""ScopeReplacer object %(name)r was used incorrectly: %(msg)s%(extra)s"""
1191
is_user_error = False
1193
def __init__(self, name, msg, extra=None):
1194
BzrNewError.__init__(self)
1198
self.extra = ': ' + str(extra)
1203
class InvalidImportLine(BzrNewError):
1204
"""Not a valid import statement: %(msg)\n%(text)s"""
1206
is_user_error = False
1208
def __init__(self, text, msg):
1209
BzrNewError.__init__(self)
1214
class ImportNameCollision(BzrNewError):
1215
"""Tried to import an object to the same name as an existing object. %(name)s"""
1217
is_user_error = False
1219
def __init__(self, name):
1220
BzrNewError.__init__(self)