~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/errors.py

  • Committer: Aaron Bentley
  • Date: 2006-09-25 21:54:28 UTC
  • mto: This revision was merged to the branch mainline in revision 2042.
  • Revision ID: aaron.bentley@utoronto.ca-20060925215428-b2fe01fddf80004b
Change angle brackets to square brackets

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):
478
538
        self.format = format
479
539
 
480
540
 
481
 
 
482
541
class StrictCommitFailed(Exception):
483
542
    """Commit refused because there are unknowns in the tree."""
484
543
 
489
548
    is_user_error = False
490
549
 
491
550
    def __init__(self, branch, revision):
492
 
        self.branch = branch
493
 
        self.revision = revision
 
551
        BzrNewError.__init__(self, branch=branch, revision=revision)
 
552
 
 
553
 
 
554
class NoSuchRevisionSpec(BzrNewError):
 
555
    """No namespace registered for string: %(spec)r"""
 
556
 
 
557
    def __init__(self, spec):
 
558
        BzrNewError.__init__(self, spec=spec)
 
559
 
 
560
 
 
561
class InvalidRevisionSpec(BzrNewError):
 
562
    """Requested revision: '%(spec)s' does not exist in branch:
 
563
%(branch)s%(extra)s"""
 
564
 
 
565
    def __init__(self, spec, branch, extra=None):
 
566
        BzrNewError.__init__(self, branch=branch, spec=spec)
 
567
        if extra:
 
568
            self.extra = '\n' + str(extra)
 
569
        else:
 
570
            self.extra = ''
494
571
 
495
572
 
496
573
class HistoryMissing(BzrError):
559
636
        self.bases = bases
560
637
 
561
638
 
562
 
class NoCommits(BzrError):
 
639
class NoCommits(BzrNewError):
 
640
    """Branch %(branch)s has no commits."""
 
641
 
563
642
    def __init__(self, branch):
564
 
        msg = "Branch %s has no commits." % branch
565
 
        BzrError.__init__(self, msg)
 
643
        BzrNewError.__init__(self, branch=branch)
566
644
 
567
645
 
568
646
class UnlistableStore(BzrError):
736
814
        BzrNewError.__init__(self)
737
815
 
738
816
 
 
817
class SmartProtocolError(TransportError):
 
818
    """Generic bzr smart protocol error: %(details)s"""
 
819
 
 
820
    def __init__(self, details):
 
821
        self.details = details
 
822
 
 
823
 
739
824
# A set of semi-meaningful errors which can be thrown
740
825
class TransportNotPossible(TransportError):
741
826
    """Transport operation not possible: %(msg)s %(orig_error)%"""
750
835
 
751
836
 
752
837
class InvalidRange(TransportError):
753
 
    """Invalid range access."""
 
838
    """Invalid range access in %(path)s at %(offset)s."""
754
839
    
755
840
    def __init__(self, path, offset):
756
841
        TransportError.__init__(self, ("Invalid range access in %s at %d"
757
842
                                       % (path, offset)))
 
843
        self.path = path
 
844
        self.offset = offset
758
845
 
759
846
 
760
847
class InvalidHttpResponse(TransportError):
922
1009
        DependencyNotPresent.__init__(self, 'paramiko', error)
923
1010
 
924
1011
 
 
1012
class PointlessMerge(BzrNewError):
 
1013
    """Nothing to merge."""
 
1014
 
 
1015
 
925
1016
class UninitializableFormat(BzrNewError):
926
1017
    """Format %(format)s cannot be initialised by this version of bzr."""
927
1018
 
930
1021
        self.format = format
931
1022
 
932
1023
 
 
1024
class BadConversionTarget(BzrNewError):
 
1025
    """Cannot convert to format %(format)s.  %(problem)s"""
 
1026
 
 
1027
    def __init__(self, problem, format):
 
1028
        BzrNewError.__init__(self)
 
1029
        self.problem = problem
 
1030
        self.format = format
 
1031
 
 
1032
 
933
1033
class NoDiff(BzrNewError):
934
1034
    """Diff is not installed on this machine: %(msg)s"""
935
1035
 
936
1036
    def __init__(self, msg):
937
 
        super(NoDiff, self).__init__(msg=msg)
 
1037
        BzrNewError.__init__(self, msg=msg)
938
1038
 
939
1039
 
940
1040
class NoDiff3(BzrNewError):
1045
1145
    """Not a bzr revision-bundle: %(text)r"""
1046
1146
 
1047
1147
    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
 
1148
        BzrNewError.__init__(self)
 
1149
        self.text = text
 
1150
 
 
1151
 
 
1152
class BadBundle(BzrNewError): 
 
1153
    """Bad bzr revision-bundle: %(text)r"""
 
1154
 
 
1155
    def __init__(self, text):
 
1156
        BzrNewError.__init__(self)
 
1157
        self.text = text
 
1158
 
 
1159
 
 
1160
class MalformedHeader(BadBundle): 
 
1161
    """Malformed bzr revision-bundle header: %(text)r"""
 
1162
 
 
1163
    def __init__(self, text):
 
1164
        BzrNewError.__init__(self)
 
1165
        self.text = text
 
1166
 
 
1167
 
 
1168
class MalformedPatches(BadBundle): 
 
1169
    """Malformed patches in bzr revision-bundle: %(text)r"""
 
1170
 
 
1171
    def __init__(self, text):
 
1172
        BzrNewError.__init__(self)
 
1173
        self.text = text
 
1174
 
 
1175
 
 
1176
class MalformedFooter(BadBundle): 
 
1177
    """Malformed footer in bzr revision-bundle: %(text)r"""
 
1178
 
 
1179
    def __init__(self, text):
 
1180
        BzrNewError.__init__(self)
 
1181
        self.text = text
 
1182
 
 
1183
 
 
1184
class UnsupportedEOLMarker(BadBundle):
 
1185
    """End of line marker was not \\n in bzr revision-bundle"""    
 
1186
 
 
1187
    def __init__(self):
 
1188
        BzrNewError.__init__(self)
 
1189
 
 
1190
 
 
1191
class BadInventoryFormat(BzrNewError):
 
1192
    """Root class for inventory serialization errors"""
 
1193
 
 
1194
 
 
1195
class UnexpectedInventoryFormat(BadInventoryFormat):
 
1196
    """The inventory was not in the expected format:\n %(msg)s"""
 
1197
 
 
1198
    def __init__(self, msg):
 
1199
        BadInventoryFormat.__init__(self, msg=msg)
 
1200
 
 
1201
 
 
1202
class NoSmartServer(NotBranchError):
 
1203
    """No smart server available at %(url)s"""
 
1204
 
 
1205
    def __init__(self, url):
 
1206
        self.url = url
 
1207
 
 
1208
 
 
1209
class UnknownSSH(BzrNewError):
 
1210
    """Unrecognised value for BZR_SSH environment variable: %(vendor)s"""
 
1211
 
 
1212
    def __init__(self, vendor):
 
1213
        BzrNewError.__init__(self)
 
1214
        self.vendor = vendor
 
1215
 
 
1216
 
 
1217
class GhostRevisionUnusableHere(BzrNewError):
 
1218
    """Ghost revision {%(revision_id)s} cannot be used here."""
 
1219
 
 
1220
    def __init__(self, revision_id):
 
1221
        BzrNewError.__init__(self)
 
1222
        self.revision_id = revision_id
 
1223
 
 
1224
 
 
1225
class IllegalUseOfScopeReplacer(BzrNewError):
 
1226
    """ScopeReplacer object %(name)r was used incorrectly: %(msg)s%(extra)s"""
 
1227
 
 
1228
    is_user_error = False
 
1229
 
 
1230
    def __init__(self, name, msg, extra=None):
 
1231
        BzrNewError.__init__(self)
 
1232
        self.name = name
 
1233
        self.msg = msg
 
1234
        if extra:
 
1235
            self.extra = ': ' + str(extra)
 
1236
        else:
 
1237
            self.extra = ''
 
1238
 
 
1239
 
 
1240
class InvalidImportLine(BzrNewError):
 
1241
    """Not a valid import statement: %(msg)\n%(text)s"""
 
1242
 
 
1243
    is_user_error = False
 
1244
 
 
1245
    def __init__(self, text, msg):
 
1246
        BzrNewError.__init__(self)
 
1247
        self.text = text
 
1248
        self.msg = msg
 
1249
 
 
1250
 
 
1251
class ImportNameCollision(BzrNewError):
 
1252
    """Tried to import an object to the same name as an existing object. %(name)s"""
 
1253
 
 
1254
    is_user_error = False
 
1255
 
 
1256
    def __init__(self, name):
 
1257
        BzrNewError.__init__(self)
 
1258
        self.name = name