~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/errors.py

  • Committer: John Arbash Meinel
  • Date: 2006-10-11 00:23:23 UTC
  • mfrom: (2070 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2071.
  • Revision ID: john@arbash-meinel.com-20061011002323-82ba88c293d7caff
[merge] bzr.dev 2070

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
>>> try:
45
45
...   raise NotBranchError(path='/foo/bar')
46
46
... except:
47
 
...   print sys.exc_type
 
47
...   print '%s.%s' % (sys.exc_type.__module__, sys.exc_type.__name__)
48
48
...   print sys.exc_value
49
49
...   path = getattr(sys.exc_value, 'path', None)
50
50
...   if path is not None:
96
96
class BzrError(StandardError):
97
97
    
98
98
    is_user_error = True
99
 
    
 
99
 
100
100
    def __str__(self):
101
101
        # XXX: Should we show the exception class in 
102
102
        # exceptions that don't provide their own message?  
120
120
    # base classes should override the docstring with their human-
121
121
    # readable explanation
122
122
 
123
 
    def __init__(self, **kwds):
 
123
    def __init__(self, *args, **kwds):
 
124
        # XXX: Use the underlying BzrError to always generate the args attribute
 
125
        # if it doesn't exist.  We can't use super here, because exceptions are
 
126
        # old-style classes in python2.4 (but new in 2.5).  --bmc, 20060426
 
127
        BzrError.__init__(self, *args)
124
128
        for key, value in kwds.items():
125
129
            setattr(self, key, value)
126
130
 
127
131
    def __str__(self):
128
132
        try:
129
 
            return self.__doc__ % self.__dict__
130
 
        except (NameError, ValueError, KeyError), e:
131
 
            return 'Unprintable exception %s: %s' \
132
 
                % (self.__class__.__name__, str(e))
 
133
            # __str__() should always return a 'str' object
 
134
            # never a 'unicode' object.
 
135
            s = self.__doc__ % self.__dict__
 
136
            if isinstance(s, unicode):
 
137
                return s.encode('utf8')
 
138
            return s
 
139
        except (TypeError, NameError, ValueError, KeyError), e:
 
140
            return 'Unprintable exception %s(%r): %s' \
 
141
                % (self.__class__.__name__,
 
142
                   self.__dict__, str(e))
 
143
 
 
144
 
 
145
class AlreadyBuilding(BzrNewError):
 
146
    """The tree builder is already building a tree."""
133
147
 
134
148
 
135
149
class BzrCheckError(BzrNewError):
161
175
 
162
176
class InvalidRevisionId(BzrNewError):
163
177
    """Invalid revision-id {%(revision_id)s} in %(branch)s"""
 
178
 
164
179
    def __init__(self, revision_id, branch):
165
180
        # branch can be any string or object with __str__ defined
166
181
        BzrNewError.__init__(self)
168
183
        self.branch = branch
169
184
 
170
185
 
 
186
class NoSuchId(BzrNewError):
 
187
    """The file id %(file_id)s is not present in the tree %(tree)s."""
 
188
    
 
189
    def __init__(self, tree, file_id):
 
190
        BzrNewError.__init__(self)
 
191
        self.file_id = file_id
 
192
        self.tree = tree
 
193
 
 
194
 
171
195
class NoWorkingTree(BzrNewError):
172
196
    """No WorkingTree exists for %(base)s."""
173
197
    
176
200
        self.base = base
177
201
 
178
202
 
 
203
class NotBuilding(BzrNewError):
 
204
    """Not currently building a tree."""
 
205
 
 
206
 
179
207
class NotLocalUrl(BzrNewError):
180
208
    """%(url)s is not a local path."""
181
209
    
197
225
    # BzrCommandError, and non-UI code should not throw a subclass of
198
226
    # BzrCommandError.  ADHB 20051211
199
227
    def __init__(self, msg):
200
 
        self.msg = msg
 
228
        # Object.__str__() must return a real string
 
229
        # returning a Unicode string is a python error.
 
230
        if isinstance(msg, unicode):
 
231
            self.msg = msg.encode('utf8')
 
232
        else:
 
233
            self.msg = msg
201
234
 
202
235
    def __str__(self):
203
236
        return self.msg
254
287
 
255
288
    def __init__(self, msg, base, args):
256
289
        PathError.__init__(self, base, msg)
257
 
        self.args = [base]
258
 
        self.args.extend(args)
 
290
        self.args = [base] + list(args)
259
291
 
260
292
 
261
293
class UnsupportedProtocol(PathError):
265
297
        PathError.__init__(self, url, extra=extra)
266
298
 
267
299
 
 
300
class ShortReadvError(PathError):
 
301
    """readv() read %(actual)s bytes rather than %(length)s bytes at %(offset)s for %(path)s%(extra)s"""
 
302
 
 
303
    is_user_error = False
 
304
 
 
305
    def __init__(self, path, offset, length, actual, extra=None):
 
306
        PathError.__init__(self, path, extra=extra)
 
307
        self.offset = offset
 
308
        self.length = length
 
309
        self.actual = actual
 
310
 
 
311
 
268
312
class PathNotChild(BzrNewError):
269
313
    """Path %(path)r is not a child of path %(base)r%(extra)s"""
270
314
 
304
348
(use bzr checkout if you wish to build a working tree): %(path)s"""
305
349
 
306
350
 
 
351
class AtomicFileAlreadyClosed(PathError):
 
352
    """'%(function)s' called on an AtomicFile after it was closed: %(path)s"""
 
353
 
 
354
    def __init__(self, path, function):
 
355
        PathError.__init__(self, path=path, extra=None)
 
356
        self.function = function
 
357
 
 
358
 
 
359
class InaccessibleParent(PathError):
 
360
    """Parent not accessible given base %(base)s and relative path %(path)s"""
 
361
 
 
362
    def __init__(self, path, base):
 
363
        PathError.__init__(self, path)
 
364
        self.base = base
 
365
 
 
366
 
307
367
class NoRepositoryPresent(BzrNewError):
308
368
    """No repository present: %(path)r"""
309
369
    def __init__(self, bzrdir):
338
398
        self.bzrdir = bzrdir_format
339
399
 
340
400
 
 
401
class IncompatibleRevision(BzrNewError):
 
402
    """Revision is not compatible with %(repo_format)s"""
 
403
 
 
404
    def __init__(self, repo_format):
 
405
        BzrNewError.__init__(self)
 
406
        self.repo_format = repo_format
 
407
 
 
408
 
341
409
class NotVersionedError(BzrNewError):
342
410
    """%(path)s is not versioned"""
343
411
    def __init__(self, path):
478
546
        self.format = format
479
547
 
480
548
 
481
 
 
482
549
class StrictCommitFailed(Exception):
483
550
    """Commit refused because there are unknowns in the tree."""
484
551
 
489
556
    is_user_error = False
490
557
 
491
558
    def __init__(self, branch, revision):
492
 
        self.branch = branch
493
 
        self.revision = revision
 
559
        BzrNewError.__init__(self, branch=branch, revision=revision)
 
560
 
 
561
 
 
562
class NoSuchRevisionSpec(BzrNewError):
 
563
    """No namespace registered for string: %(spec)r"""
 
564
 
 
565
    def __init__(self, spec):
 
566
        BzrNewError.__init__(self, spec=spec)
 
567
 
 
568
 
 
569
class InvalidRevisionSpec(BzrNewError):
 
570
    """Requested revision: '%(spec)s' does not exist in branch:
 
571
%(branch)s%(extra)s"""
 
572
 
 
573
    def __init__(self, spec, branch, extra=None):
 
574
        BzrNewError.__init__(self, branch=branch, spec=spec)
 
575
        if extra:
 
576
            self.extra = '\n' + str(extra)
 
577
        else:
 
578
            self.extra = ''
494
579
 
495
580
 
496
581
class HistoryMissing(BzrError):
559
644
        self.bases = bases
560
645
 
561
646
 
562
 
class NoCommits(BzrError):
 
647
class NoCommits(BzrNewError):
 
648
    """Branch %(branch)s has no commits."""
 
649
 
563
650
    def __init__(self, branch):
564
 
        msg = "Branch %s has no commits." % branch
565
 
        BzrError.__init__(self, msg)
 
651
        BzrNewError.__init__(self, branch=branch)
566
652
 
567
653
 
568
654
class UnlistableStore(BzrError):
736
822
        BzrNewError.__init__(self)
737
823
 
738
824
 
 
825
class SmartProtocolError(TransportError):
 
826
    """Generic bzr smart protocol error: %(details)s"""
 
827
 
 
828
    def __init__(self, details):
 
829
        self.details = details
 
830
 
 
831
 
739
832
# A set of semi-meaningful errors which can be thrown
740
833
class TransportNotPossible(TransportError):
741
 
    """Transport operation not possible: %(msg)s %(orig_error)%"""
 
834
    """Transport operation not possible: %(msg)s %(orig_error)s"""
742
835
 
743
836
 
744
837
class ConnectionError(TransportError):
750
843
 
751
844
 
752
845
class InvalidRange(TransportError):
753
 
    """Invalid range access."""
 
846
    """Invalid range access in %(path)s at %(offset)s."""
754
847
    
755
848
    def __init__(self, path, offset):
756
849
        TransportError.__init__(self, ("Invalid range access in %s at %d"
757
850
                                       % (path, offset)))
 
851
        self.path = path
 
852
        self.offset = offset
758
853
 
759
854
 
760
855
class InvalidHttpResponse(TransportError):
795
890
        BzrError.__init__(self, message)
796
891
 
797
892
 
 
893
class NoEmailInUsername(BzrNewError):
 
894
    """%(username)r does not seem to contain a reasonable email address"""
 
895
 
 
896
    def __init__(self, username):
 
897
        BzrNewError.__init__(self)
 
898
        self.username = username
 
899
 
 
900
 
798
901
class SigningFailed(BzrError):
799
902
    def __init__(self, command_line):
800
903
        BzrError.__init__(self, "Failed to gpg sign data with command '%s'"
922
1025
        DependencyNotPresent.__init__(self, 'paramiko', error)
923
1026
 
924
1027
 
 
1028
class PointlessMerge(BzrNewError):
 
1029
    """Nothing to merge."""
 
1030
 
 
1031
 
925
1032
class UninitializableFormat(BzrNewError):
926
1033
    """Format %(format)s cannot be initialised by this version of bzr."""
927
1034
 
930
1037
        self.format = format
931
1038
 
932
1039
 
 
1040
class BadConversionTarget(BzrNewError):
 
1041
    """Cannot convert to format %(format)s.  %(problem)s"""
 
1042
 
 
1043
    def __init__(self, problem, format):
 
1044
        BzrNewError.__init__(self)
 
1045
        self.problem = problem
 
1046
        self.format = format
 
1047
 
 
1048
 
933
1049
class NoDiff(BzrNewError):
934
1050
    """Diff is not installed on this machine: %(msg)s"""
935
1051
 
936
1052
    def __init__(self, msg):
937
 
        super(NoDiff, self).__init__(msg=msg)
 
1053
        BzrNewError.__init__(self, msg=msg)
938
1054
 
939
1055
 
940
1056
class NoDiff3(BzrNewError):
1045
1161
    """Not a bzr revision-bundle: %(text)r"""
1046
1162
 
1047
1163
    def __init__(self, text):
1048
 
        self.text = text
1049
 
 
1050
 
 
1051
 
class BadBundle(Exception): pass
1052
 
 
1053
 
 
1054
 
class MalformedHeader(BadBundle): pass
1055
 
 
1056
 
 
1057
 
class MalformedPatches(BadBundle): pass
1058
 
 
1059
 
 
1060
 
class MalformedFooter(BadBundle): pass
 
1164
        BzrNewError.__init__(self)
 
1165
        self.text = text
 
1166
 
 
1167
 
 
1168
class BadBundle(BzrNewError): 
 
1169
    """Bad bzr revision-bundle: %(text)r"""
 
1170
 
 
1171
    def __init__(self, text):
 
1172
        BzrNewError.__init__(self)
 
1173
        self.text = text
 
1174
 
 
1175
 
 
1176
class MalformedHeader(BadBundle): 
 
1177
    """Malformed bzr revision-bundle header: %(text)r"""
 
1178
 
 
1179
    def __init__(self, text):
 
1180
        BzrNewError.__init__(self)
 
1181
        self.text = text
 
1182
 
 
1183
 
 
1184
class MalformedPatches(BadBundle): 
 
1185
    """Malformed patches in bzr revision-bundle: %(text)r"""
 
1186
 
 
1187
    def __init__(self, text):
 
1188
        BzrNewError.__init__(self)
 
1189
        self.text = text
 
1190
 
 
1191
 
 
1192
class MalformedFooter(BadBundle): 
 
1193
    """Malformed footer in bzr revision-bundle: %(text)r"""
 
1194
 
 
1195
    def __init__(self, text):
 
1196
        BzrNewError.__init__(self)
 
1197
        self.text = text
 
1198
 
 
1199
 
 
1200
class UnsupportedEOLMarker(BadBundle):
 
1201
    """End of line marker was not \\n in bzr revision-bundle"""    
 
1202
 
 
1203
    def __init__(self):
 
1204
        BzrNewError.__init__(self)
 
1205
 
 
1206
 
 
1207
class IncompatibleFormat(BzrNewError):
 
1208
    """Bundle format %(bundle_format)s is incompatible with %(other)s"""
 
1209
 
 
1210
    def __init__(self, bundle_format, other):
 
1211
        BzrNewError.__init__(self)
 
1212
        self.bundle_format = bundle_format
 
1213
        self.other = other
 
1214
 
 
1215
 
 
1216
class BadInventoryFormat(BzrNewError):
 
1217
    """Root class for inventory serialization errors"""
 
1218
 
 
1219
 
 
1220
class UnexpectedInventoryFormat(BadInventoryFormat):
 
1221
    """The inventory was not in the expected format:\n %(msg)s"""
 
1222
 
 
1223
    def __init__(self, msg):
 
1224
        BadInventoryFormat.__init__(self, msg=msg)
 
1225
 
 
1226
 
 
1227
class NoSmartServer(NotBranchError):
 
1228
    """No smart server available at %(url)s"""
 
1229
 
 
1230
    def __init__(self, url):
 
1231
        self.url = url
 
1232
 
 
1233
 
 
1234
class UnknownSSH(BzrNewError):
 
1235
    """Unrecognised value for BZR_SSH environment variable: %(vendor)s"""
 
1236
 
 
1237
    def __init__(self, vendor):
 
1238
        BzrNewError.__init__(self)
 
1239
        self.vendor = vendor
 
1240
 
 
1241
 
 
1242
class GhostRevisionUnusableHere(BzrNewError):
 
1243
    """Ghost revision {%(revision_id)s} cannot be used here."""
 
1244
 
 
1245
    def __init__(self, revision_id):
 
1246
        BzrNewError.__init__(self)
 
1247
        self.revision_id = revision_id
 
1248
 
 
1249
 
 
1250
class IllegalUseOfScopeReplacer(BzrNewError):
 
1251
    """ScopeReplacer object %(name)r was used incorrectly: %(msg)s%(extra)s"""
 
1252
 
 
1253
    is_user_error = False
 
1254
 
 
1255
    def __init__(self, name, msg, extra=None):
 
1256
        BzrNewError.__init__(self)
 
1257
        self.name = name
 
1258
        self.msg = msg
 
1259
        if extra:
 
1260
            self.extra = ': ' + str(extra)
 
1261
        else:
 
1262
            self.extra = ''
 
1263
 
 
1264
 
 
1265
class InvalidImportLine(BzrNewError):
 
1266
    """Not a valid import statement: %(msg)\n%(text)s"""
 
1267
 
 
1268
    is_user_error = False
 
1269
 
 
1270
    def __init__(self, text, msg):
 
1271
        BzrNewError.__init__(self)
 
1272
        self.text = text
 
1273
        self.msg = msg
 
1274
 
 
1275
 
 
1276
class ImportNameCollision(BzrNewError):
 
1277
    """Tried to import an object to the same name as an existing object. %(name)s"""
 
1278
 
 
1279
    is_user_error = False
 
1280
 
 
1281
    def __init__(self, name):
 
1282
        BzrNewError.__init__(self)
 
1283
        self.name = name