Add error handling for X/xAI API calls

- Wrap xAI search in try/except like Reddit
- Show error message but continue with Reddit results
- Parse function checks for API errors before processing

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Matt Van Horn
2026-01-23 16:50:05 -08:00
parent 546f93fe43
commit 9604d0ba46
2 changed files with 26 additions and 8 deletions
+11
View File
@@ -133,6 +133,7 @@ def run_research(
if mock: if mock:
raw_xai = load_fixture("xai_sample.json") raw_xai = load_fixture("xai_sample.json")
else: else:
try:
raw_xai = xai_x.search_x( raw_xai = xai_x.search_x(
config["XAI_API_KEY"], config["XAI_API_KEY"],
selected_models["xai"], selected_models["xai"],
@@ -141,6 +142,16 @@ def run_research(
to_date, to_date,
depth=depth, depth=depth,
) )
except http.HTTPError as e:
if progress:
progress.show_error(f"X API failed: {e}")
raw_xai = {"error": str(e)}
x_error = f"API error: {e}"
except Exception as e:
if progress:
progress.show_error(f"X error: {e}")
raw_xai = {"error": str(e)}
x_error = f"{type(e).__name__}: {e}"
# Parse response # Parse response
x_items = xai_x.parse_x_response(raw_xai) x_items = xai_x.parse_x_response(raw_xai)
+7
View File
@@ -125,6 +125,13 @@ def parse_x_response(response: Dict[str, Any]) -> List[Dict[str, Any]]:
""" """
items = [] items = []
# Check for API errors first
if "error" in response and response["error"]:
error = response["error"]
err_msg = error.get("message", str(error)) if isinstance(error, dict) else str(error)
print(f"[X ERROR] xAI API error: {err_msg}", flush=True)
return items
# Try to find the output text # Try to find the output text
output_text = "" output_text = ""
if "output" in response: if "output" in response: