fix: correct user context handling and error responses

- Fix SearchMediaItems to retrieve user object from context instead of string
- Remove redundant UUID parsing, use user.ID directly
- Add error logging for search failures with query details
- Fix JWT middleware to use echo.NewHTTPError for consistent error format
- Improves debugging and error response consistency across API
This commit is contained in:
2026-03-06 01:52:36 -05:00
parent 79690751c8
commit 2ac42a8d91
2 changed files with 12 additions and 9 deletions
+11 -8
View File
@@ -1418,18 +1418,19 @@ func (mh *MediaHandler) DeleteMediaHighlight(c echo.Context) error {
// SearchMediaItems handles GET /api/media-items/search
func (mh *MediaHandler) SearchMediaItems(c echo.Context) error {
query := c.QueryParam("q")
userID := c.Get("user_id").(string)
// Safely get user from context
userID, ok := c.Get("user").(database.Users)
if !ok {
return c.JSON(http.StatusUnauthorized, map[string]string{"error": "user not authenticated"})
}
libraryID := c.QueryParam("library_id")
if query == "" {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "query parameter 'q' is required"})
}
userUUID, err := uuid.Parse(userID)
if err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid user id"})
}
// Validate library_id if provided
var libUUID pgtype.UUID
if libraryID != "" {
@@ -1448,7 +1449,7 @@ func (mh *MediaHandler) SearchMediaItems(c echo.Context) error {
// Build params - conditionally and library_id filter
partialParams := database.SearchMediaItemsParams{
SearchPattern: pgtype.Text{String: searchPattern, Valid: true},
UserID: pgtype.UUID{Bytes: userUUID, Valid: true},
UserID: userID.ID,
Limit: pgtype.Int4{Int32: limit, Valid: true},
Offset: pgtype.Int4{Int32: offset, Valid: true},
LibraryID: libUUID, // May be invalid (empty)
@@ -1457,6 +1458,7 @@ func (mh *MediaHandler) SearchMediaItems(c echo.Context) error {
partialResults, err := mh.db.SearchMediaItems(c.Request().Context(), partialParams)
if err != nil && err != pgx.ErrNoRows {
c.Logger().Error("search error", "error", err.Error(), "query", query, "library_id", libraryID)
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
}
@@ -1466,7 +1468,7 @@ func (mh *MediaHandler) SearchMediaItems(c echo.Context) error {
fuzzyParams := database.SearchMediaItemsFuzzyParams{
SearchQuery: pgtype.Text{String: query, Valid: true},
UserID: pgtype.UUID{Bytes: userUUID, Valid: true},
UserID: userID.ID,
Limit: pgtype.Int4{Int32: limit, Valid: true},
Offset: pgtype.Int4{Int32: offset, Valid: true},
LibraryID: libUUID,
@@ -1475,6 +1477,7 @@ func (mh *MediaHandler) SearchMediaItems(c echo.Context) error {
fuzzyResults, err := mh.db.SearchMediaItemsFuzzy(c.Request().Context(), fuzzyParams)
if err != nil && err != pgx.ErrNoRows {
c.Logger().Error("fuzzy search error", "error", err.Error(), "query", query)
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
}
+1 -1
View File
@@ -83,7 +83,7 @@ func createJWTMiddleware(cfg *Config) echo.MiddlewareFunc {
userIDStr, _ := claims["user_id"].(string)
userUUID, err := uuid.Parse(userIDStr)
if err != nil {
c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid user ID in token"})
c.Error(echo.NewHTTPError(http.StatusBadRequest, "invalid user ID in token"))
return
}