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

tracim_backend.views.errors   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 22
dl 0
loc 33
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A ErrorSchema.build_from_validation_error() 0 7 1
A ErrorSchema.build_from_exception() 0 13 1
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