Example #1
0
    def get_request_data(response: Response) -> Optional[JsonType]:
        if response.get_raw_data() == "":
            return None

        try:
            response_json = response.get_json()
        except JSONDecodeError as error:
            raise ResponseParseError(
                f"Unable to decode response data to json. data='{response.get_raw_data()}'"
            ) from error
        return response_json
Example #2
0
    def get_exception(response: Response) -> APIRequestError:
        if response.get_raw_data() == "":
            return ErrorHandler.get_exception(response)

        key_fields = ("errorCode", "error_code")
        error_code = None
        try:
            data = response.get_json()
        except JSONDecodeError:
            return ErrorHandler.get_exception(response)

        for key_field in key_fields:
            try:
                error_code = int(data.get(key_field))
                if error_code is not None:
                    break
            except (ValueError, TypeError):
                pass

        exception_class = ERROR_CODES_WITH_EXCEPTIONS_MAP.get(error_code)
        if not exception_class:
            return ErrorHandler.get_exception(response)

        return exception_class()