/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/errors.py

  • Committer: Andrew Bennetts
  • Date: 2008-09-08 12:59:00 UTC
  • mfrom: (3695 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3756.
  • Revision ID: andrew.bennetts@canonical.com-20080908125900-8ywtsr7jqyyatjz0
Merge from bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
 
21
21
from bzrlib import (
 
22
    osutils,
22
23
    symbol_versioning,
23
24
    )
24
25
from bzrlib.patches import (
92
93
            for key, value in kwds.items():
93
94
                setattr(self, key, value)
94
95
 
95
 
    def __str__(self):
 
96
    def _format(self):
96
97
        s = getattr(self, '_preformatted_string', None)
97
98
        if s is not None:
98
 
            # contains a preformatted message; must be cast to plain str
99
 
            return str(s)
 
99
            # contains a preformatted message
 
100
            return s
100
101
        try:
101
102
            fmt = self._get_format_string()
102
103
            if fmt:
107
108
                s = fmt % d
108
109
                # __str__() should always return a 'str' object
109
110
                # never a 'unicode' object.
110
 
                if isinstance(s, unicode):
111
 
                    return s.encode('utf8')
112
111
                return s
113
112
        except (AttributeError, TypeError, NameError, ValueError, KeyError), e:
114
113
            return 'Unprintable exception %s: dict=%r, fmt=%r, error=%r' \
117
116
                   getattr(self, '_fmt', None),
118
117
                   e)
119
118
 
 
119
    def __unicode__(self):
 
120
        u = self._format()
 
121
        if isinstance(u, str):
 
122
            # Try decoding the str using the default encoding.
 
123
            u = unicode(u)
 
124
        elif not isinstance(u, unicode):
 
125
            # Try to make a unicode object from it, because __unicode__ must
 
126
            # return a unicode object.
 
127
            u = unicode(u)
 
128
        return u
 
129
    
 
130
    def __str__(self):
 
131
        s = self._format()
 
132
        if isinstance(s, unicode):
 
133
            s = s.encode('utf8')
 
134
        else:
 
135
            # __str__ must return a str.
 
136
            s = str(s)
 
137
        return s
 
138
 
120
139
    def _get_format_string(self):
121
140
        """Return format string for this exception or None"""
122
141
        fmt = getattr(self, '_fmt', None)
134
153
               getattr(self, '_fmt', None),
135
154
               )
136
155
 
 
156
    def __eq__(self, other):
 
157
        if self.__class__ != other.__class__:
 
158
            return NotImplemented
 
159
        return self.__dict__ == other.__dict__
 
160
 
137
161
 
138
162
class InternalBzrError(BzrError):
139
163
    """Base class for errors that are internal in nature.
200
224
        self.message = message
201
225
 
202
226
 
 
227
class DirstateCorrupt(BzrError):
 
228
 
 
229
    _fmt = "The dirstate file (%(state)s) appears to be corrupt: %(msg)s"
 
230
 
 
231
    def __init__(self, state, msg):
 
232
        BzrError.__init__(self)
 
233
        self.state = state
 
234
        self.msg = msg
 
235
 
 
236
 
203
237
class DisabledMethod(InternalBzrError):
204
238
 
205
239
    _fmt = "The smart server method '%(class_name)s' is disabled."
365
399
    # are not intended to be caught anyway.  UI code need not subclass
366
400
    # BzrCommandError, and non-UI code should not throw a subclass of
367
401
    # BzrCommandError.  ADHB 20051211
368
 
    def __init__(self, msg):
369
 
        # Object.__str__() must return a real string
370
 
        # returning a Unicode string is a python error.
371
 
        if isinstance(msg, unicode):
372
 
            self.msg = msg.encode('utf8')
373
 
        else:
374
 
            self.msg = msg
375
 
 
376
 
    def __str__(self):
377
 
        return self.msg
378
402
 
379
403
 
380
404
class NotWriteLocked(BzrError):
561
585
        PathError.__init__(self, base, reason)
562
586
 
563
587
 
 
588
class InvalidRebaseURLs(PathError):
 
589
 
 
590
    _fmt = "URLs differ by more than path: %(from_)r and %(to)r"
 
591
 
 
592
    def __init__(self, from_, to):
 
593
        self.from_ = from_
 
594
        self.to = to
 
595
        PathError.__init__(self, from_, 'URLs differ by more than path.')
 
596
 
 
597
 
 
598
class UnavailableRepresentation(InternalBzrError):
 
599
 
 
600
    _fmt = ("The encoding '%(wanted)s' is not available for key %(key)s which "
 
601
        "is encoded as '%(native)s'.")
 
602
 
 
603
    def __init__(self, key, wanted, native):
 
604
        InternalBzrError.__init__(self)
 
605
        self.wanted = wanted
 
606
        self.native = native
 
607
        self.key = key
 
608
 
 
609
 
564
610
class UnknownHook(BzrError):
565
611
 
566
612
    _fmt = "The %(type)s hook '%(hook)s' is unknown in this version of bzrlib."
738
784
 
739
785
class IncompatibleRepositories(BzrError):
740
786
 
741
 
    _fmt = "Repository %(target)s is not compatible with repository"\
742
 
        " %(source)s"
 
787
    _fmt = "%(target)s\n" \
 
788
            "is not compatible with\n" \
 
789
            "%(source)s\n" \
 
790
            "%(details)s"
743
791
 
744
 
    def __init__(self, source, target):
745
 
        BzrError.__init__(self, target=target, source=source)
 
792
    def __init__(self, source, target, details=None):
 
793
        if details is None:
 
794
            details = "(no details)"
 
795
        BzrError.__init__(self, target=target, source=source, details=details)
746
796
 
747
797
 
748
798
class IncompatibleRevision(BzrError):
835
885
        BzrError.__init__(self, filename=filename, kind=kind)
836
886
 
837
887
 
 
888
class BadFilenameEncoding(BzrError):
 
889
 
 
890
    _fmt = ('Filename %(filename)r is not valid in your current filesystem'
 
891
            ' encoding %(fs_encoding)s')
 
892
 
 
893
    def __init__(self, filename, fs_encoding):
 
894
        BzrError.__init__(self)
 
895
        self.filename = filename
 
896
        self.fs_encoding = fs_encoding
 
897
 
 
898
 
838
899
class ForbiddenControlFileError(BzrError):
839
900
 
840
901
    _fmt = 'Cannot operate on "%(filename)s" because it is a control file'
893
954
        self.obj = obj
894
955
 
895
956
 
896
 
class ReadOnlyLockError(LockError):
897
 
 
898
 
    _fmt = "Cannot acquire write lock on %(fname)s. %(msg)s"
899
 
 
900
 
    @symbol_versioning.deprecated_method(symbol_versioning.zero_ninetytwo)
901
 
    def __init__(self, fname, msg):
902
 
        LockError.__init__(self, '')
903
 
        self.fname = fname
904
 
        self.msg = msg
905
 
 
906
 
 
907
957
class LockFailed(LockError):
908
958
 
909
959
    internal_error = False
1065
1115
        BzrError.__init__(self, branch=branch, revision=revision)
1066
1116
 
1067
1117
 
1068
 
# zero_ninetyone: this exception is no longer raised and should be removed
1069
 
class NotLeftParentDescendant(InternalBzrError):
1070
 
 
1071
 
    _fmt = ("Revision %(old_revision)s is not the left parent of"
1072
 
            " %(new_revision)s, but branch %(branch_location)s expects this")
1073
 
 
1074
 
    def __init__(self, branch, old_revision, new_revision):
1075
 
        BzrError.__init__(self, branch_location=branch.base,
1076
 
                          old_revision=old_revision,
1077
 
                          new_revision=new_revision)
1078
 
 
1079
 
 
1080
1118
class RangeInChangeOption(BzrError):
1081
1119
 
1082
1120
    _fmt = "Option --change does not accept revision ranges"
1475
1513
        self.details = details
1476
1514
 
1477
1515
 
 
1516
class UnexpectedProtocolVersionMarker(TransportError):
 
1517
 
 
1518
    _fmt = "Received bad protocol version marker: %(marker)r"
 
1519
 
 
1520
    def __init__(self, marker):
 
1521
        self.marker = marker
 
1522
 
 
1523
 
 
1524
class UnknownSmartMethod(InternalBzrError):
 
1525
 
 
1526
    _fmt = "The server does not recognise the '%(verb)s' request."
 
1527
 
 
1528
    def __init__(self, verb):
 
1529
        self.verb = verb
 
1530
 
 
1531
 
 
1532
class SmartMessageHandlerError(InternalBzrError):
 
1533
 
 
1534
    _fmt = "The message handler raised an exception: %(exc_value)s."
 
1535
 
 
1536
    def __init__(self, exc_info):
 
1537
        self.exc_type, self.exc_value, self.tb = exc_info
 
1538
        
 
1539
 
1478
1540
# A set of semi-meaningful errors which can be thrown
1479
1541
class TransportNotPossible(TransportError):
1480
1542
 
2066
2128
        self.path = path
2067
2129
 
2068
2130
 
 
2131
class RepositoryUpgradeRequired(UpgradeRequired):
 
2132
 
 
2133
    _fmt = "To use this feature you must upgrade your repository at %(path)s."
 
2134
 
 
2135
 
2069
2136
class LocalRequiresBoundBranch(BzrError):
2070
2137
 
2071
2138
    _fmt = "Cannot perform local-only commits on unbound branches."
2235
2302
            " Please set BZR_SSH environment variable.")
2236
2303
 
2237
2304
 
 
2305
class GhostRevisionsHaveNoRevno(BzrError):
 
2306
    """When searching for revnos, if we encounter a ghost, we are stuck"""
 
2307
 
 
2308
    _fmt = ("Could not determine revno for {%(revision_id)s} because"
 
2309
            " its ancestry shows a ghost at {%(ghost_revision_id)s}")
 
2310
 
 
2311
    def __init__(self, revision_id, ghost_revision_id):
 
2312
        self.revision_id = revision_id
 
2313
        self.ghost_revision_id = ghost_revision_id
 
2314
 
 
2315
        
2238
2316
class GhostRevisionUnusableHere(BzrError):
2239
2317
 
2240
2318
    _fmt = "Ghost revision {%(revision_id)s} cannot be used here."
2318
2396
        self.patch_type = patch_type
2319
2397
 
2320
2398
 
 
2399
class TargetNotBranch(BzrError):
 
2400
    """A merge directive's target branch is required, but isn't a branch"""
 
2401
 
 
2402
    _fmt = ("Your branch does not have all of the revisions required in "
 
2403
            "order to merge this merge directive and the target "
 
2404
            "location specified in the merge directive is not a branch: "
 
2405
            "%(location)s.")
 
2406
 
 
2407
    def __init__(self, location):
 
2408
        BzrError.__init__(self)
 
2409
        self.location = location
 
2410
 
 
2411
 
2321
2412
class UnsupportedInventoryKind(BzrError):
2322
2413
    
2323
2414
    _fmt = """Unsupported entry kind %(kind)s"""
2366
2457
class TagsNotSupported(BzrError):
2367
2458
 
2368
2459
    _fmt = ("Tags not supported by %(branch)s;"
2369
 
            " you may be able to use bzr upgrade --dirstate-tags.")
 
2460
            " you may be able to use bzr upgrade.")
2370
2461
 
2371
2462
    def __init__(self, branch):
2372
2463
        self.branch = branch
2417
2508
        self.response_tuple = response_tuple
2418
2509
 
2419
2510
 
 
2511
class ErrorFromSmartServer(BzrError):
 
2512
    """An error was received from a smart server.
 
2513
 
 
2514
    :seealso: UnknownErrorFromSmartServer
 
2515
    """
 
2516
 
 
2517
    _fmt = "Error received from smart server: %(error_tuple)r"
 
2518
 
 
2519
    internal_error = True
 
2520
 
 
2521
    def __init__(self, error_tuple):
 
2522
        self.error_tuple = error_tuple
 
2523
        try:
 
2524
            self.error_verb = error_tuple[0]
 
2525
        except IndexError:
 
2526
            self.error_verb = None
 
2527
        self.error_args = error_tuple[1:]
 
2528
 
 
2529
 
 
2530
class UnknownErrorFromSmartServer(BzrError):
 
2531
    """An ErrorFromSmartServer could not be translated into a typical bzrlib
 
2532
    error.
 
2533
 
 
2534
    This is distinct from ErrorFromSmartServer so that it is possible to
 
2535
    distinguish between the following two cases:
 
2536
      - ErrorFromSmartServer was uncaught.  This is logic error in the client
 
2537
        and so should provoke a traceback to the user.
 
2538
      - ErrorFromSmartServer was caught but its error_tuple could not be
 
2539
        translated.  This is probably because the server sent us garbage, and
 
2540
        should not provoke a traceback.
 
2541
    """
 
2542
 
 
2543
    _fmt = "Server sent an unexpected error: %(error_tuple)r"
 
2544
 
 
2545
    internal_error = False
 
2546
 
 
2547
    def __init__(self, error_from_smart_server):
 
2548
        """Constructor.
 
2549
 
 
2550
        :param error_from_smart_server: An ErrorFromSmartServer instance.
 
2551
        """
 
2552
        self.error_from_smart_server = error_from_smart_server
 
2553
        self.error_tuple = error_from_smart_server.error_tuple
 
2554
        
 
2555
 
2420
2556
class ContainerError(BzrError):
2421
2557
    """Base class of container errors."""
2422
2558
 
2538
2674
        BzrError.__init__(self, bzrdir=bzrdir, display_url=display_url)
2539
2675
 
2540
2676
 
 
2677
class UnsyncedBranches(BzrDirError):
 
2678
 
 
2679
    _fmt = ("'%(display_url)s' is not in sync with %(target_url)s.  See"
 
2680
            " bzr help sync-for-reconfigure.")
 
2681
 
 
2682
    def __init__(self, bzrdir, target_branch):
 
2683
        BzrDirError.__init__(self, bzrdir)
 
2684
        import bzrlib.urlutils as urlutils
 
2685
        self.target_url = urlutils.unescape_for_display(target_branch.base,
 
2686
                                                        'ascii')
 
2687
 
 
2688
 
2541
2689
class AlreadyBranch(BzrDirError):
2542
2690
 
2543
2691
    _fmt = "'%(display_url)s' is already a branch."
2558
2706
    _fmt = "'%(display_url)s' is already a lightweight checkout."
2559
2707
 
2560
2708
 
 
2709
class AlreadyUsingShared(BzrDirError):
 
2710
 
 
2711
    _fmt = "'%(display_url)s' is already using a shared repository."
 
2712
 
 
2713
 
 
2714
class AlreadyStandalone(BzrDirError):
 
2715
 
 
2716
    _fmt = "'%(display_url)s' is already standalone."
 
2717
 
 
2718
 
2561
2719
class ReconfigurationNotSupported(BzrDirError):
2562
2720
 
2563
2721
    _fmt = "Requested reconfiguration of '%(display_url)s' is not supported."
2616
2774
        self.timezone = timezone
2617
2775
 
2618
2776
 
 
2777
class CommandAvailableInPlugin(StandardError):
 
2778
    
 
2779
    internal_error = False
 
2780
 
 
2781
    def __init__(self, cmd_name, plugin_metadata, provider):
 
2782
        
 
2783
        self.plugin_metadata = plugin_metadata
 
2784
        self.cmd_name = cmd_name
 
2785
        self.provider = provider
 
2786
 
 
2787
    def __str__(self):
 
2788
 
 
2789
        _fmt = ('"%s" is not a standard bzr command. \n' 
 
2790
                'However, the following official plugin provides this command: %s\n'
 
2791
                'You can install it by going to: %s'
 
2792
                % (self.cmd_name, self.plugin_metadata['name'], 
 
2793
                    self.plugin_metadata['url']))
 
2794
 
 
2795
        return _fmt
 
2796
 
 
2797
 
 
2798
class NoPluginAvailable(BzrError):
 
2799
    pass    
 
2800
 
 
2801
 
2619
2802
class NotATerminal(BzrError):
2620
2803
 
2621
2804
    _fmt = 'Unable to ask for a password without real terminal.'
2630
2813
        from bzrlib.osutils import get_user_encoding
2631
2814
        self.path = path
2632
2815
        self.kind = kind
2633
 
        self.user_encoding = get_user_encoding()
 
2816
        self.user_encoding = osutils.get_user_encoding()
 
2817
 
 
2818
 
 
2819
class NoSuchAlias(BzrError):
 
2820
 
 
2821
    _fmt = ('The alias "%(alias_name)s" does not exist.')
 
2822
 
 
2823
    def __init__(self, alias_name):
 
2824
        BzrError.__init__(self, alias_name=alias_name)
 
2825
 
 
2826
 
 
2827
class DirectoryLookupFailure(BzrError):
 
2828
    """Base type for lookup errors."""
 
2829
 
 
2830
    pass
 
2831
 
 
2832
 
 
2833
class InvalidLocationAlias(DirectoryLookupFailure):
 
2834
 
 
2835
    _fmt = '"%(alias_name)s" is not a valid location alias.'
 
2836
 
 
2837
    def __init__(self, alias_name):
 
2838
        DirectoryLookupFailure.__init__(self, alias_name=alias_name)
 
2839
 
 
2840
 
 
2841
class UnsetLocationAlias(DirectoryLookupFailure):
 
2842
 
 
2843
    _fmt = 'No %(alias_name)s location assigned.'
 
2844
 
 
2845
    def __init__(self, alias_name):
 
2846
        DirectoryLookupFailure.__init__(self, alias_name=alias_name[1:])
 
2847
 
 
2848
 
 
2849
class CannotBindAddress(BzrError):
 
2850
 
 
2851
    _fmt = 'Cannot bind address "%(host)s:%(port)i": %(orig_error)s.'
 
2852
 
 
2853
    def __init__(self, host, port, orig_error):
 
2854
        BzrError.__init__(self, host=host, port=port,
 
2855
            orig_error=orig_error[1])
 
2856
 
 
2857
 
 
2858
class UnknownRules(BzrError):
 
2859
 
 
2860
    _fmt = ('Unknown rules detected: %(unknowns_str)s.')
 
2861
 
 
2862
    def __init__(self, unknowns):
 
2863
        BzrError.__init__(self, unknowns_str=", ".join(unknowns))
 
2864
 
 
2865
 
 
2866
class HookFailed(BzrError):
 
2867
    """Raised when a pre_change_branch_tip hook function fails anything other
 
2868
    than TipChangeRejected.
 
2869
    """
 
2870
 
 
2871
    _fmt = ("Hook '%(hook_name)s' during %(hook_stage)s failed:\n"
 
2872
            "%(traceback_text)s%(exc_value)s")
 
2873
 
 
2874
    def __init__(self, hook_stage, hook_name, exc_info):
 
2875
        import traceback
 
2876
        self.hook_stage = hook_stage
 
2877
        self.hook_name = hook_name
 
2878
        self.exc_info = exc_info
 
2879
        self.exc_type = exc_info[0]
 
2880
        self.exc_value = exc_info[1]
 
2881
        self.exc_tb = exc_info[2]
 
2882
        self.traceback_text = ''.join(traceback.format_tb(self.exc_tb))
 
2883
 
 
2884
 
 
2885
class TipChangeRejected(BzrError):
 
2886
    """A pre_change_branch_tip hook function may raise this to cleanly and
 
2887
    explicitly abort a change to a branch tip.
 
2888
    """
 
2889
    
 
2890
    _fmt = u"Tip change rejected: %(msg)s"
 
2891
 
 
2892
    def __init__(self, msg):
 
2893
        self.msg = msg
 
2894