Skip to content

Commit 07862bb

Browse files
authored
Localize runtime error fallback notifications (#173)
1 parent 0cbff0b commit 07862bb

2 files changed

Lines changed: 42 additions & 4 deletions

File tree

main.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,18 @@ def _runtime_error_notification_message(exc: Exception, *, route_label: str) ->
334334
error_text = f"{type(exc).__name__}: {exc}"
335335
if len(error_text) > 1200:
336336
error_text = error_text[:1197] + "..."
337+
if str(NOTIFY_LANG or "").strip().lower().startswith("zh"):
338+
return "\n".join(
339+
(
340+
"LongBridge 策略运行失败",
341+
f"服务: {os.getenv('K_SERVICE') or SECRET_NAME or 'longbridge-platform'}",
342+
f"版本: {os.getenv('K_REVISION') or '<unknown>'}",
343+
f"路由: {route_label}",
344+
f"策略: {STRATEGY_PROFILE}",
345+
f"账户范围: {ACCOUNT_REGION}",
346+
f"错误: {error_text}",
347+
)
348+
)
337349
return "\n".join(
338350
(
339351
"LongBridge strategy run failed",

tests/test_request_handling.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919

2020
@contextmanager
21-
def install_stub_modules():
21+
def install_stub_modules(*, notify_lang="en"):
2222
flask_module = types.ModuleType("flask")
2323

2424
class Flask:
@@ -67,7 +67,7 @@ def run(self, *args, **kwargs):
6767
market_timezone="Asia/Hong_Kong",
6868
symbol_suffix=".HK",
6969
trading_currency="HKD",
70-
notify_lang="en",
70+
notify_lang=notify_lang,
7171
tg_token=None,
7272
tg_chat_id="shared-chat-id",
7373
dry_run_only=False,
@@ -186,8 +186,8 @@ def run(self, *args, **kwargs):
186186
sys.modules[name] = previous
187187

188188

189-
def load_module():
190-
with install_stub_modules():
189+
def load_module(*, notify_lang="en"):
190+
with install_stub_modules(notify_lang=notify_lang):
191191
with patch.dict(
192192
os.environ,
193193
{
@@ -277,6 +277,32 @@ def fake_post(_url, *, json, timeout):
277277
self.assertIn("LongBridge strategy run failed", observed["payloads"][0][0]["text"])
278278
self.assertIn("RuntimeError: boom", observed["payloads"][0][0]["text"])
279279

280+
def test_handle_trigger_runtime_error_fallback_uses_chinese_copy(self):
281+
module = load_module(notify_lang="zh")
282+
observed = {"payloads": []}
283+
284+
class FakeResponse:
285+
status_code = 200
286+
287+
def fake_post(_url, *, json, timeout):
288+
observed["payloads"].append((json, timeout))
289+
return FakeResponse()
290+
291+
module.TG_TOKEN = "token-1"
292+
module.TG_CHAT_ID = "chat-1"
293+
module.requests.post = fake_post
294+
module.run_strategy = lambda: (_ for _ in ()).throw(RuntimeError("boom"))
295+
296+
with module.app.test_request_context("/", method="POST"):
297+
body, status = module.handle_trigger()
298+
299+
self.assertEqual(status, 500)
300+
self.assertEqual(body, "Error")
301+
text = observed["payloads"][0][0]["text"]
302+
self.assertIn("LongBridge 策略运行失败", text)
303+
self.assertIn("服务:", text)
304+
self.assertIn("错误: RuntimeError: boom", text)
305+
280306
def test_handle_trigger_allows_get(self):
281307
module = load_module()
282308
observed = {"called": False}

0 commit comments

Comments
 (0)