17
17
"""Exceptions for bzr, and reporting of them.
26
from bzrlib.i18n import gettext
29
20
# TODO: is there any value in providing the .args field used by standard
30
21
# python exceptions? A list of values with no names seems less useful
102
93
# __str__() should always return a 'str' object
103
94
# never a 'unicode' object.
105
except (AttributeError, TypeError, NameError, ValueError, KeyError), e:
106
return 'Unprintable exception %s: dict=%r, fmt=%r, error=%r' \
107
% (self.__class__.__name__,
109
getattr(self, '_fmt', None),
97
pass # just bind to 'e' for formatting below
100
return 'Unprintable exception %s: dict=%r, fmt=%r, error=%r' \
101
% (self.__class__.__name__,
103
getattr(self, '_fmt', None),
112
106
def __unicode__(self):
113
107
u = self._format()
136
130
"""Return format string for this exception or None"""
137
131
fmt = getattr(self, '_fmt', None)
138
132
if fmt is not None:
140
unicode_fmt = unicode(fmt) #_fmt strings should be ascii
141
if type(fmt) == unicode:
142
trace.mutter("Unicode strings in error.fmt are deprecated")
143
return gettext(unicode_fmt)
144
fmt = getattr(self, '__doc__', None)
146
symbol_versioning.warn("%s uses its docstring as a format, "
147
"it should use _fmt instead" % self.__class__.__name__,
150
return 'Unprintable exception %s: dict=%r, fmt=%r' \
151
% (self.__class__.__name__,
153
getattr(self, '_fmt', None),
133
from bzrlib.i18n import gettext
134
return gettext(unicode(fmt)) # _fmt strings should be ascii
156
136
def __eq__(self, other):
157
137
if self.__class__ is not other.__class__:
170
150
internal_error = True
173
class BzrNewError(BzrError):
174
"""Deprecated error base class."""
175
# base classes should override the docstring with their human-
176
# readable explanation
178
def __init__(self, *args, **kwds):
179
# XXX: Use the underlying BzrError to always generate the args
180
# attribute if it doesn't exist. We can't use super here, because
181
# exceptions are old-style classes in python2.4 (but new in 2.5).
183
symbol_versioning.warn('BzrNewError was deprecated in bzr 0.13; '
184
'please convert %s to use BzrError instead'
185
% self.__class__.__name__,
188
BzrError.__init__(self, *args)
189
for key, value in kwds.items():
190
setattr(self, key, value)
194
# __str__() should always return a 'str' object
195
# never a 'unicode' object.
196
s = self.__doc__ % self.__dict__
197
if isinstance(s, unicode):
198
return s.encode('utf8')
200
except (TypeError, NameError, ValueError, KeyError), e:
201
return 'Unprintable exception %s(%r): %r' \
202
% (self.__class__.__name__,
206
153
class AlreadyBuilding(BzrError):
208
155
_fmt = "The tree builder is already building a tree."
790
737
self.path = bzrdir.transport.clone('..').base
793
class FileInWrongBranch(BzrError):
795
_fmt = 'File "%(path)s" is not in branch %(branch_base)s.'
797
# use PathNotChild instead
798
@symbol_versioning.deprecated_method(symbol_versioning.deprecated_in((2, 3, 0)))
799
def __init__(self, branch, path):
800
BzrError.__init__(self)
802
self.branch_base = branch.base
806
740
class UnsupportedFormatError(BzrError):
808
742
_fmt = "Unsupported branch format: %(format)s\nPlease run 'bzr upgrade'"
1296
1230
not_ancestor_id=not_ancestor_id)
1299
class AmbiguousBase(BzrError):
1301
def __init__(self, bases):
1302
symbol_versioning.warn("BzrError AmbiguousBase has been deprecated "
1303
"as of bzrlib 0.8.", DeprecationWarning, stacklevel=2)
1304
msg = ("The correct base is unclear, because %s are all equally close"
1306
BzrError.__init__(self, msg)
1310
1233
class NoCommits(BranchError):
1312
1235
_fmt = "Branch %(branch)s has no commits."
2079
2002
BzrMoveFailedError.__init__(self, from_path, to_path, extra)
2082
class BzrRemoveChangedFilesError(BzrError):
2083
"""Used when user is trying to remove changed files."""
2085
_fmt = ("Can't safely remove modified or unknown files:\n"
2086
"%(changes_as_text)s"
2087
"Use --keep to not delete them, or --force to delete them regardless.")
2089
def __init__(self, tree_delta):
2090
symbol_versioning.warn(symbol_versioning.deprecated_in((2, 3, 0)) %
2091
"BzrRemoveChangedFilesError", DeprecationWarning, stacklevel=2)
2092
BzrError.__init__(self)
2093
self.changes_as_text = tree_delta.get_changes_as_text()
2094
#self.paths_as_string = '\n'.join(changed_files)
2095
#self.paths_as_string = '\n'.join([quotefn(p) for p in changed_files])
2098
2005
class BzrBadParameterNotString(BzrBadParameter):
2100
2007
_fmt = "Parameter %(param)s is not a string or unicode string."
3101
3008
BzrError.__init__(self, unknowns_str=", ".join(unknowns))
3104
class HookFailed(BzrError):
3105
"""Raised when a pre_change_branch_tip hook function fails anything other
3106
than TipChangeRejected.
3108
Note that this exception is no longer raised, and the import is only left
3109
to be nice to code which might catch it in a plugin.
3112
_fmt = ("Hook '%(hook_name)s' during %(hook_stage)s failed:\n"
3113
"%(traceback_text)s%(exc_value)s")
3115
def __init__(self, hook_stage, hook_name, exc_info, warn=True):
3117
symbol_versioning.warn("BzrError HookFailed has been deprecated "
3118
"as of bzrlib 2.1.", DeprecationWarning, stacklevel=2)
3120
self.hook_stage = hook_stage
3121
self.hook_name = hook_name
3122
self.exc_info = exc_info
3123
self.exc_type = exc_info[0]
3124
self.exc_value = exc_info[1]
3125
self.exc_tb = exc_info[2]
3126
self.traceback_text = ''.join(traceback.format_tb(self.exc_tb))
3129
3011
class TipChangeRejected(BzrError):
3130
3012
"""A pre_change_branch_tip hook function may raise this to cleanly and
3131
3013
explicitly abort a change to a branch tip.