comparison mercurial/error.py @ 29509:945b4c14c570

error: make HintException a mix-in class not derived from BaseException (API) HintException is unrelated to the hierarchy of errors. It is an implementation detail whether a class inherits from HintException or not, a sort of "private inheritance" in C++. New Hint isn't an exception class, which prevents catching error by its type: try: dosomething() except error.Hint: pass Unfortunately, this passes on PyPy 5.3.1, but not on Python 2, and raises more detailed TypeError on Python 3.
author Yuya Nishihara <yuya@tcha.org>
date Sat, 09 Jul 2016 14:28:30 +0900
parents 13bb8de97f87
children 19205a0e2bf1
comparison
equal deleted inserted replaced
29508:d65ec41b6384 29509:945b4c14c570
13 13
14 from __future__ import absolute_import 14 from __future__ import absolute_import
15 15
16 # Do not import anything here, please 16 # Do not import anything here, please
17 17
18 class HintException(Exception): 18 class Hint(object):
19 """Mix-in to provide a hint of an error
20
21 This should come first in the inheritance list to consume **kw and pass
22 only *args to the exception class.
23 """
19 def __init__(self, *args, **kw): 24 def __init__(self, *args, **kw):
20 Exception.__init__(self, *args) 25 super(Hint, self).__init__(*args)
21 self.hint = kw.get('hint') 26 self.hint = kw.get('hint')
22 27
23 class RevlogError(HintException): 28 class RevlogError(Hint, Exception):
24 pass 29 pass
25 30
26 class FilteredIndexError(IndexError): 31 class FilteredIndexError(IndexError):
27 pass 32 pass
28 33
48 pass 53 pass
49 54
50 class CommandError(Exception): 55 class CommandError(Exception):
51 """Exception raised on errors in parsing the command line.""" 56 """Exception raised on errors in parsing the command line."""
52 57
53 class InterventionRequired(HintException): 58 class InterventionRequired(Hint, Exception):
54 """Exception raised when a command requires human intervention.""" 59 """Exception raised when a command requires human intervention."""
55 60
56 class Abort(HintException): 61 class Abort(Hint, Exception):
57 """Raised if a command needs to print an error and exit.""" 62 """Raised if a command needs to print an error and exit."""
58 63
59 class HookLoadError(Abort): 64 class HookLoadError(Abort):
60 """raised when loading a hook fails, aborting an operation 65 """raised when loading a hook fails, aborting an operation
61 66
85 """Raised when an EOF is received for a prompt""" 90 """Raised when an EOF is received for a prompt"""
86 def __init__(self): 91 def __init__(self):
87 from .i18n import _ 92 from .i18n import _
88 Abort.__init__(self, _('response expected')) 93 Abort.__init__(self, _('response expected'))
89 94
90 class OutOfBandError(HintException): 95 class OutOfBandError(Hint, Exception):
91 """Exception raised when a remote repo reports failure""" 96 """Exception raised when a remote repo reports failure"""
92 97
93 class ParseError(HintException): 98 class ParseError(Hint, Exception):
94 """Raised when parsing config files and {rev,file}sets (msg[, pos])""" 99 """Raised when parsing config files and {rev,file}sets (msg[, pos])"""
95 100
96 class UnknownIdentifier(ParseError): 101 class UnknownIdentifier(ParseError):
97 """Exception raised when a {rev,file}set references an unknown identifier""" 102 """Exception raised when a {rev,file}set references an unknown identifier"""
98 103
100 from .i18n import _ 105 from .i18n import _
101 ParseError.__init__(self, _("unknown identifier: %s") % function) 106 ParseError.__init__(self, _("unknown identifier: %s") % function)
102 self.function = function 107 self.function = function
103 self.symbols = symbols 108 self.symbols = symbols
104 109
105 class RepoError(HintException): 110 class RepoError(Hint, Exception):
106 pass 111 pass
107 112
108 class RepoLookupError(RepoError): 113 class RepoLookupError(RepoError):
109 pass 114 pass
110 115