Skip to content

Commit ae05c92

Browse files
feat: include should_retry and http_status_code properties in all DeepLExceptions
1 parent abcc0d3 commit ae05c92

File tree

3 files changed

+62
-17
lines changed

3 files changed

+62
-17
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99
### Added
10-
* State explicitly that this library supports Python 3.11.
10+
* State explicitly that this library supports Python 3.11.
11+
* Added the `should_retry` and `http_status_code` properties to all exceptions
12+
thrown by the library.
1113
### Fixed
1214
* Fix `py` dependency by upgrading `pytest` version to 7.2.0 for Python 3.7+.
1315
For Python 3.6 tests `pytest` needs to be added manually.

deepl/exceptions.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,28 @@
22
# Use of this source code is governed by an MIT
33
# license that can be found in the LICENSE file.
44

5+
from typing import Optional
6+
57

68
class DeepLException(Exception):
7-
"""Base class for deepl module exceptions."""
9+
"""Base class for deepl module exceptions.
810
9-
pass
11+
:param message: Message describing the error that occurred.
12+
:param should_retry: True if the request would normally be retried
13+
following this error, otherwise false.
14+
:param http_status_code: The HTTP status code in the response, if
15+
applicable, otherwise None.
16+
"""
17+
18+
def __init__(
19+
self,
20+
message: str,
21+
should_retry: bool = False,
22+
http_status_code: Optional[int] = None,
23+
):
24+
super().__init__(message)
25+
self.should_retry = should_retry
26+
self.http_status_code = http_status_code
1027

1128

1229
class AuthorizationException(DeepLException):
@@ -37,11 +54,13 @@ class ConnectionException(DeepLException):
3754

3855
def __init__(
3956
self,
40-
message,
41-
should_retry=False,
57+
message: str,
58+
should_retry: bool = False,
4259
):
43-
super().__init__(message)
44-
self.should_retry = should_retry
60+
super().__init__(
61+
message,
62+
should_retry=should_retry,
63+
)
4564

4665

4766
class DocumentTranslationException(DeepLException):
@@ -51,7 +70,9 @@ class DocumentTranslationException(DeepLException):
5170
:param document_handle: The document handle of the associated document.
5271
"""
5372

54-
def __init__(self, message, document_handle):
73+
def __init__(
74+
self, message: str, document_handle: "DocumentHandle" # noqa
75+
):
5576
super().__init__(message)
5677
self.document_handle = document_handle
5778

deepl/translator.py

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -558,28 +558,48 @@ def _raise_for_status(
558558
return
559559
elif status_code == http.HTTPStatus.FORBIDDEN:
560560
raise AuthorizationException(
561-
f"Authorization failure, check auth_key{message}"
561+
f"Authorization failure, check auth_key{message}",
562+
http_status_code=status_code,
562563
)
563564
elif status_code == self._HTTP_STATUS_QUOTA_EXCEEDED:
564565
raise QuotaExceededException(
565-
f"Quota for this billing period has been exceeded{message}"
566+
f"Quota for this billing period has been exceeded{message}",
567+
http_status_code=status_code,
566568
)
567569
elif status_code == http.HTTPStatus.NOT_FOUND:
568570
if glossary:
569-
raise GlossaryNotFoundException(f"Glossary not found{message}")
570-
raise DeepLException(f"Not found, check server_url{message}")
571+
raise GlossaryNotFoundException(
572+
f"Glossary not found{message}",
573+
http_status_code=status_code,
574+
)
575+
raise DeepLException(
576+
f"Not found, check server_url{message}",
577+
http_status_code=status_code,
578+
)
571579
elif status_code == http.HTTPStatus.BAD_REQUEST:
572-
raise DeepLException(f"Bad request{message}")
580+
raise DeepLException(
581+
f"Bad request{message}", http_status_code=status_code
582+
)
573583
elif status_code == http.HTTPStatus.TOO_MANY_REQUESTS:
574584
raise TooManyRequestsException(
575585
"Too many requests, DeepL servers are currently experiencing "
576-
f"high load{message}"
586+
f"high load{message}",
587+
should_retry=True,
588+
http_status_code=status_code,
577589
)
578590
elif status_code == http.HTTPStatus.SERVICE_UNAVAILABLE:
579591
if downloading_document:
580-
raise DocumentNotReadyException(f"Document not ready{message}")
592+
raise DocumentNotReadyException(
593+
f"Document not ready{message}",
594+
should_retry=True,
595+
http_status_code=status_code,
596+
)
581597
else:
582-
raise DeepLException(f"Service unavailable{message}")
598+
raise DeepLException(
599+
f"Service unavailable{message}",
600+
should_retry=True,
601+
http_status_code=status_code,
602+
)
583603
else:
584604
status_name = (
585605
http.client.responses[status_code]
@@ -588,7 +608,9 @@ def _raise_for_status(
588608
)
589609
raise DeepLException(
590610
f"Unexpected status code: {status_code} {status_name}, "
591-
f"content: {content}."
611+
f"content: {content}.",
612+
should_retry=False,
613+
http_status_code=status_code,
592614
)
593615

594616
def _check_valid_languages(

0 commit comments

Comments
 (0)