fix(playqueue): refuse queue index updates when tracks fail to load

RetrieveWithMediaFiles now returns ErrIncompleteQueue when it reads the queue but a track query fails. Before, it returned the partially loaded list with no error, and a current-only PUT /api/queue could validate and map an index against that list and store the wrong track.

updateQueue already fails on any retrieval error, so it now returns 500 and writes nothing. The GET handlers (getPlayQueue, getPlayQueueByIndex and GET /api/queue) treat ErrIncompleteQueue like before and still return the tracks that loaded.
This commit is contained in:
Deluan committed 2026-11-14 19:46:18 -05:00
1 parent b5fe0b4221
commit d4eecb2785
8 files changed
+74 -6

No files matched your search

+1 -1
View File
@@ -86,7 +86,7 @@ func getQueue(ds model.DataStore) http.HandlerFunc {
user, _ := request.UserFrom(ctx)
repo := ds.PlayQueue()
pq, err := repo.RetrieveWithMediaFiles(ctx, user.ID)
if err != nil && !errors.Is(err, model.ErrNotFound) {
if err != nil && !errors.Is(err, model.ErrNotFound) && !errors.Is(err, model.ErrIncompleteQueue) {
log.Error(ctx, "Error retrieving queue", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
+22
View File
@@ -3,6 +3,7 @@ package nativeapi
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
@@ -151,6 +152,20 @@ var _ = Describe("Queue Endpoints", func() {
Expect(resp.Position).To(Equal(int64(0)))
})
It("returns the loaded tracks when some of them fail to load", func() {
repo.Queue = &model.PlayQueue{UserID: user.ID, Items: model.MediaFiles{{ID: "track1"}}}
repo.WithMediaFilesErr = fmt.Errorf("%w: chunk failed", model.ErrIncompleteQueue)
req := httptest.NewRequest("GET", "/queue", nil)
req = req.WithContext(request.WithUser(req.Context(), user))
w := httptest.NewRecorder()
getQueue(ds)(w, req)
Expect(w.Code).To(Equal(http.StatusOK))
var resp model.PlayQueue
Expect(json.Unmarshal(w.Body.Bytes(), &resp)).To(Succeed())
Expect(resp.Items).To(HaveLen(1))
})
It("returns internal server error when retrieve fails", func() {
repo.Err = true
req := httptest.NewRequest("GET", "/queue", nil)
@@ -223,6 +238,13 @@ var _ = Describe("Queue Endpoints", func() {
Expect(put(updateQueuePayload{Current: new(2)})).To(Equal(http.StatusBadRequest))
})
It("refuses to update current when the tracks fail to load", func() {
repo.WithMediaFilesErr = fmt.Errorf("%w: chunk failed", model.ErrIncompleteQueue)
Expect(put(updateQueuePayload{Current: new(0)})).To(Equal(http.StatusInternalServerError))
Expect(repo.LastCols).To(BeNil())
Expect(repo.Queue.Current).To(Equal(2))
})
It("keeps the current track and position the client saw when only ids change", func() {
Expect(put(updateQueuePayload{Ids: new([]string{"s1", "s2", "s3"})})).To(Equal(http.StatusNoContent))
Expect(repo.Queue.Current).To(Equal(1))