/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: John Arbash Meinel
  • Date: 2008-08-14 17:08:56 UTC
  • mfrom: (3629 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3633.
  • Revision ID: john@arbash-meinel.com-20080814170856-p2ie5z7l6vowrftz
merge bzr.dev 3629, update NEWS entry to be in correct location.

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)
135
153
               getattr(self, '_fmt', None),
136
154
               )
137
155
 
 
156
    def __eq__(self, other):
 
157
        if self.__class__ != other.__class__:
 
158
            return NotImplemented
 
159
        return self.__dict__ == other.__dict__
 
160
 
138
161
 
139
162
class InternalBzrError(BzrError):
140
163
    """Base class for errors that are internal in nature.
366
389
    # are not intended to be caught anyway.  UI code need not subclass
367
390
    # BzrCommandError, and non-UI code should not throw a subclass of
368
391
    # BzrCommandError.  ADHB 20051211
369
 
    def __init__(self, msg):
370
 
        # Object.__str__() must return a real string
371
 
        # returning a Unicode string is a python error.
372
 
        if isinstance(msg, unicode):
373
 
            self.msg = msg.encode('utf8')
374
 
        else:
375
 
            self.msg = msg
376
 
 
377
 
    def __str__(self):
378
 
        return self.msg
379
392
 
380
393
 
381
394
class NotWriteLocked(BzrError):
761
774
 
762
775
class IncompatibleRepositories(BzrError):
763
776
 
764
 
    _fmt = "Repository %(target)s is not compatible with repository"\
765
 
        " %(source)s"
 
777
    _fmt = "%(target)s\n" \
 
778
            "is not compatible with\n" \
 
779
            "%(source)s\n" \
 
780
            "%(details)s"
766
781
 
767
 
    def __init__(self, source, target):
768
 
        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)
769
786
 
770
787
 
771
788
class IncompatibleRevision(BzrError):
2368
2385
        self.patch_type = patch_type
2369
2386
 
2370
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
 
2371
2401
class UnsupportedInventoryKind(BzrError):
2372
2402
    
2373
2403
    _fmt = """Unsupported entry kind %(kind)s"""
2416
2446
class TagsNotSupported(BzrError):
2417
2447
 
2418
2448
    _fmt = ("Tags not supported by %(branch)s;"
2419
 
            " you may be able to use bzr upgrade --dirstate-tags.")
 
2449
            " you may be able to use bzr upgrade.")
2420
2450
 
2421
2451
    def __init__(self, branch):
2422
2452
        self.branch = branch
2789
2819
 
2790
2820
    def __init__(self, unknowns):
2791
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