Skip to content

Commit

Permalink
refactor: Display the response content after a request has failed.
Browse files Browse the repository at this point in the history
  • Loading branch information
bookfere committed Dec 18, 2023
1 parent 4ba5bb3 commit 49f4e8c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
16 changes: 10 additions & 6 deletions engines/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os.path
import traceback

from mechanize import Browser, Request
from mechanize import Browser, Request, HTTPError
from calibre import get_proxies
from calibre.utils.localization import lang_as_iso639_1

Expand Down Expand Up @@ -206,13 +206,17 @@ def get_result(self, url, data=None, headers={}, method='GET',
if not stream:
response = result = response.read().decode('utf-8').strip()
return response if callback is None else callback(response)
except Exception:
except Exception as e:
if silence:
return None
error = '\n%s' % traceback.format_exc(chain=False).strip()
data = '\n%s%s' % (result, error) if result else error
raise Exception(_('Can not parse returned response. Raw data: {}')
.format('\n%s' % data))
error = [traceback.format_exc(chain=False).strip()]
if isinstance(e, HTTPError):
error.append(e.read().decode('utf-8'))
elif result:
error.append(result)
raise Exception(
_('Can not parse returned response. Raw data: {}')
.format('\n\n' + '\n\n'.join(error)))

def get_usage(self):
return None
Expand Down
39 changes: 23 additions & 16 deletions tests/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,11 @@ def test_translate(self, mock_browser):
self.assertEqual('你好世界!', self.translator.translate('Hello World!'))

result.return_value = '<dummy info>'
with self.assertRaises(Exception) as e:
self.translator.translate('Hello World!')
self.assertIn(
error = re.compile(
_('Can not parse returned response. Raw data: {}')
.format('\n\n<dummy info>\n') + 'Traceback',
str(e.exception))
.format('\n\nTraceback.*\n\n<dummy info>'), re.S)
with self.assertRaisesRegex(Exception, error):
self.translator.translate('Hello World!')


class TestChatgptTranslate(unittest.TestCase):
Expand Down Expand Up @@ -326,23 +325,31 @@ def setUp(self):
engine_data = json.loads(engine_data)
CustomTranslate.set_engine_data(engine_data)

@patch('calibre_plugins.ebook_translator.engines.base.Request')
@patch('calibre_plugins.ebook_translator.engines.base.Browser')
def test_translate_json(self, mock_browser):
def test_translate(self, mock_browser, mock_request):
translator = CustomTranslate()
translator.set_source_lang('English')
translator.set_target_lang('Chinese')
request = mock_browser.return_value.response.return_value.read. \
return_value.decode
request.return_value = '{"text": "你好世界!"}'
self.assertEqual('你好世界!', translator.translate('Hello World!'))

# JSON response
request.return_value = '{"text": "你好世界"}'
self.assertEqual('你好世界', translator.translate('Hello "World"'))
mock_request.assert_called_with(
'https://example.api',
b'{"source": "en", "target": "zh", "text": "Hello \\"World\\""}',
headers={'Content-Type': 'application/json'},
timeout=10.0,
method='POST')
# XML response
translator.engine_data.update({'response': 'response.text'})
request.return_value = '<test>你好世界</test>'
self.assertEqual('你好世界', translator.translate('Hello World!'))

request.return_value = '<test>你好世界</test>'
self.assertEqual('你好世界', translator.translate('Hello World'))
# Plain response
translator.engine_data.update({'response': 'response'})
request.return_value = '你好世界'
self.assertEqual('你好世界', translator.translate('Hello World!'))
request.return_value = '你好世界'
self.assertEqual('你好世界', translator.translate('Hello World'))

@patch('calibre_plugins.ebook_translator.engines.base.Browser')
def test_translate_urlencoded(self, mock_browser):
Expand All @@ -352,6 +359,6 @@ def test_translate_urlencoded(self, mock_browser):
translator.set_source_lang('English')
translator.set_target_lang('Chinese')
mock_browser.return_value.response.return_value.read.return_value \
.decode.return_value = '{"text": "\\"你好\\"\\n世界"}'
.decode.return_value = '{"text": "\\"你好\\"\\n世界"}'
self.assertEqual(
'\"你好\"\n世界', translator.translate('\"Hello\"\nWorld!'))
'\"你好\"\n世界', translator.translate('\"Hello\"\nWorld'))

0 comments on commit 49f4e8c

Please sign in to comment.