Skip to content

Commit fe95bc3

Browse files
Raise DocumentNotReadyException when attempting to download a document before it has been translated
1 parent 1fd3140 commit fe95bc3

File tree

5 files changed

+22
-4
lines changed

5 files changed

+22
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
* Add `--glossary-id` argument for CLI document command.
1212
### Changed
1313
* Improve README.
14+
* Raise `DocumentNotReadyException` when attempting to download a document before it has been translated. Previously the
15+
base class `DeepLException` was thrown.
1416
### Deprecated
1517
### Removed
1618
### Fixed

deepl/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
AuthorizationException,
1111
ConnectionException,
1212
DeepLException,
13+
DocumentNotReadyException,
1314
DocumentTranslationException,
1415
GlossaryNotFoundException,
1516
TooManyRequestsException,

deepl/exceptions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,9 @@ class GlossaryNotFoundException(DeepLException):
5454
"""The specified glossary was not found."""
5555

5656
pass
57+
58+
59+
class DocumentNotReadyException(DeepLException):
60+
"""The translation of the specified document is not yet complete."""
61+
62+
pass

deepl/translator.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from . import http_client, util
66
from .exceptions import (
7+
DocumentNotReadyException,
78
GlossaryNotFoundException,
89
QuotaExceededException,
910
TooManyRequestsException,
@@ -494,6 +495,7 @@ def _raise_for_status(
494495
content: str,
495496
json: Optional[dict],
496497
glossary: bool = False,
498+
downloading_document: bool = False,
497499
):
498500
message = ""
499501
if json is not None and "message" in json:
@@ -522,6 +524,11 @@ def _raise_for_status(
522524
"Too many requests, DeepL servers are currently experiencing "
523525
f"high load{message}"
524526
)
527+
elif status_code == http.HTTPStatus.SERVICE_UNAVAILABLE:
528+
if downloading_document:
529+
raise DocumentNotReadyException(f"Document not ready{message}")
530+
else:
531+
raise DeepLException(f"Service unavailable{message}")
525532
else:
526533
status_name = (
527534
http.client.responses[status_code]
@@ -939,9 +946,9 @@ def translate_document_download(
939946
url, data=data, stream=True
940947
)
941948

942-
if status_code == http.HTTPStatus.SERVICE_UNAVAILABLE:
943-
raise DeepLException("Document not ready for download")
944-
self._raise_for_status(status_code, "<file>", json)
949+
self._raise_for_status(
950+
status_code, "<file>", json, downloading_document=True
951+
)
945952

946953
if output_file:
947954
for chunk in response.iter_content(chunk_size=chunk_size):

tests/test_translate_document.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ def test_translate_document_low_level(
157157
assert status.ok and not status.done
158158

159159
# Calling download() before document is ready will fail
160-
with pytest.raises(deepl.DeepLException, match="Document not ready"):
160+
with pytest.raises(
161+
deepl.DocumentNotReadyException, match="Document not ready"
162+
):
161163
with open(output_document_path, "wb") as output_file:
162164
translator.translate_document_download(handle, output_file)
163165

0 commit comments

Comments
 (0)