From 6b4e5f01984f9ec2aabe878d5b3cc96cc913c4f9 Mon Sep 17 00:00:00 2001 From: John O'Keefe Date: Mon, 26 Jan 2026 11:31:22 -0500 Subject: [PATCH] Improve error handling in auth handlers - Add specific pgx.ErrNoRows checks in GetProfile - Better error handling in UpdatePassword and GetScanSettings - Return appropriate HTTP status codes for different error types - Improve error message consistency --- internal/handlers/auth.go | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/internal/handlers/auth.go b/internal/handlers/auth.go index f248639..8d6ca24 100644 --- a/internal/handlers/auth.go +++ b/internal/handlers/auth.go @@ -281,7 +281,10 @@ func (h *AuthHandler) GetProfile(c echo.Context) error { user, err := h.db.GetUser(c.Request().Context(), pgtype.UUID{Bytes: userUUID, Valid: true}) if err != nil { - return c.JSON(http.StatusNotFound, map[string]string{"error": "user not found"}) + if err == pgx.ErrNoRows { + return c.JSON(http.StatusNotFound, map[string]string{"error": "user not found"}) + } + return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) } firstName := "" @@ -652,6 +655,9 @@ func (h *AuthHandler) UpdatePassword(c echo.Context) error { // Get current user's password hash passwordHash, err := h.db.GetUserPasswordHash(c.Request().Context(), pgtype.UUID{Bytes: userUUID, Valid: true}) if err != nil { + if err == pgx.ErrNoRows { + return c.JSON(http.StatusNotFound, map[string]string{"error": "user not found"}) + } return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to get user"}) } @@ -738,11 +744,14 @@ func (h *AuthHandler) GetScanSettings(c echo.Context) error { settings, err := h.db.GetScanSettings(c.Request().Context(), pgtype.UUID{Bytes: userUUID, Valid: true}) if err != nil { - // If no settings found, return defaults - return c.JSON(http.StatusOK, map[string]interface{}{ - "scan_frequency_minutes": 60, - "auto_scan_enabled": true, - }) + if err == pgx.ErrNoRows { + // If no settings found, return defaults + return c.JSON(http.StatusOK, map[string]interface{}{ + "scan_frequency_minutes": 60, + "auto_scan_enabled": true, + }) + } + return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) } return c.JSON(http.StatusOK, map[string]interface{}{