/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 breezy/errors.py

  • Committer: Jelmer Vernooij
  • Date: 2017-06-10 18:44:23 UTC
  • mfrom: (6681 work)
  • mto: This revision was merged to the branch mainline in revision 6684.
  • Revision ID: jelmer@jelmer.uk-20170610184423-viizzbubmjxjq5p9
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
from __future__ import absolute_import
21
21
 
 
22
from .sixish import (
 
23
    PY3,
 
24
    )
 
25
 
22
26
# TODO: is there any value in providing the .args field used by standard
23
27
# python exceptions?   A list of values with no names seems less useful
24
28
# to me.
87
91
        if s is not None:
88
92
            # contains a preformatted message
89
93
            return s
 
94
        err = None
90
95
        try:
91
96
            fmt = self._get_format_string()
92
97
            if fmt:
96
101
                # never a 'unicode' object.
97
102
                return s
98
103
        except Exception as e:
99
 
            pass # just bind to 'e' for formatting below
100
 
        else:
101
 
            e = None
 
104
            err = e
102
105
        return 'Unprintable exception %s: dict=%r, fmt=%r, error=%r' \
103
106
            % (self.__class__.__name__,
104
107
               self.__dict__,
105
108
               getattr(self, '_fmt', None),
106
 
               e)
107
 
 
108
 
    def __unicode__(self):
109
 
        u = self._format()
110
 
        if isinstance(u, str):
111
 
            # Try decoding the str using the default encoding.
112
 
            u = unicode(u)
113
 
        elif not isinstance(u, unicode):
114
 
            # Try to make a unicode object from it, because __unicode__ must
115
 
            # return a unicode object.
116
 
            u = unicode(u)
117
 
        return u
118
 
 
119
 
    def __str__(self):
120
 
        s = self._format()
121
 
        if isinstance(s, unicode):
122
 
            s = s.encode('utf8')
123
 
        else:
124
 
            # __str__ must return a str.
125
 
            s = str(s)
126
 
        return s
 
109
               err)
 
110
 
 
111
    if PY3:
 
112
        __str__ = _format
 
113
    else:
 
114
        def __str__(self):
 
115
            return self._format().encode('utf-8')
 
116
 
 
117
        __unicode__ = _format
127
118
 
128
119
    def __repr__(self):
129
120
        return '%s(%s)' % (self.__class__.__name__, str(self))
133
124
        fmt = getattr(self, '_fmt', None)
134
125
        if fmt is not None:
135
126
            from breezy.i18n import gettext
136
 
            return gettext(unicode(fmt)) # _fmt strings should be ascii
 
127
            return gettext(fmt) # _fmt strings should be ascii
137
128
 
138
129
    def __eq__(self, other):
139
130
        if self.__class__ is not other.__class__:
140
131
            return NotImplemented
141
132
        return self.__dict__ == other.__dict__
142
133
 
 
134
    def __hash__(self):
 
135
        return id(self)
 
136
 
143
137
 
144
138
class InternalBzrError(BzrError):
145
139
    """Base class for errors that are internal in nature.
192
186
        self.class_name = class_name
193
187
 
194
188
 
195
 
class IncompatibleAPI(BzrError):
196
 
 
197
 
    _fmt = 'The API for "%(api)s" is not compatible with "%(wanted)s". '\
198
 
        'It supports versions "%(minimum)s" to "%(current)s".'
199
 
 
200
 
    def __init__(self, api, wanted, minimum, current):
 
189
class IncompatibleVersion(BzrError):
 
190
 
 
191
    _fmt = 'API %(api)s is not compatible; one of versions %(wanted)r '\
 
192
           'is required, but current version is %(current)r.'
 
193
 
 
194
    def __init__(self, api, wanted, current):
201
195
        self.api = api
202
196
        self.wanted = wanted
203
 
        self.minimum = minimum
204
197
        self.current = current
205
198
 
206
199
 
261
254
    _fmt = 'There is no public branch set for "%(branch_url)s".'
262
255
 
263
256
    def __init__(self, branch):
264
 
        import breezy.urlutils as urlutils
 
257
        from . import urlutils
265
258
        public_location = urlutils.unescape_for_display(branch.base, 'ascii')
266
259
        BzrError.__init__(self, branch_url=public_location)
267
260
 
653
646
 
654
647
    _fmt = 'Not a branch: "%(path)s"%(detail)s.'
655
648
 
656
 
    def __init__(self, path, detail=None, bzrdir=None):
657
 
       import breezy.urlutils as urlutils
 
649
    def __init__(self, path, detail=None, controldir=None):
 
650
       from . import urlutils
658
651
       path = urlutils.unescape_for_display(path, 'ascii')
659
652
       if detail is not None:
660
653
           detail = ': ' + detail
661
654
       self.detail = detail
662
 
       self.bzrdir = bzrdir
 
655
       self.controldir = controldir
663
656
       PathError.__init__(self, path=path)
664
657
 
665
658
    def __repr__(self):
666
659
        return '<%s %r>' % (self.__class__.__name__, self.__dict__)
667
660
 
668
 
    def _format(self):
669
 
        # XXX: Ideally self.detail would be a property, but Exceptions in
670
 
        # Python 2.4 have to be old-style classes so properties don't work.
671
 
        # Instead we override _format.
 
661
    def _get_format_string(self):
 
662
        # GZ 2017-06-08: Not the best place to lazy fill detail in.
672
663
        if self.detail is None:
673
 
            if self.bzrdir is not None:
674
 
                try:
675
 
                    self.bzrdir.open_repository()
676
 
                except NoRepositoryPresent:
677
 
                    self.detail = ''
678
 
                except Exception:
679
 
                    # Just ignore unexpected errors.  Raising arbitrary errors
680
 
                    # during str(err) can provoke strange bugs.  Concretely
681
 
                    # Launchpad's codehosting managed to raise NotBranchError
682
 
                    # here, and then get stuck in an infinite loop/recursion
683
 
                    # trying to str() that error.  All this error really cares
684
 
                    # about that there's no working repository there, and if
685
 
                    # open_repository() fails, there probably isn't.
686
 
                    self.detail = ''
687
 
                else:
688
 
                    self.detail = ': location is a repository'
 
664
           self.detail = self._get_detail()
 
665
        return super(NotBranchError, self)._get_format_string()
 
666
 
 
667
    def _get_detail(self):
 
668
        if self.controldir is not None:
 
669
            try:
 
670
                self.controldir.open_repository()
 
671
            except NoRepositoryPresent:
 
672
                return ''
 
673
            except Exception as e:
 
674
                # Just ignore unexpected errors.  Raising arbitrary errors
 
675
                # during str(err) can provoke strange bugs.  Concretely
 
676
                # Launchpad's codehosting managed to raise NotBranchError
 
677
                # here, and then get stuck in an infinite loop/recursion
 
678
                # trying to str() that error.  All this error really cares
 
679
                # about that there's no working repository there, and if
 
680
                # open_repository() fails, there probably isn't.
 
681
                return ': ' + e.__class__.__name__
689
682
            else:
690
 
                self.detail = ''
691
 
        return PathError._format(self)
 
683
                return ': location is a repository'
 
684
        return ''
692
685
 
693
686
 
694
687
class NoSubmitBranch(PathError):
696
689
    _fmt = 'No submit branch available for branch "%(path)s"'
697
690
 
698
691
    def __init__(self, branch):
699
 
       import breezy.urlutils as urlutils
 
692
       from . import urlutils
700
693
       self.path = urlutils.unescape_for_display(branch.base, 'ascii')
701
694
 
702
695
 
753
746
class NoRepositoryPresent(BzrError):
754
747
 
755
748
    _fmt = 'No repository present: "%(path)s"'
756
 
    def __init__(self, bzrdir):
 
749
    def __init__(self, controldir):
757
750
        BzrError.__init__(self)
758
 
        self.path = bzrdir.transport.clone('..').base
 
751
        self.path = controldir.transport.clone('..').base
759
752
 
760
753
 
761
754
class UnsupportedFormatError(BzrError):
774
767
 
775
768
class IncompatibleFormat(BzrError):
776
769
 
777
 
    _fmt = "Format %(format)s is not compatible with .bzr version %(bzrdir)s."
 
770
    _fmt = "Format %(format)s is not compatible with .bzr version %(controldir)s."
778
771
 
779
 
    def __init__(self, format, bzrdir_format):
 
772
    def __init__(self, format, controldir_format):
780
773
        BzrError.__init__(self)
781
774
        self.format = format
782
 
        self.bzrdir = bzrdir_format
 
775
        self.controldir = controldir_format
783
776
 
784
777
 
785
778
class ParseFormatError(BzrError):
2795
2788
 
2796
2789
class BzrDirError(BzrError):
2797
2790
 
2798
 
    def __init__(self, bzrdir):
2799
 
        import breezy.urlutils as urlutils
2800
 
        display_url = urlutils.unescape_for_display(bzrdir.user_url,
 
2791
    def __init__(self, controldir):
 
2792
        from . import urlutils
 
2793
        display_url = urlutils.unescape_for_display(controldir.user_url,
2801
2794
                                                    'ascii')
2802
 
        BzrError.__init__(self, bzrdir=bzrdir, display_url=display_url)
 
2795
        BzrError.__init__(self, controldir=controldir, display_url=display_url)
2803
2796
 
2804
2797
 
2805
2798
class UnsyncedBranches(BzrDirError):
2807
2800
    _fmt = ("'%(display_url)s' is not in sync with %(target_url)s.  See"
2808
2801
            " brz help sync-for-reconfigure.")
2809
2802
 
2810
 
    def __init__(self, bzrdir, target_branch):
2811
 
        BzrDirError.__init__(self, bzrdir)
2812
 
        import breezy.urlutils as urlutils
 
2803
    def __init__(self, controldir, target_branch):
 
2804
        BzrError.__init__(self, controldir)
 
2805
        from . import urlutils
2813
2806
        self.target_url = urlutils.unescape_for_display(target_branch.base,
2814
2807
                                                        'ascii')
2815
2808
 
3199
3192
 
3200
3193
class NoColocatedBranchSupport(BzrError):
3201
3194
 
3202
 
    _fmt = ("%(bzrdir)r does not support co-located branches.")
 
3195
    _fmt = ("%(controldir)r does not support co-located branches.")
3203
3196
 
3204
 
    def __init__(self, bzrdir):
3205
 
        self.bzrdir = bzrdir
 
3197
    def __init__(self, controldir):
 
3198
        self.controldir = controldir
3206
3199
 
3207
3200
 
3208
3201
class NoWhoami(BzrError):