Esempio n. 1
0
 def handle_exception(self, exc: APIException) -> Response:
     response = super().handle_exception(exc)
     response.data["status"] = "error"
     response.data["error_code"] = exc.get_codes()
     response.data["error_message"] = exc.detail
     del response.data["detail"]
     return response
Esempio n. 2
0
 def _deal_other_error(cls, exc: APIException) -> list:
     """逻辑代码raise的error处理"""
     return [
         {
             "type": cls._get_error_type(exc),
             "code": exc.get_codes(),
             "message": exc.detail,
         }
     ]
Esempio n. 3
0
def get_code(exc: APIException) -> str:
    try:
        code = exc.get_codes()
    except AttributeError:
        try:
            code = exc.__class__.default_code
        except AttributeError:
            # Django's exceptions do not have a code attribute at all
            # So we just convert the class name to a lowercase snake_case string
            code = camel_to_snake(exc.__class__.__name__)

    if type(code) == dict:
        code = list(code.values())[0]

    return str(code)
Esempio n. 4
0
def get_code(exc: APIException) -> str:
    try:
        code = exc.get_codes()
    except AttributeError:
        try:
            code = exc.__class__.default_code
        except AttributeError:
            # Django's exceptions do not have a code attribute at all
            # So we just convert the class name to a lowercase snake_case string
            code = camel_to_snake(exc.__class__.__name__)

    if type(code) == dict or issubclass(type(code), dict):
        code = list(code.values())[0]

        # Some error codes may be nested one level deeper
        # unpack those as well
        if type(code) == list:
            code = code[0]

    return str(code)