fix: address code review findings
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"naviwatcher/internal/database"
|
||||
)
|
||||
@@ -36,7 +37,11 @@ func SyncAlbums(ctx context.Context, client *NavidromeClient, db *database.DB) e
|
||||
|
||||
albums, err := client.GetArtistAlbums(artist.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("sync albums: get albums for artist %s: %w", artist.ID, err)
|
||||
// A transient failure for one artist must not abort the whole pull
|
||||
// and take down the daemon; log it and continue with the remaining
|
||||
// artists, matching the resilience of SyncAll/ScanAll.
|
||||
log.Printf("sync albums: skip artist %s: %v", artist.ID, err)
|
||||
continue
|
||||
}
|
||||
|
||||
// Delete existing albums for this artist and insert fresh set within
|
||||
|
||||
@@ -360,18 +360,30 @@ func TestSyncAlbums_APIErrorMidSync(t *testing.T) {
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
// A transient failure for one artist must not abort the whole pull: the
|
||||
// failing artist is skipped (and logged) while the others succeed.
|
||||
err = SyncAlbums(ctx, nc, db)
|
||||
if err == nil {
|
||||
t.Fatal("SyncAlbums() expected error for API failure mid-sync, got nil")
|
||||
if err != nil {
|
||||
t.Fatalf("SyncAlbums() expected no fatal error on per-artist API failure, got %v", err)
|
||||
}
|
||||
|
||||
// The first artist's albums should have been stored before the error.
|
||||
// The first artist's albums should have been stored despite the second one
|
||||
// failing.
|
||||
albums1, err := database.GetLocalAlbumsByArtist(db, "1")
|
||||
if err != nil {
|
||||
t.Fatalf("GetLocalAlbumsByArtist(1) error: %v", err)
|
||||
}
|
||||
if len(albums1) != 1 {
|
||||
t.Errorf("expected 1 album for artist 1 (synced before error), got %d", len(albums1))
|
||||
t.Errorf("expected 1 album for artist 1 (synced before skip), got %d", len(albums1))
|
||||
}
|
||||
|
||||
// The failing artist should have no local albums stored.
|
||||
albums2, err := database.GetLocalAlbumsByArtist(db, "2")
|
||||
if err != nil {
|
||||
t.Fatalf("GetLocalAlbumsByArtist(2) error: %v", err)
|
||||
}
|
||||
if len(albums2) != 0 {
|
||||
t.Errorf("expected 0 albums for artist 2 (skipped on error), got %d", len(albums2))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user