Passed
Pull Request — develop (#47)
by inkhey
01:32
created

ErrorSchema.build_from_exception()   A

Complexity

Conditions 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 13
rs 9.85
c 0
b 0
f 0
cc 1
nop 3
1
from hapic.error import DefaultErrorBuilder
2
from hapic.processor import ProcessValidationError
3
from tracim_backend.error_code import ERROR_CODE_GENERIC_SCHEMA_VALIDATION_ERROR
4
5
6
class ErrorSchema(DefaultErrorBuilder):
7
    """
8
    This class is both a builder and a Marshmallow Schema, His named is used for
9
    swagger ui error schema. That's why we call it ErrorSchema To have
10
    a nice naming in swagger ui.
11
    """
12
    def build_from_exception(
13
        self,
14
        exception: Exception,
15
        include_traceback: bool = False,
16
    ) -> dict:
17
        error_dict = DefaultErrorBuilder.build_from_exception(
18
            self,
19
            exception,
20
            include_traceback
21
        )
22
        code = getattr(exception, 'error_code', None)
23
        error_dict['code'] = code
24
        return error_dict
25
26
    def build_from_validation_error(
27
        self,
28
        error: ProcessValidationError,
29
    ) -> dict:
30
        error_dict = DefaultErrorBuilder.build_from_validation_error(self, error)  # nopep8
31
        error_dict['code'] = ERROR_CODE_GENERIC_SCHEMA_VALIDATION_ERROR
32
        return error_dict
33