96
101
# never a 'unicode' object.
98
103
except Exception as e:
99
pass # just bind to 'e' for formatting below
102
105
return 'Unprintable exception %s: dict=%r, fmt=%r, error=%r' \
103
106
% (self.__class__.__name__,
105
108
getattr(self, '_fmt', None),
108
def __unicode__(self):
110
if isinstance(u, str):
111
# Try decoding the str using the default encoding.
113
elif not isinstance(u, unicode):
114
# Try to make a unicode object from it, because __unicode__ must
115
# return a unicode object.
121
if isinstance(s, unicode):
124
# __str__ must return a str.
115
return self._format().encode('utf-8')
117
__unicode__ = _format
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
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__
144
138
class InternalBzrError(BzrError):
145
139
"""Base class for errors that are internal in nature.
654
648
_fmt = 'Not a branch: "%(path)s"%(detail)s.'
656
def __init__(self, path, detail=None, bzrdir=None):
650
def __init__(self, path, detail=None, controldir=None):
657
651
import breezy.urlutils as urlutils
658
652
path = urlutils.unescape_for_display(path, 'ascii')
659
653
if detail is not None:
660
654
detail = ': ' + detail
661
655
self.detail = detail
662
self.controldir = bzrdir
656
self.controldir = controldir
663
657
PathError.__init__(self, path=path)
665
659
def __repr__(self):
666
660
return '<%s %r>' % (self.__class__.__name__, self.__dict__)
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.
662
def _get_format_string(self):
663
# GZ 2017-06-08: Not the best place to lazy fill detail in.
672
664
if self.detail is None:
673
if self.controldir is not None:
675
self.controldir.open_repository()
676
except NoRepositoryPresent:
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.
688
self.detail = ': location is a repository'
665
self.detail = self._get_detail()
666
return super(NotBranchError, self)._get_format_string()
668
def _get_detail(self):
669
if self.controldir is not None:
671
self.controldir.open_repository()
672
except NoRepositoryPresent:
674
except Exception as e:
675
# Just ignore unexpected errors. Raising arbitrary errors
676
# during str(err) can provoke strange bugs. Concretely
677
# Launchpad's codehosting managed to raise NotBranchError
678
# here, and then get stuck in an infinite loop/recursion
679
# trying to str() that error. All this error really cares
680
# about that there's no working repository there, and if
681
# open_repository() fails, there probably isn't.
682
return ': ' + e.__class__.__name__
691
return PathError._format(self)
684
return ': location is a repository'
694
688
class NoSubmitBranch(PathError):
753
747
class NoRepositoryPresent(BzrError):
755
749
_fmt = 'No repository present: "%(path)s"'
756
def __init__(self, bzrdir):
750
def __init__(self, controldir):
757
751
BzrError.__init__(self)
758
self.path = bzrdir.transport.clone('..').base
752
self.path = controldir.transport.clone('..').base
761
755
class UnsupportedFormatError(BzrError):
775
769
class IncompatibleFormat(BzrError):
777
_fmt = "Format %(format)s is not compatible with .bzr version %(bzrdir)s."
771
_fmt = "Format %(format)s is not compatible with .bzr version %(controldir)s."
779
def __init__(self, format, bzrdir_format):
773
def __init__(self, format, controldir_format):
780
774
BzrError.__init__(self)
781
775
self.format = format
782
self.controldir = bzrdir_format
776
self.controldir = controldir_format
785
779
class ParseFormatError(BzrError):
2796
2790
class BzrDirError(BzrError):
2798
def __init__(self, bzrdir):
2799
import breezy.urlutils as urlutils
2800
display_url = urlutils.unescape_for_display(bzrdir.user_url,
2792
def __init__(self, controldir):
2793
import .urlutils as urlutils
2794
display_url = urlutils.unescape_for_display(controldir.user_url,
2802
BzrError.__init__(self, bzrdir=bzrdir, display_url=display_url)
2796
BzrError.__init__(self, controldir=controldir, display_url=display_url)
2805
2799
class UnsyncedBranches(BzrDirError):
2807
2801
_fmt = ("'%(display_url)s' is not in sync with %(target_url)s. See"
2808
2802
" brz help sync-for-reconfigure.")
2810
def __init__(self, bzrdir, target_branch):
2811
BzrDirError.__init__(self, bzrdir)
2812
import breezy.urlutils as urlutils
2804
def __init__(self, controldir, target_branch):
2805
controldirError.__init__(self, controldir)
2806
import .urlutils as urlutils
2813
2807
self.target_url = urlutils.unescape_for_display(target_branch.base,
3200
3194
class NoColocatedBranchSupport(BzrError):
3202
_fmt = ("%(bzrdir)r does not support co-located branches.")
3196
_fmt = ("%(controldir)r does not support co-located branches.")
3204
def __init__(self, bzrdir):
3205
self.controldir = bzrdir
3198
def __init__(self, controldir):
3199
self.controldir = controldir
3208
3202
class NoWhoami(BzrError):