From 9604d0ba468298fe5db0a8a097cd2aa91a4d32b9 Mon Sep 17 00:00:00 2001 From: Matt Van Horn Date: Fri, 23 Jan 2026 16:50:05 -0800 Subject: [PATCH] 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 --- scripts/last30days.py | 27 +++++++++++++++++++-------- scripts/lib/xai_x.py | 7 +++++++ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/scripts/last30days.py b/scripts/last30days.py index bc3ddc9..ceae026 100644 --- a/scripts/last30days.py +++ b/scripts/last30days.py @@ -133,14 +133,25 @@ def run_research( if mock: raw_xai = load_fixture("xai_sample.json") else: - raw_xai = xai_x.search_x( - config["XAI_API_KEY"], - selected_models["xai"], - topic, - from_date, - to_date, - depth=depth, - ) + try: + raw_xai = xai_x.search_x( + config["XAI_API_KEY"], + selected_models["xai"], + topic, + from_date, + to_date, + 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 x_items = xai_x.parse_x_response(raw_xai) diff --git a/scripts/lib/xai_x.py b/scripts/lib/xai_x.py index a0e9dd2..2a0fe25 100644 --- a/scripts/lib/xai_x.py +++ b/scripts/lib/xai_x.py @@ -125,6 +125,13 @@ def parse_x_response(response: Dict[str, Any]) -> List[Dict[str, Any]]: """ 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 output_text = "" if "output" in response: