Do not panic if when updatePlaylist is called with a non-existent ID.

Fix #2876
This commit is contained in:
Deluan 2024-05-11 15:37:39 -04:00
parent 2fdc1677f7
commit ed83c22632
3 changed files with 9 additions and 5 deletions

View File

@ -231,13 +231,17 @@ func (s *playlists) Update(ctx context.Context, playlistID string,
var pls *model.Playlist
var err error
repo := tx.Playlist(ctx)
tracks := repo.Tracks(playlistID, true)
if tracks == nil {
return fmt.Errorf("%w: playlist '%s'", model.ErrNotFound, playlistID)
}
if needsTrackRefresh {
pls, err = repo.GetWithTracks(playlistID, true)
pls.RemoveTracks(idxToRemove)
pls.AddTracks(idsToAdd)
} else {
if len(idsToAdd) > 0 {
_, err = repo.Tracks(playlistID, true).Add(idsToAdd)
_, err = tracks.Add(idsToAdd)
if err != nil {
return err
}
@ -264,7 +268,7 @@ func (s *playlists) Update(ctx context.Context, playlistID string,
}
// Special case: The playlist is now empty
if len(idxToRemove) > 0 && len(pls.Tracks) == 0 {
if err = repo.Tracks(playlistID, true).DeleteAll(); err != nil {
if err = tracks.DeleteAll(); err != nil {
return err
}
}

View File

@ -40,7 +40,7 @@ func (r *playlistRepository) Tracks(playlistId string, refreshSmartPlaylist bool
pls, err := r.Get(playlistId)
if err != nil {
log.Error(r.ctx, "Error getting playlist's tracks - THIS SHOULD NOT HAPPEN!", "playlistId", playlistId, err)
log.Warn(r.ctx, "Error getting playlist's tracks", "playlistId", playlistId, err)
return nil
}
if refreshSmartPlaylist {

View File

@ -43,7 +43,7 @@ func (api *Router) getPlaylist(ctx context.Context, id string) (*responses.Subso
pls, err := api.ds.Playlist(ctx).GetWithTracks(id, true)
if errors.Is(err, model.ErrNotFound) {
log.Error(ctx, err.Error(), "id", id)
return nil, newError(responses.ErrorDataNotFound, "Directory not found")
return nil, newError(responses.ErrorDataNotFound, "playlist not found")
}
if err != nil {
log.Error(ctx, err)
@ -150,7 +150,7 @@ func (api *Router) UpdatePlaylist(r *http.Request) (*responses.Subsonic, error)
return nil, newError(responses.ErrorAuthorizationFail)
}
if err != nil {
log.Error(r, err)
log.Error(r, "Error updating playlist", "id", playlistId, err)
return nil, err
}
return newResponse(), nil