Skip to content

Commit 11ab183

Browse files
hive-botclaude
andcommitted
fix: wrap document upload failures in DocumentTranslationException
translate_document() called translate_document_upload() outside its error handling, so an upload failure raised the underlying exception (e.g. QuotaExceededException) instead of DocumentTranslationException. Upload failures are now wrapped too, with document_handle set to None and the original exception as the cause. Argument errors (ValueError, TypeError) are still raised unwrapped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 4ab8939 commit 11ab183

3 files changed

Lines changed: 48 additions & 21 deletions

File tree

‎deepl/exceptions.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,19 @@ class DocumentTranslationException(DeepLException):
7676
"""Error occurred while translating document.
7777
7878
:param message: Message describing the error that occurred.
79-
:param document_handle: The document handle of the associated document.
79+
:param document_handle: The document handle of the associated document,
80+
or None if the error occurred before the document was uploaded.
8081
"""
8182

82-
def __init__(self, message: str, document_handle: DocumentHandle):
83+
def __init__(
84+
self, message: str, document_handle: Optional[DocumentHandle]
85+
):
8386
super().__init__(message)
8487
self.document_handle = document_handle
8588

8689
def __str__(self):
90+
if self.document_handle is None:
91+
return super().__str__()
8792
return f"{super().__str__()}, document handle: {self.document_handle}"
8893

8994
@property

‎deepl/translator.py‎

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -649,8 +649,8 @@ def translate_document_from_filepath(
649649
allows the number of billed characters to be queried.
650650
651651
:raises DocumentTranslationException: If an error occurs during
652-
translation. The exception includes information about the document
653-
request.
652+
upload or translation. The exception includes the document handle
653+
if the document was already uploaded, otherwise the handle is None.
654654
"""
655655
_check_target_lang_given(
656656
"translate_document_from_filepath", target_lang
@@ -757,26 +757,33 @@ def translate_document(
757757
allows the number of billed characters to be queried.
758758
759759
:raises DocumentTranslationException: If an error occurs during
760-
translation, the exception includes the document handle.
760+
upload or translation. The exception includes the document handle
761+
if the document was already uploaded, otherwise the handle is None.
761762
"""
762763
_check_target_lang_given("translate_document", target_lang)
763764

764-
handle = self.translate_document_upload(
765-
input_document,
766-
target_lang=target_lang,
767-
source_lang=source_lang,
768-
formality=formality,
769-
glossary=glossary,
770-
glossary_ids=glossary_ids,
771-
style_rule=style_rule,
772-
style_profile=style_profile,
773-
translation_memory=translation_memory,
774-
translation_memory_threshold=translation_memory_threshold,
775-
filename=filename,
776-
output_format=output_format,
777-
enable_watermark=enable_watermark,
778-
extra_body_parameters=extra_body_parameters,
779-
)
765+
try:
766+
handle = self.translate_document_upload(
767+
input_document,
768+
target_lang=target_lang,
769+
source_lang=source_lang,
770+
formality=formality,
771+
glossary=glossary,
772+
glossary_ids=glossary_ids,
773+
style_rule=style_rule,
774+
style_profile=style_profile,
775+
translation_memory=translation_memory,
776+
translation_memory_threshold=translation_memory_threshold,
777+
filename=filename,
778+
output_format=output_format,
779+
enable_watermark=enable_watermark,
780+
extra_body_parameters=extra_body_parameters,
781+
)
782+
except (TypeError, ValueError):
783+
# Invalid arguments, rejected before anything is sent.
784+
raise
785+
except Exception as e:
786+
raise DocumentTranslationException(str(e), None) from e
780787

781788
try:
782789
status = self.translate_document_wait_until_done(handle, timeout_s)

‎tests/offline/test_translate_document.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,21 @@ def test_raises_with_handle_when_document_translation_fails(mocked):
481481
assert exc_info.value.document_handle.document_id == "doc-1"
482482

483483

484+
def test_wraps_upload_failure_without_handle(mocked):
485+
mocked.post(url("v2/document"), status=456, json={"message": "limit"})
486+
487+
with pytest.raises(deepl.DocumentTranslationException) as exc_info:
488+
make_translator().translate_document(
489+
b"hello", io.BytesIO(), target_lang="DE", filename="a.txt"
490+
)
491+
492+
assert exc_info.value.document_handle is None
493+
assert isinstance(exc_info.value.__cause__, deepl.QuotaExceededException)
494+
assert "Quota for this billing period has been exceeded" in str(
495+
exc_info.value
496+
)
497+
498+
484499
def test_raises_with_handle_when_document_status_fails(mocked):
485500
reply_with_handle(
486501
mocked,

0 commit comments

Comments
 (0)