From 49f4e8cac9f01422c68ba6ff9de267c7c7d09678 Mon Sep 17 00:00:00 2001 From: bookfere Date: Mon, 18 Dec 2023 13:53:37 +0800 Subject: [PATCH] refactor: Display the response content after a request has failed. --- engines/base.py | 16 ++++++++++------ tests/test_engine.py | 39 +++++++++++++++++++++++---------------- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/engines/base.py b/engines/base.py index a31e6cd..da1c35c 100644 --- a/engines/base.py +++ b/engines/base.py @@ -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 @@ -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 diff --git a/tests/test_engine.py b/tests/test_engine.py index d1e53f3..4387960 100644 --- a/tests/test_engine.py +++ b/tests/test_engine.py @@ -84,12 +84,11 @@ def test_translate(self, mock_browser): self.assertEqual('你好世界!', self.translator.translate('Hello World!')) result.return_value = '' - 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\n') + 'Traceback', - str(e.exception)) + .format('\n\nTraceback.*\n\n'), re.S) + with self.assertRaisesRegex(Exception, error): + self.translator.translate('Hello World!') class TestChatgptTranslate(unittest.TestCase): @@ -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 = '你好世界!' - self.assertEqual('你好世界!', translator.translate('Hello World!')) - + request.return_value = '你好世界' + 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): @@ -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'))