Skip to content

ding.interaction.exception.base

ding.interaction.exception.base

ResponseException

Bases: Exception, _IResponseInformation

Overview

Response exception, which can be directly raised in methods to create fail http response.

status_code property

Overview

Get http status code of response

Returns: - status_code (:obj:int): Http status code

__init__(error)

Overview

Constructor of ResponseException

Arguments: - error (:obj:HTTPError): Original http exception object

Full Source Code

../ding/interaction/exception/base.py

1from abc import ABCMeta 2from typing import Mapping, Any 3 4from requests.exceptions import HTTPError 5 6from ..base import get_values_from_response 7 8 9class _IResponseInformation(metaclass=ABCMeta): 10 """ 11 Overview: 12 Response information basic structure interface 13 """ 14 15 @property 16 def success(self) -> bool: 17 """ 18 Overview: 19 Get response success or not 20 Returns: 21 - success (:obj:`bool`): Response success or not 22 """ 23 raise NotImplementedError 24 25 @property 26 def code(self) -> int: 27 """ 28 Overview: 29 Get response error code (`0` means success) 30 Returns: 31 - code (:obj:`int`): Response error code 32 """ 33 raise NotImplementedError 34 35 @property 36 def message(self) -> str: 37 """ 38 Overview: 39 Get response message 40 Returns: 41 - message (:obj:`str`): Response message 42 """ 43 raise NotImplementedError 44 45 @property 46 def data(self) -> Mapping[str, Any]: 47 """ 48 Overview: 49 Get response data 50 Returns: 51 - data (:obj:`Mapping[str, Any]`): Response data 52 """ 53 raise NotImplementedError 54 55 56# exception class for processing response 57class ResponseException(Exception, _IResponseInformation, metaclass=ABCMeta): 58 """ 59 Overview: 60 Response exception, which can be directly raised in methods to create fail http response. 61 """ 62 63 def __init__(self, error: HTTPError): 64 """ 65 Overview: 66 Constructor of `ResponseException` 67 Arguments: 68 - error (:obj:`HTTPError`): Original http exception object 69 """ 70 self.__error = error 71 self.__status_code, self.__success, self.__code, self.__message, self.__data = \ 72 get_values_from_response(error.response) 73 Exception.__init__(self, self.__message) 74 75 @property 76 def status_code(self) -> int: 77 """ 78 Overview: 79 Get http status code of response 80 Returns: 81 - status_code (:obj:`int`): Http status code 82 """ 83 return self.__status_code 84 85 @property 86 def success(self) -> bool: 87 return self.__success 88 89 @property 90 def code(self) -> int: 91 return self.__code 92 93 @property 94 def message(self) -> str: 95 return self.__message 96 97 @property 98 def data(self) -> Mapping[str, Any]: 99 return self.__data