From 9a2472faf5fe01c2fad3346dbf6fbde667e30d68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Chr=C3=A1stek?= Date: Wed, 7 May 2025 10:39:00 +0200 Subject: [PATCH] Feat: Add more specific exception classes for HTTP status codes This commit introduces new exception classes for specific HTTP status codes, including: - MethodNotAllowedException (405) - TimeoutException (408) - GoneException (410) - UnsupportedMediaTypeException (415) - TooManyRequestsException (429) --- phrasetms_client/exceptions.py | 38 ++++++++++++++++++++++++++++++---- phrasetms_client/rest.py | 20 +++++++++++++++--- 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/phrasetms_client/exceptions.py b/phrasetms_client/exceptions.py index a21f8523..23bc41bd 100644 --- a/phrasetms_client/exceptions.py +++ b/phrasetms_client/exceptions.py @@ -133,22 +133,52 @@ def __init__(self, status=None, reason=None, http_resp=None): super(BadRequestException, self).__init__(status, reason, http_resp) +class UnauthorizedException(ApiException): + + def __init__(self, status=None, reason=None, http_resp=None): + super(UnauthorizedException, self).__init__(status, reason, http_resp) + + +class ForbiddenException(ApiException): + + def __init__(self, status=None, reason=None, http_resp=None): + super(ForbiddenException, self).__init__(status, reason, http_resp) + + class NotFoundException(ApiException): def __init__(self, status=None, reason=None, http_resp=None): super(NotFoundException, self).__init__(status, reason, http_resp) -class UnauthorizedException(ApiException): +class MethodNotAllowedException(ApiException): def __init__(self, status=None, reason=None, http_resp=None): - super(UnauthorizedException, self).__init__(status, reason, http_resp) + super(MethodNotAllowedException, self).__init__(status, reason, http_resp) -class ForbiddenException(ApiException): +class TimeoutException(ApiException): def __init__(self, status=None, reason=None, http_resp=None): - super(ForbiddenException, self).__init__(status, reason, http_resp) + super(TimeoutException, self).__init__(status, reason, http_resp) + + +class GoneException(ApiException): + + def __init__(self, status=None, reason=None, http_resp=None): + super(GoneException, self).__init__(status, reason, http_resp) + + +class UnsupportedMediaTypeException(ApiException): + + def __init__(self, status=None, reason=None, http_resp=None): + super(UnsupportedMediaTypeException, self).__init__(status, reason, http_resp) + + +class TooManyRequestsException(ApiException): + + def __init__(self, status=None, reason=None, http_resp=None): + super(TooManyRequestsException, self).__init__(status, reason, http_resp) class ServiceException(ApiException): diff --git a/phrasetms_client/rest.py b/phrasetms_client/rest.py index 077433f3..f07f2169 100644 --- a/phrasetms_client/rest.py +++ b/phrasetms_client/rest.py @@ -21,7 +21,7 @@ from urllib.parse import urlencode, quote_plus import urllib3 -from phrasetms_client.exceptions import ApiException, UnauthorizedException, ForbiddenException, NotFoundException, ServiceException, ApiValueError, BadRequestException +from phrasetms_client.exceptions import ApiException, GoneException, MethodNotAllowedException, TimeoutException, TooManyRequestsException, UnauthorizedException, ForbiddenException, NotFoundException, ServiceException, ApiValueError, BadRequestException, UnsupportedMediaTypeException logger = logging.getLogger(__name__) @@ -69,7 +69,6 @@ def __init__(self, configuration, pools_size=4, maxsize=None): if configuration.tls_server_name: addition_pool_args['server_hostname'] = configuration.tls_server_name - if configuration.socket_options is not None: addition_pool_args['socket_options'] = configuration.socket_options @@ -220,7 +219,7 @@ def request(self, method, url, query_params=None, headers=None, if not 200 <= r.status <= 299: if r.status == 400: raise BadRequestException(http_resp=r) - + if r.status == 401: raise UnauthorizedException(http_resp=r) @@ -230,6 +229,21 @@ def request(self, method, url, query_params=None, headers=None, if r.status == 404: raise NotFoundException(http_resp=r) + if r.status == 405: + raise MethodNotAllowedException(http_resp=r) + + if r.status == 408: + raise TimeoutException(http_resp=r) + + if r.status == 410: + raise GoneException(http_resp=r) + + if r.status == 415: + raise UnsupportedMediaTypeException(http_resp=r) + + if r.status == 429: + raise TooManyRequestsException(http_resp=r) + if 500 <= r.status <= 599: raise ServiceException(http_resp=r)