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:
@@ -1418,18 +1418,19 @@ func (mh *MediaHandler) DeleteMediaHighlight(c echo.Context) error {
|
|||||||
// SearchMediaItems handles GET /api/media-items/search
|
// SearchMediaItems handles GET /api/media-items/search
|
||||||
func (mh *MediaHandler) SearchMediaItems(c echo.Context) error {
|
func (mh *MediaHandler) SearchMediaItems(c echo.Context) error {
|
||||||
query := c.QueryParam("q")
|
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")
|
libraryID := c.QueryParam("library_id")
|
||||||
|
|
||||||
if query == "" {
|
if query == "" {
|
||||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": "query parameter 'q' is required"})
|
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
|
// Validate library_id if provided
|
||||||
var libUUID pgtype.UUID
|
var libUUID pgtype.UUID
|
||||||
if libraryID != "" {
|
if libraryID != "" {
|
||||||
@@ -1448,7 +1449,7 @@ func (mh *MediaHandler) SearchMediaItems(c echo.Context) error {
|
|||||||
// Build params - conditionally and library_id filter
|
// Build params - conditionally and library_id filter
|
||||||
partialParams := database.SearchMediaItemsParams{
|
partialParams := database.SearchMediaItemsParams{
|
||||||
SearchPattern: pgtype.Text{String: searchPattern, Valid: true},
|
SearchPattern: pgtype.Text{String: searchPattern, Valid: true},
|
||||||
UserID: pgtype.UUID{Bytes: userUUID, Valid: true},
|
UserID: userID.ID,
|
||||||
Limit: pgtype.Int4{Int32: limit, Valid: true},
|
Limit: pgtype.Int4{Int32: limit, Valid: true},
|
||||||
Offset: pgtype.Int4{Int32: offset, Valid: true},
|
Offset: pgtype.Int4{Int32: offset, Valid: true},
|
||||||
LibraryID: libUUID, // May be invalid (empty)
|
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)
|
partialResults, err := mh.db.SearchMediaItems(c.Request().Context(), partialParams)
|
||||||
|
|
||||||
if err != nil && err != pgx.ErrNoRows {
|
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()})
|
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{
|
fuzzyParams := database.SearchMediaItemsFuzzyParams{
|
||||||
SearchQuery: pgtype.Text{String: query, Valid: true},
|
SearchQuery: pgtype.Text{String: query, Valid: true},
|
||||||
UserID: pgtype.UUID{Bytes: userUUID, Valid: true},
|
UserID: userID.ID,
|
||||||
Limit: pgtype.Int4{Int32: limit, Valid: true},
|
Limit: pgtype.Int4{Int32: limit, Valid: true},
|
||||||
Offset: pgtype.Int4{Int32: offset, Valid: true},
|
Offset: pgtype.Int4{Int32: offset, Valid: true},
|
||||||
LibraryID: libUUID,
|
LibraryID: libUUID,
|
||||||
@@ -1475,6 +1477,7 @@ func (mh *MediaHandler) SearchMediaItems(c echo.Context) error {
|
|||||||
fuzzyResults, err := mh.db.SearchMediaItemsFuzzy(c.Request().Context(), fuzzyParams)
|
fuzzyResults, err := mh.db.SearchMediaItemsFuzzy(c.Request().Context(), fuzzyParams)
|
||||||
|
|
||||||
if err != nil && err != pgx.ErrNoRows {
|
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()})
|
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ func createJWTMiddleware(cfg *Config) echo.MiddlewareFunc {
|
|||||||
userIDStr, _ := claims["user_id"].(string)
|
userIDStr, _ := claims["user_id"].(string)
|
||||||
userUUID, err := uuid.Parse(userIDStr)
|
userUUID, err := uuid.Parse(userIDStr)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user