Skip to content

Commit bc5857f

Browse files
Deprecate DocumentTranslationException.document_request, use document_handle instead
1 parent 9b88f6a commit bc5857f

3 files changed

Lines changed: 34 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
* Add support for HTML tag handling in `translate_text()`.
1111
### Changed
1212
### Deprecated
13+
* `DocumentTranslationException.document_request` is deprecated, use `document_handle` instead.
1314
### Removed
1415
### Fixed
1516
### Security

deepl/exceptions.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ class TooManyRequestsException(DeepLException):
2828

2929

3030
class ConnectionException(DeepLException):
31-
"""Connection to the DeepL API failed."""
31+
"""Connection to the DeepL API failed.
32+
33+
:param message: Message describing the error that occurred.
34+
:param should_retry: True if the request would normally be retried
35+
following this error, otherwise false.
36+
"""
3237

3338
def __init__(
3439
self,
@@ -40,16 +45,28 @@ def __init__(
4045

4146

4247
class DocumentTranslationException(DeepLException):
43-
"""Error occurred while translating document."""
48+
"""Error occurred while translating document.
49+
50+
:param message: Message describing the error that occurred.
51+
:param document_handle: The document handle of the associated document.
52+
"""
4453

45-
def __init__(self, message, document_request):
54+
def __init__(self, message, document_handle):
4655
super().__init__(message)
47-
self.document_request = document_request
56+
self.document_handle = document_handle
4857

4958
def __str__(self):
50-
return (
51-
f"{super().__str__()}, document request: {self.document_request}"
59+
return f"{super().__str__()}, document handle: {self.document_handle}"
60+
61+
@property
62+
def document_request(self):
63+
"""Deprecated, use document_handle instead."""
64+
import warnings
65+
66+
warnings.warn(
67+
"document_request is deprecated", DeprecationWarning, stacklevel=2
5268
)
69+
return self.document_handle
5370

5471

5572
class GlossaryNotFoundException(DeepLException):

tests/test_translate_document.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Copyright 2022 DeepL SE (https://www.deepl.com)
22
# Use of this source code is governed by an MIT
33
# license that can be found in the LICENSE file.
4+
import re
45

56
from .conftest import example_text, needs_mock_server, needs_real_server
67
import deepl
@@ -112,16 +113,21 @@ def test_document_failure(
112113
):
113114
server.set_doc_failure(1)
114115

115-
# Ensure that the document ID and key are printed if an error occurs during
116-
# document translation
117116
with pytest.raises(
118117
deepl.exceptions.DocumentTranslationException,
119-
match="ID: [0-9A-F]{32}, key: [0-9A-F]{64}",
120-
):
118+
) as exc_info:
121119
translator.translate_document_from_filepath(
122120
example_document_path, output_document_path, target_lang="DE"
123121
)
124122

123+
# Ensure that document translation error contains document handle
124+
exception = exc_info.value
125+
assert exception.document_handle is not None
126+
match = re.compile("ID: [0-9A-F]{32}, key: [0-9A-F]{64}")
127+
assert match.search(str(exception)) is not None
128+
# document_request is a deprecated alias for document_handle
129+
assert exception.document_request is not None
130+
125131

126132
def test_invalid_document(translator, tmpdir):
127133
tmpdir = pathlib.Path(tmpdir)

0 commit comments

Comments
 (0)