|
@@ -129,6 +129,19 @@ class ContextInterface(object):
|
129
|
129
|
raise NotImplementedError()
|
130
|
130
|
|
131
|
131
|
|
|
132
|
+class HandledException(object):
|
|
133
|
+ """
|
|
134
|
+ Representation of an handled exception with it's http code
|
|
135
|
+ """
|
|
136
|
+ def __init__(
|
|
137
|
+ self,
|
|
138
|
+ exception_class: typing.Type[Exception],
|
|
139
|
+ http_code: int = 500,
|
|
140
|
+ ):
|
|
141
|
+ self.exception_class = exception_class
|
|
142
|
+ self.http_code = http_code
|
|
143
|
+
|
|
144
|
+
|
132
|
145
|
class BaseContext(ContextInterface):
|
133
|
146
|
def get_default_error_builder(self) -> ErrorBuilderInterface:
|
134
|
147
|
""" see hapic.context.ContextInterface#get_default_error_builder"""
|
|
@@ -166,17 +179,17 @@ class BaseContext(ContextInterface):
|
166
|
179
|
except Exception as exc:
|
167
|
180
|
# Reverse list to read first user given exception before
|
168
|
181
|
# the hapic default Exception catch
|
169
|
|
- handled = reversed(
|
|
182
|
+ handled_exceptions = reversed(
|
170
|
183
|
self._get_handled_exception_class_and_http_codes(),
|
171
|
184
|
)
|
172
|
|
- for handled_exception_class, http_code in handled:
|
|
185
|
+ for handled_exception in handled_exceptions:
|
173
|
186
|
# TODO BS 2018-05-04: How to be attentive to hierarchy ?
|
174
|
|
- if isinstance(exc, handled_exception_class):
|
|
187
|
+ if isinstance(exc, handled_exception.exception_class):
|
175
|
188
|
error_builder = self.get_default_error_builder()
|
176
|
189
|
error_body = error_builder.build_from_exception(exc)
|
177
|
190
|
return self.get_response(
|
178
|
191
|
json.dumps(error_body),
|
179
|
|
- http_code,
|
|
192
|
+ handled_exception.http_code,
|
180
|
193
|
)
|
181
|
194
|
raise exc
|
182
|
195
|
|
|
@@ -184,7 +197,7 @@ class BaseContext(ContextInterface):
|
184
|
197
|
|
185
|
198
|
def _get_handled_exception_class_and_http_codes(
|
186
|
199
|
self,
|
187
|
|
- ) -> typing.List[typing.Tuple[typing.Type[Exception], int]]:
|
|
200
|
+ ) -> typing.List[HandledException]:
|
188
|
201
|
"""
|
189
|
202
|
:return: A list of tuple where: thirst item of tuple is a exception
|
190
|
203
|
class and second tuple item is a http code. This list will be used by
|