Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ def _runtime_error_notification_message(exc: Exception) -> str:
error_text = f"{type(exc).__name__}: {exc}"
if len(error_text) > 1200:
error_text = error_text[:1197] + "..."
if str(os.getenv("NOTIFY_LANG") or "").strip().lower().startswith("zh"):
return "\n".join(
(
"Firstrade 策略运行失败",
f"服务: {os.getenv('K_SERVICE') or 'firstrade-quant-service'}",
f"版本: {os.getenv('K_REVISION') or '<unknown>'}",
f"路由: {request.method} {request.path}",
f"策略: {os.getenv('STRATEGY_PROFILE') or '<unset>'}",
f"账户范围: {os.getenv('ACCOUNT_REGION') or '<unset>'}",
f"错误: {error_text}",
)
)
return "\n".join(
(
"Firstrade strategy run failed",
Expand Down
31 changes: 31 additions & 0 deletions tests/test_request_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,37 @@ def send(message):
assert "strategy: mega_cap_leader_rotation_top50_balanced" in sent_messages[0][2]


def test_run_endpoint_error_notification_uses_chinese_copy(monkeypatch):
monkeypatch.setenv("FIRSTRADE_RUN_STRATEGY_ON_HTTP", "true")
monkeypatch.setenv("TELEGRAM_TOKEN", "token-1")
monkeypatch.setenv("GLOBAL_TELEGRAM_CHAT_ID", "chat-1")
monkeypatch.setenv("NOTIFY_LANG", "zh")
monkeypatch.setenv("STRATEGY_PROFILE", "mega_cap_leader_rotation_top50_balanced")
sent_messages = []

def fake_build_sender(token, chat_id):
def send(message):
sent_messages.append((token, chat_id, message))

return send

monkeypatch.setattr(main, "build_sender", fake_build_sender)
monkeypatch.setattr(
main,
"_run_strategy_cycle_with_report",
lambda: (_ for _ in ()).throw(ValueError("snapshot denied")),
)
client = main.app.test_client()

response = client.post("/run")

assert response.status_code == 500
text = sent_messages[0][2]
assert "Firstrade 策略运行失败" in text
assert "策略: mega_cap_leader_rotation_top50_balanced" in text
assert "错误: ValueError: snapshot denied" in text


def test_run_endpoint_error_does_not_require_telegram_config(monkeypatch):
monkeypatch.setenv("FIRSTRADE_RUN_STRATEGY_ON_HTTP", "true")
monkeypatch.delenv("TELEGRAM_TOKEN", raising=False)
Expand Down