From 210671059a418de13cf9274707011c8af1c88337 Mon Sep 17 00:00:00 2001 From: Lars Date: Fri, 27 Mar 2026 07:58:56 +0100 Subject: [PATCH] debug: comprehensive error handling and logging for list_goals - try-catch around entire endpoint - try-catch for each goal progress update - Detailed error logging with traceback - Continue processing other goals if one fails - Clear error message to frontend This will show exact error location in logs. --- backend/routers/goals.py | 51 ++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/backend/routers/goals.py b/backend/routers/goals.py index e6a6391..8a0fdea 100644 --- a/backend/routers/goals.py +++ b/backend/routers/goals.py @@ -161,27 +161,42 @@ def list_goals(session: dict = Depends(require_auth)): """List all goals for current user""" pid = session['profile_id'] - with get_db() as conn: - cur = get_cursor(conn) - cur.execute(""" - SELECT id, goal_type, is_primary, status, - target_value, current_value, start_value, unit, - start_date, target_date, reached_date, - name, description, - progress_pct, projection_date, on_track, - created_at, updated_at - FROM goals - WHERE profile_id = %s - ORDER BY is_primary DESC, created_at DESC - """, (pid,)) + try: + with get_db() as conn: + cur = get_cursor(conn) + cur.execute(""" + SELECT id, goal_type, is_primary, status, + target_value, current_value, start_value, unit, + start_date, target_date, reached_date, + name, description, + progress_pct, projection_date, on_track, + created_at, updated_at + FROM goals + WHERE profile_id = %s + ORDER BY is_primary DESC, created_at DESC + """, (pid,)) - goals = [r2d(row) for row in cur.fetchall()] + goals = [r2d(row) for row in cur.fetchall()] + print(f"[DEBUG] Loaded {len(goals)} goals for profile {pid}") - # Update current values for each goal - for goal in goals: - _update_goal_progress(conn, pid, goal) + # Update current values for each goal + for goal in goals: + try: + _update_goal_progress(conn, pid, goal) + except Exception as e: + print(f"[ERROR] Failed to update progress for goal {goal.get('id')}: {e}") + # Continue with other goals even if one fails - return goals + return goals + + except Exception as e: + print(f"[ERROR] list_goals failed: {e}") + import traceback + traceback.print_exc() + raise HTTPException( + status_code=500, + detail=f"Fehler beim Laden der Ziele: {str(e)}" + ) @router.post("/create") def create_goal(data: GoalCreate, session: dict = Depends(require_auth)):