|
| 1 | +from bunq.sdk.exception import BadRequestException |
| 2 | +from bunq.sdk.exception import UnauthorizedException |
| 3 | +from bunq.sdk.exception import ForbiddenException |
| 4 | +from bunq.sdk.exception import NotFoundException |
| 5 | +from bunq.sdk.exception import MethodNotAllowedException |
| 6 | +from bunq.sdk.exception import TooManyRequestsException |
| 7 | +from bunq.sdk.exception import PleaseContactBunqException |
| 8 | +from bunq.sdk.exception import UnknownApiErrorException |
| 9 | +from bunq.sdk.exception import ApiException |
| 10 | + |
| 11 | + |
| 12 | +class ExceptionFactory: |
| 13 | + # Error response code constants |
| 14 | + _HTTP_RESPONSE_CODE_BAD_REQUEST = 400 |
| 15 | + _HTTP_RESPONSE_CODE_UNAUTHORIZED = 401 |
| 16 | + _HTTP_RESPONSE_CODE_FORBIDDEN = 403 |
| 17 | + _HTTP_RESPONSE_CODE_NOT_FOUND = 404 |
| 18 | + _HTTP_RESPONSE_CODE_METHOD_NOT_ALLOWED = 405 |
| 19 | + _HTTP_RESPONSE_CODE_TOO_MANY_REQUESTS = 429 |
| 20 | + _HTTP_RESPONSE_CODE_INTERNAL_SERVER_ERROR = 500 |
| 21 | + |
| 22 | + # Constants for formatting messages |
| 23 | + _FORMAT_RESPONSE_CODE_LINE = 'HTTP Response Code: {}' |
| 24 | + _GLUE_ERROR_MESSAGES = '\n' |
| 25 | + |
| 26 | + @classmethod |
| 27 | + def create_exception_for_response(cls, response_code, messages): |
| 28 | + """ |
| 29 | + :type response_code: int |
| 30 | + :type messages: list[str] |
| 31 | +
|
| 32 | + :return: The exception according to the status code. |
| 33 | + :rtype: ApiException |
| 34 | + """ |
| 35 | + |
| 36 | + error_message = cls._generate_message_error(response_code, messages) |
| 37 | + |
| 38 | + if response_code == cls._HTTP_RESPONSE_CODE_BAD_REQUEST: |
| 39 | + return BadRequestException(error_message, response_code) |
| 40 | + if response_code == cls._HTTP_RESPONSE_CODE_UNAUTHORIZED: |
| 41 | + return UnauthorizedException(error_message, response_code) |
| 42 | + if response_code == cls._HTTP_RESPONSE_CODE_FORBIDDEN: |
| 43 | + return ForbiddenException(error_message, response_code) |
| 44 | + if response_code == cls._HTTP_RESPONSE_CODE_NOT_FOUND: |
| 45 | + return NotFoundException(error_message, response_code) |
| 46 | + if response_code == cls._HTTP_RESPONSE_CODE_METHOD_NOT_ALLOWED: |
| 47 | + return MethodNotAllowedException(error_message, response_code) |
| 48 | + if response_code == cls._HTTP_RESPONSE_CODE_TOO_MANY_REQUESTS: |
| 49 | + return TooManyRequestsException(error_message, response_code) |
| 50 | + if response_code == cls._HTTP_RESPONSE_CODE_INTERNAL_SERVER_ERROR: |
| 51 | + return PleaseContactBunqException(error_message, response_code) |
| 52 | + |
| 53 | + return UnknownApiErrorException(error_message, response_code) |
| 54 | + |
| 55 | + @classmethod |
| 56 | + def _generate_message_error(cls, response_code, messages): |
| 57 | + """ |
| 58 | + :type response_code: int |
| 59 | + :type messages: list[str] |
| 60 | +
|
| 61 | + :rtype: str |
| 62 | + """ |
| 63 | + |
| 64 | + line_response_code = cls._FORMAT_RESPONSE_CODE_LINE \ |
| 65 | + .format(response_code) |
| 66 | + |
| 67 | + return cls._glue_messages([line_response_code] + messages) |
| 68 | + |
| 69 | + @classmethod |
| 70 | + def _glue_messages(cls, messages): |
| 71 | + """ |
| 72 | + :type messages: list[str] |
| 73 | +
|
| 74 | + :rtype: str |
| 75 | + """ |
| 76 | + |
| 77 | + return cls._GLUE_ERROR_MESSAGES.join(messages) |
0 commit comments