~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/errors.py

  • Committer: Andrew Bennetts
  • Date: 2008-07-25 06:42:08 UTC
  • mto: This revision was merged to the branch mainline in revision 3581.
  • Revision ID: andrew.bennetts@canonical.com-20080725064208-ui70gluukdypd4y9
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.

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
99
            # contains a preformatted message; must be cast to plain str
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
        return u
 
125
    
 
126
    def __str__(self):
 
127
        s = self._format()
 
128
        if isinstance(s, unicode):
 
129
            return s.encode('utf8')
 
130
        return s
 
131
 
121
132
    def _get_format_string(self):
122
133
        """Return format string for this exception or None"""
123
134
        fmt = getattr(self, '_fmt', None)
371
382
    # are not intended to be caught anyway.  UI code need not subclass
372
383
    # BzrCommandError, and non-UI code should not throw a subclass of
373
384
    # 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
385
 
385
386
 
386
387
class NotWriteLocked(BzrError):
2794
2795
 
2795
2796
    def __init__(self, unknowns):
2796
2797
        BzrError.__init__(self, unknowns_str=", ".join(unknowns))
 
2798
 
 
2799
 
 
2800
class HookFailed(BzrError):
 
2801
    """Raised when a pre_change_branch_tip hook function fails anything other
 
2802
    than TipChangeRejected.
 
2803
    """
 
2804
 
 
2805
    _fmt = ("Hook '%(hook_name)s' during %(hook_stage)s failed:\n"
 
2806
            "%(traceback_text)s%(exc_value)s")
 
2807
 
 
2808
    def __init__(self, hook_stage, hook_name, exc_info):
 
2809
        import traceback
 
2810
        self.hook_stage = hook_stage
 
2811
        self.hook_name = hook_name
 
2812
        self.exc_info = exc_info
 
2813
        self.exc_type = exc_info[0]
 
2814
        self.exc_value = exc_info[1]
 
2815
        self.exc_tb = exc_info[2]
 
2816
        self.traceback_text = ''.join(traceback.format_tb(self.exc_tb))
 
2817
 
 
2818
 
 
2819
class TipChangeRejected(BzrError):
 
2820
    """A pre_change_branch_tip hook function may raise this to cleanly and
 
2821
    explicitly abort a change to a branch tip.
 
2822
    """
 
2823
    
 
2824
    _fmt = u"Tip change rejected: %(msg)s"
 
2825
 
 
2826
    def __init__(self, msg):
 
2827
        self.msg = msg
 
2828