feat: graceful degradation when FoJin API is unavailable (P0)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
xianren
2026-04-05 08:04:24 +08:00
parent da2a7f2dc8
commit 5688f40c1a
3 changed files with 37 additions and 5 deletions
+20 -4
View File
@@ -7,12 +7,18 @@ Two modes:
"""
import json
import logging
import os
from typing import Optional
import requests
class FojinUnavailableError(Exception):
"""Raised when FoJin API is unreachable. Callers should handle gracefully."""
pass
class FojinBridge:
"""Bridge to FoJin Buddhist text platform."""
@@ -108,11 +114,21 @@ class FojinBridge:
# ── Helpers ──────────────────────────────────────────────
def _get(self, path: str, params: Optional[dict] = None) -> dict:
"""Make GET request to FoJin API."""
"""Make GET request to FoJin API.
Raises:
FojinUnavailableError: When FoJin is unreachable (connection/timeout)
requests.HTTPError: On 4xx/5xx responses
"""
url = f"{self.base_url}{path}"
resp = self.session.get(url, params=params, timeout=30)
resp.raise_for_status()
return resp.json()
try:
resp = self.session.get(url, params=params, timeout=30)
resp.raise_for_status()
return resp.json()
except requests.ConnectionError as e:
raise FojinUnavailableError(f"FoJin API unreachable: {e}") from e
except requests.Timeout as e:
raise FojinUnavailableError(f"FoJin API timeout: {e}") from e
def test_connection(self) -> bool:
"""Test if FoJin API is reachable."""