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
This commit is contained in:
2026-01-26 11:31:22 -05:00
parent 123ddab242
commit 6b4e5f0198
+15 -6
View File
@@ -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{}{