Skip to content

Commit a91f1dc

Browse files
Add error_message to DocumentStatus, improve DocumentTranslationException message
1 parent ce6bd30 commit a91f1dc

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77

8+
## [Unreleased]
9+
### Added
10+
* Add `error_message` property to `DocumentStatus`, describing the error in case of document translation failure.
11+
### Changed
12+
### Deprecated
13+
### Removed
14+
### Fixed
15+
### Security
16+
17+
818
## [1.5.1] - 2022-04-11
919
### Fixed
1020
* Fix bug in CLI languages command causing some target languages to be omitted.
@@ -138,6 +148,7 @@ Version increased to avoid conflicts with old packages on PyPI.
138148
Initial version.
139149

140150

151+
[Unreleased]: https://github.com/DeepLcom/deepl-python/compare/v1.5.1...HEAD
141152
[1.5.1]: https://github.com/DeepLcom/deepl-python/compare/v1.5.0...v1.5.1
142153
[1.5.0]: https://github.com/DeepLcom/deepl-python/compare/v1.4.1...v1.5.0
143154
[1.4.1]: https://github.com/DeepLcom/deepl-python/compare/v1.4.0...v1.4.1

deepl/translator.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ class DocumentStatus:
7777
completes in seconds, or None if unknown.
7878
:param billed_characters: Number of characters billed for this document, or
7979
None if unknown or before translation is complete.
80+
:param error_message: A short description of the error, or None if no error
81+
has occurred.
8082
"""
8183

8284
class Status(Enum):
@@ -87,11 +89,16 @@ class Status(Enum):
8789
ERROR = "error"
8890

8991
def __init__(
90-
self, status: Status, seconds_remaining=None, billed_characters=None
92+
self,
93+
status: Status,
94+
seconds_remaining=None,
95+
billed_characters=None,
96+
error_message=None,
9197
):
9298
self._status = self.Status(status)
9399
self._seconds_remaining = seconds_remaining
94100
self._billed_characters = billed_characters
101+
self._error_message = error_message
95102

96103
def __str__(self) -> str:
97104
return self.status.value
@@ -116,6 +123,10 @@ def seconds_remaining(self) -> Optional[int]:
116123
def billed_characters(self) -> Optional[int]:
117124
return self._billed_characters
118125

126+
@property
127+
def error_message(self) -> Optional[int]:
128+
return self._error_message
129+
119130

120131
class GlossaryInfo:
121132
"""Information about a glossary, excluding the entry list.
@@ -850,8 +861,10 @@ def translate_document(
850861
raise DocumentTranslationException(str(e), handle) from e
851862

852863
if not status.ok:
864+
error_message = status.error_message or "unknown error"
853865
raise DocumentTranslationException(
854-
"Error occurred while translating document", handle
866+
f"Error occurred while translating document: {error_message}",
867+
handle,
855868
)
856869

857870
def translate_document_upload(
@@ -923,7 +936,10 @@ def translate_document_get_status(
923936
status = json["status"]
924937
seconds_remaining = json.get("seconds_remaining", None)
925938
billed_characters = json.get("billed_characters", None)
926-
return DocumentStatus(status, seconds_remaining, billed_characters)
939+
error_message = json.get("error_message", None)
940+
return DocumentStatus(
941+
status, seconds_remaining, billed_characters, error_message
942+
)
927943

928944
def translate_document_download(
929945
self,

tests/test_translate_document.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def test_translate_document_with_waiting(
7171

7272

7373
@needs_mock_server
74-
def test_translate_document(
74+
def test_translate_large_document(
7575
translator, example_large_document_path, example_large_document_translation
7676
):
7777
with io.BytesIO() as output_file:
@@ -107,12 +107,11 @@ def test_translate_document_formality(
107107
assert "Wie geht es dir?" == output_document_path.read_text()
108108

109109

110-
@needs_mock_server
111-
def test_document_failure(
112-
translator, server, example_document_path, output_document_path
110+
def test_document_failure_during_translation(
111+
translator, example_document_path, output_document_path
113112
):
114-
server.set_doc_failure(1)
115-
113+
# Translating text from DE to DE will trigger error
114+
example_document_path.write_text(example_text["DE"])
116115
with pytest.raises(
117116
deepl.exceptions.DocumentTranslationException,
118117
) as exc_info:
@@ -122,6 +121,7 @@ def test_document_failure(
122121

123122
# Ensure that document translation error contains document handle
124123
exception = exc_info.value
124+
assert "Source and target language" in str(exception)
125125
assert exception.document_handle is not None
126126
match = re.compile("ID: [0-9A-F]{32}, key: [0-9A-F]{64}")
127127
assert match.search(str(exception)) is not None

0 commit comments

Comments
 (0)