comparison mercurial/error.py @ 47290:5e736d2e9703

errors: move Abort earlier, so more exceptions can subclass it I'd like to make at least `InterventionRequired` subclass `Abort` and Python requires the superclass to be defined before the subtype. Differential Revision: https://phab.mercurial-scm.org/D10736
author Martin von Zweigbergk <martinvonz@google.com>
date Tue, 18 May 2021 21:58:12 -0700
parents 33c0c25d0b0f
children d9c71bbe20f7
comparison
equal deleted inserted replaced
47289:33c0c25d0b0f 47290:5e736d2e9703
49 def __init__(self, *args, **kw): 49 def __init__(self, *args, **kw):
50 self.hint = kw.pop('hint', None) 50 self.hint = kw.pop('hint', None)
51 super(Hint, self).__init__(*args, **kw) 51 super(Hint, self).__init__(*args, **kw)
52 52
53 53
54 class Abort(Hint, Exception):
55 """Raised if a command needs to print an error and exit."""
56
57 def __init__(self, message, hint=None, detailed_exit_code=None):
58 # type: (bytes, Optional[bytes]) -> None
59 self.message = message
60 self.hint = hint
61 self.detailed_exit_code = detailed_exit_code
62 # Pass the message into the Exception constructor to help extensions
63 # that look for exc.args[0].
64 Exception.__init__(self, message)
65
66 def __bytes__(self):
67 return self.message
68
69 if pycompat.ispy3:
70
71 def __str__(self):
72 # the output would be unreadable if the message was translated,
73 # but do not replace it with encoding.strfromlocal(), which
74 # may raise another exception.
75 return pycompat.sysstr(self.__bytes__())
76
77 def format(self):
78 # type: () -> bytes
79 from .i18n import _
80
81 message = _(b"abort: %s\n") % self.message
82 if self.hint:
83 message += _(b"(%s)\n") % self.hint
84 return message
85
86
54 class StorageError(Hint, Exception): 87 class StorageError(Hint, Exception):
55 """Raised when an error occurs in a storage layer. 88 """Raised when an error occurs in a storage layer.
56 89
57 Usually subclassed by a storage-specific exception. 90 Usually subclassed by a storage-specific exception.
58 """ 91 """
178 _( 211 _(
179 b"unresolved conflicts (see 'hg resolve', then 'hg %s --continue')" 212 b"unresolved conflicts (see 'hg resolve', then 'hg %s --continue')"
180 ) 213 )
181 % opname, 214 % opname,
182 ) 215 )
183
184
185 class Abort(Hint, Exception):
186 """Raised if a command needs to print an error and exit."""
187
188 def __init__(self, message, hint=None, detailed_exit_code=None):
189 # type: (bytes, Optional[bytes]) -> None
190 self.message = message
191 self.hint = hint
192 self.detailed_exit_code = detailed_exit_code
193 # Pass the message into the Exception constructor to help extensions
194 # that look for exc.args[0].
195 Exception.__init__(self, message)
196
197 def __bytes__(self):
198 return self.message
199
200 if pycompat.ispy3:
201
202 def __str__(self):
203 # the output would be unreadable if the message was translated,
204 # but do not replace it with encoding.strfromlocal(), which
205 # may raise another exception.
206 return pycompat.sysstr(self.__bytes__())
207
208 def format(self):
209 # type: () -> bytes
210 from .i18n import _
211
212 message = _(b"abort: %s\n") % self.message
213 if self.hint:
214 message += _(b"(%s)\n") % self.hint
215 return message
216 216
217 217
218 class InputError(Abort): 218 class InputError(Abort):
219 """Indicates that the user made an error in their input. 219 """Indicates that the user made an error in their input.
220 220