/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-08-07 00:25:38 UTC
  • mfrom: (3612 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3613.
  • Revision ID: andrew.bennetts@canonical.com-20080807002538-mtl1fcgy2fdabha4
Merge from bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
93
93
            for key, value in kwds.items():
94
94
                setattr(self, key, value)
95
95
 
96
 
    def __str__(self):
 
96
    def _format(self):
97
97
        s = getattr(self, '_preformatted_string', None)
98
98
        if s is not None:
99
 
            # contains a preformatted message; must be cast to plain str
100
 
            return str(s)
 
99
            # contains a preformatted message
 
100
            return s
101
101
        try:
102
102
            fmt = self._get_format_string()
103
103
            if fmt:
108
108
                s = fmt % d
109
109
                # __str__() should always return a 'str' object
110
110
                # never a 'unicode' object.
111
 
                if isinstance(s, unicode):
112
 
                    return s.encode('utf8')
113
111
                return s
114
112
        except (AttributeError, TypeError, NameError, ValueError, KeyError), e:
115
113
            return 'Unprintable exception %s: dict=%r, fmt=%r, error=%r' \
118
116
                   getattr(self, '_fmt', None),
119
117
                   e)
120
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
 
121
139
    def _get_format_string(self):
122
140
        """Return format string for this exception or None"""
123
141
        fmt = getattr(self, '_fmt', None)
371
389
    # are not intended to be caught anyway.  UI code need not subclass
372
390
    # BzrCommandError, and non-UI code should not throw a subclass of
373
391
    # BzrCommandError.  ADHB 20051211
374
 
    def __init__(self, msg):
375
 
        # Object.__str__() must return a real string
376
 
        # returning a Unicode string is a python error.
377
 
        if isinstance(msg, unicode):
378
 
            self.msg = msg.encode('utf8')
379
 
        else:
380
 
            self.msg = msg
381
 
 
382
 
    def __str__(self):
383
 
        return self.msg
384
392
 
385
393
 
386
394
class NotWriteLocked(BzrError):
766
774
 
767
775
class IncompatibleRepositories(BzrError):
768
776
 
769
 
    _fmt = "Repository %(target)s is not compatible with repository"\
770
 
        " %(source)s"
 
777
    _fmt = "%(target)s\n" \
 
778
            "is not compatible with\n" \
 
779
            "%(source)s\n" \
 
780
            "%(details)s"
771
781
 
772
 
    def __init__(self, source, target):
773
 
        BzrError.__init__(self, target=target, source=source)
 
782
    def __init__(self, source, target, details=None):
 
783
        if details is None:
 
784
            details = "(no details)"
 
785
        BzrError.__init__(self, target=target, source=source, details=details)
774
786
 
775
787
 
776
788
class IncompatibleRevision(BzrError):
2373
2385
        self.patch_type = patch_type
2374
2386
 
2375
2387
 
 
2388
class TargetNotBranch(BzrError):
 
2389
    """A merge directive's target branch is required, but isn't a branch"""
 
2390
 
 
2391
    _fmt = ("Your branch does not have all of the revisions required in "
 
2392
            "order to merge this merge directive and the target "
 
2393
            "location specified in the merge directive is not a branch: "
 
2394
            "%(location)s.")
 
2395
 
 
2396
    def __init__(self, location):
 
2397
        BzrError.__init__(self)
 
2398
        self.location = location
 
2399
 
 
2400
 
2376
2401
class UnsupportedInventoryKind(BzrError):
2377
2402
    
2378
2403
    _fmt = """Unsupported entry kind %(kind)s"""
2421
2446
class TagsNotSupported(BzrError):
2422
2447
 
2423
2448
    _fmt = ("Tags not supported by %(branch)s;"
2424
 
            " you may be able to use bzr upgrade --dirstate-tags.")
 
2449
            " you may be able to use bzr upgrade.")
2425
2450
 
2426
2451
    def __init__(self, branch):
2427
2452
        self.branch = branch
2794
2819
 
2795
2820
    def __init__(self, unknowns):
2796
2821
        BzrError.__init__(self, unknowns_str=", ".join(unknowns))
 
2822
 
 
2823
 
 
2824
class HookFailed(BzrError):
 
2825
    """Raised when a pre_change_branch_tip hook function fails anything other
 
2826
    than TipChangeRejected.
 
2827
    """
 
2828
 
 
2829
    _fmt = ("Hook '%(hook_name)s' during %(hook_stage)s failed:\n"
 
2830
            "%(traceback_text)s%(exc_value)s")
 
2831
 
 
2832
    def __init__(self, hook_stage, hook_name, exc_info):
 
2833
        import traceback
 
2834
        self.hook_stage = hook_stage
 
2835
        self.hook_name = hook_name
 
2836
        self.exc_info = exc_info
 
2837
        self.exc_type = exc_info[0]
 
2838
        self.exc_value = exc_info[1]
 
2839
        self.exc_tb = exc_info[2]
 
2840
        self.traceback_text = ''.join(traceback.format_tb(self.exc_tb))
 
2841
 
 
2842
 
 
2843
class TipChangeRejected(BzrError):
 
2844
    """A pre_change_branch_tip hook function may raise this to cleanly and
 
2845
    explicitly abort a change to a branch tip.
 
2846
    """
 
2847
    
 
2848
    _fmt = u"Tip change rejected: %(msg)s"
 
2849
 
 
2850
    def __init__(self, msg):
 
2851
        self.msg = msg
 
2852