|
@@ -1,4 +1,6 @@
|
1
|
1
|
# -*- coding: utf-8 -*-
|
|
2
|
+import traceback
|
|
3
|
+
|
2
|
4
|
import marshmallow
|
3
|
5
|
|
4
|
6
|
from hapic.processor import ProcessValidationError
|
|
@@ -9,7 +11,11 @@ class ErrorBuilderInterface(marshmallow.Schema):
|
9
|
11
|
ErrorBuilder is a class who represent a Schema (marshmallow.Schema) and
|
10
|
12
|
can generate a response content from exception (build_from_exception)
|
11
|
13
|
"""
|
12
|
|
- def build_from_exception(self, exception: Exception) -> dict:
|
|
14
|
+ def build_from_exception(
|
|
15
|
+ self,
|
|
16
|
+ exception: Exception,
|
|
17
|
+ include_traceback: bool = False,
|
|
18
|
+ ) -> dict:
|
13
|
19
|
"""
|
14
|
20
|
Build the error response content from given exception
|
15
|
21
|
:param exception: Original exception who invoke this method
|
|
@@ -34,14 +40,28 @@ class DefaultErrorBuilder(ErrorBuilderInterface):
|
34
|
40
|
details = marshmallow.fields.Dict(required=False, missing={})
|
35
|
41
|
code = marshmallow.fields.Raw(missing=None)
|
36
|
42
|
|
37
|
|
- def build_from_exception(self, exception: Exception) -> dict:
|
|
43
|
+ def build_from_exception(
|
|
44
|
+ self,
|
|
45
|
+ exception: Exception,
|
|
46
|
+ include_traceback: bool = False,
|
|
47
|
+ ) -> dict:
|
38
|
48
|
"""
|
39
|
49
|
See hapic.error.ErrorBuilderInterface#build_from_exception docstring
|
40
|
50
|
"""
|
41
|
51
|
# TODO: "error_detail" attribute name should be configurable
|
|
52
|
+ message = str(exception)
|
|
53
|
+ if not message:
|
|
54
|
+ message = type(exception).__name__
|
|
55
|
+
|
|
56
|
+ details = {
|
|
57
|
+ 'error_detail': getattr(exception, 'error_detail', {}),
|
|
58
|
+ }
|
|
59
|
+ if include_traceback:
|
|
60
|
+ details['traceback'] = traceback.format_exc()
|
|
61
|
+
|
42
|
62
|
return {
|
43
|
|
- 'message': str(exception),
|
44
|
|
- 'details': getattr(exception, 'error_detail', {}),
|
|
63
|
+ 'message': message,
|
|
64
|
+ 'details': details,
|
45
|
65
|
'code': None,
|
46
|
66
|
}
|
47
|
67
|
|