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:
@@ -281,8 +281,11 @@ 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 {
|
||||
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 := ""
|
||||
if user.FirstName.Valid {
|
||||
@@ -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,12 +744,15 @@ 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 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{}{
|
||||
"scan_frequency_minutes": settings.ScanFrequencyMinutes.Int32,
|
||||
|
||||
Reference in New Issue
Block a user