diff --git a/.golangci.yml b/.golangci.yml index 9ef792d3c..70b900fe5 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -14,6 +14,7 @@ linters: - depguard - dogsled - durationcheck + - errchkjson - errorlint - forbidigo - gocritic @@ -124,6 +125,8 @@ linters: errcheck: exclude-functions: - (*github.com/zeebo/xxh3.Hasher).Write + errchkjson: + report-no-exported: true gocritic: disable-all: true enabled-checks: diff --git a/adapters/listenbrainz/client.go b/adapters/listenbrainz/client.go index 36f700bd4..25252634c 100644 --- a/adapters/listenbrainz/client.go +++ b/adapters/listenbrainz/client.go @@ -162,7 +162,10 @@ func (c *client) path(endpoint string) (string, error) { } func (c *client) makeAuthenticatedRequest(ctx context.Context, method string, endpoint string, r *listenBrainzRequest) (*listenBrainzResponse, error) { - b, _ := json.Marshal(r.Body) + b, err := json.Marshal(r.Body) + if err != nil { + return nil, fmt.Errorf("encoding request body: %w", err) + } uri, err := c.path(endpoint) if err != nil { return nil, err diff --git a/cmd/missing.go b/cmd/missing.go index ce95e39f0..e1fecd9cd 100644 --- a/cmd/missing.go +++ b/cmd/missing.go @@ -94,7 +94,10 @@ func writeMissingList(w io.Writer, format string, mfs model.MediaFileCursor) err if err != nil { return err } - j, _ := json.Marshal(displayMissingFile{ID: mf.ID, LibraryID: mf.LibraryID, Title: mf.Title, Album: mf.Album, Artist: mf.Artist, Path: mf.Path}) + j, err := json.Marshal(displayMissingFile{ID: mf.ID, LibraryID: mf.LibraryID, Title: mf.Title, Album: mf.Album, Artist: mf.Artist, Path: mf.Path}) + if err != nil { + return err + } _, _ = fmt.Fprintf(bw, "%s%s", sep, j) sep = "," } diff --git a/cmd/pls.go b/cmd/pls.go index bf16ea420..6f31d1ca6 100644 --- a/cmd/pls.go +++ b/cmd/pls.go @@ -247,7 +247,10 @@ func runList(ctx context.Context) { display[idx].Public = playlist.Public } - j, _ := json.Marshal(display) + j, err := json.Marshal(display) + if err != nil { + log.Fatal(ctx, "Failed to encode playlists", err) + } fmt.Printf("%s\n", j) } } diff --git a/cmd/plugin.go b/cmd/plugin.go index 523792aa2..d25803bf2 100644 --- a/cmd/plugin.go +++ b/cmd/plugin.go @@ -509,8 +509,8 @@ func usersToJSON(value string) (string, error) { names = append(names, u) } } - b, _ := json.Marshal(names) - return string(b), nil + b, err := json.Marshal(names) + return string(b), err } // librariesToJSON accepts either a JSON array (starts with '[') or a @@ -535,8 +535,8 @@ func librariesToJSON(value string) (string, error) { ids = append(ids, id) } } - b, _ := json.Marshal(ids) - return string(b), nil + b, err := json.Marshal(ids) + return string(b), err } var pluginRescanCmd = &cobra.Command{ diff --git a/cmd/user.go b/cmd/user.go index eb64e69fe..247b59951 100644 --- a/cmd/user.go +++ b/cmd/user.go @@ -469,7 +469,10 @@ func runUserList(ctx context.Context) { users[idx].Libraries = paths } - j, _ := json.Marshal(users) + j, err := json.Marshal(users) + if err != nil { + log.Fatal(ctx, "Failed to encode users", err) + } fmt.Printf("%s\n", j) } } diff --git a/core/artwork/trace.go b/core/artwork/trace.go index bca2f2c7d..68c0bd57a 100644 --- a/core/artwork/trace.go +++ b/core/artwork/trace.go @@ -7,6 +7,7 @@ import ( "slices" "sync" + "github.com/navidrome/navidrome/log" "github.com/navidrome/navidrome/utils/str" ) @@ -94,7 +95,10 @@ func encodeSteps(steps []TraceStep, sourcePath string) string { } out = append(out, storedStep{C: s.Candidate, O: s.Outcome, D: str.TruncateRunes(d, maxTraceDetail, "...")}) } - b, _ := json.Marshal(out) // []storedStep is all strings, so this cannot fail + b, err := json.Marshal(out) + if err != nil { + log.Error("Artwork: Could not encode resolution trace", err) + } return string(b) } diff --git a/core/storage/storagetest/fake_storage.go b/core/storage/storagetest/fake_storage.go index ea95d6326..fab69da92 100644 --- a/core/storage/storagetest/fake_storage.go +++ b/core/storage/storagetest/fake_storage.go @@ -134,7 +134,8 @@ func (ffs *FakeFS) UpdateTags(filePath string, newTags map[string]any, when ...t var tags map[string]any gomega.Expect(json.Unmarshal(f.Data, &tags)).To(gomega.Succeed()) maps.Copy(tags, newTags) - data, _ := json.Marshal(tags) + data, err := json.Marshal(tags) + gomega.Expect(err).ToNot(gomega.HaveOccurred()) f.Data = data ffs.Touch(filePath, when...) } @@ -208,7 +209,8 @@ func File(tags ...map[string]any) *fstest.MapFile { if _, ok := ts[fakeFileInfoMode]; !ok { ts[fakeFileInfoMode] = fs.ModePerm } - data, _ := json.Marshal(ts) + data, err := json.Marshal(ts) + gomega.Expect(err).ToNot(gomega.HaveOccurred()) if _, ok := ts[fakeFileInfoSize]; !ok { ts[fakeFileInfoSize] = int64(len(data)) } diff --git a/core/stream/decider_test.go b/core/stream/decider_test.go index e5de9154d..979a29825 100644 --- a/core/stream/decider_test.go +++ b/core/stream/decider_test.go @@ -26,7 +26,8 @@ func withProbe(mf *model.MediaFile) *model.MediaFile { BitDepth: gg.V(mf.BitDepth), Channels: mf.Channels, } - data, _ := json.Marshal(probe) + data, err := json.Marshal(probe) + Expect(err).ToNot(HaveOccurred()) mf.ProbeData = string(data) return mf } diff --git a/persistence/album_repository.go b/persistence/album_repository.go index 5c96a845f..e0bc6ae08 100644 --- a/persistence/album_repository.go +++ b/persistence/album_repository.go @@ -79,8 +79,13 @@ func (a *dbAlbum) PostMapArgs(args map[string]any) error { args["search_participants"] = strings.Join(participantNames, " ") args["search_normalized"] = str.NormalizeForFTS(a.Name, a.AlbumArtist) - args["tags"] = marshalTags(a.Album.Tags) - args["participants"] = marshalParticipants(a.Album.Participants) + var err error + if args["tags"], err = marshalTags(a.Album.Tags); err != nil { + return err + } + if args["participants"], err = marshalParticipants(a.Album.Participants); err != nil { + return err + } folderIDs, err := json.Marshal(a.Album.FolderIDs) if err != nil { diff --git a/persistence/artist_repository.go b/persistence/artist_repository.go index 7a7ee3a19..6fb6fa583 100644 --- a/persistence/artist_repository.go +++ b/persistence/artist_repository.go @@ -102,7 +102,10 @@ func (a *dbArtist) PostMapArgs(m map[string]any) error { for _, s := range a.Artist.SimilarArtists { sa = append(sa, dbSimilarArtist{ID: s.ID, Name: s.Name}) } - similarArtists, _ := json.Marshal(sa) + similarArtists, err := json.Marshal(sa) + if err != nil { + return fmt.Errorf("marshalling similar artists: %w", err) + } m["similar_artists"] = string(similarArtists) // When adding a derived column here, also add it to the scanner's artist Put column list // in phase_1_folders.go, or rescans will never update it (how search_normalized went stale). diff --git a/persistence/criteria_sql_benchmark_test.go b/persistence/criteria_sql_benchmark_test.go index 499e09f10..829bf8245 100644 --- a/persistence/criteria_sql_benchmark_test.go +++ b/persistence/criteria_sql_benchmark_test.go @@ -250,7 +250,10 @@ func setupBenchData(b *testing.B, ctx context.Context, conn *dbx.DB, user model. primaryArtistName := fmt.Sprintf("Artist %04d", primaryArtistIdx) participants := map[string][]map[string]string{"artist": artistEntries} - participantsJSON, _ := json.Marshal(participants) + participantsJSON, err := json.Marshal(participants) + if err != nil { + b.Fatal(err) + } if _, err := mfStmt.Exec( trackID, diff --git a/persistence/mediafile_repository.go b/persistence/mediafile_repository.go index 324c88758..3dda5a132 100644 --- a/persistence/mediafile_repository.go +++ b/persistence/mediafile_repository.go @@ -74,8 +74,13 @@ func (m *dbMediaFile) PostMapArgs(args map[string]any) error { args["full_text"] = formatFullText(fullText...) args["search_participants"] = strings.Join(participantNames, " ") args["search_normalized"] = str.NormalizeForFTS(m.FullTitle(), m.Album, m.Artist, m.AlbumArtist) - args["tags"] = marshalTags(m.MediaFile.Tags) - args["participants"] = marshalParticipants(m.MediaFile.Participants) + var err error + if args["tags"], err = marshalTags(m.MediaFile.Tags); err != nil { + return err + } + if args["participants"], err = marshalParticipants(m.MediaFile.Participants); err != nil { + return err + } return nil } diff --git a/persistence/sql_participations.go b/persistence/sql_participations.go index 7d2280042..674006965 100644 --- a/persistence/sql_participations.go +++ b/persistence/sql_participations.go @@ -47,15 +47,18 @@ func participantIDFilter(table string, artistID any, negate bool, roles []model. return Expr(table+".id"+op+sql+")", args...) } -func marshalParticipants(participants model.Participants) string { +func marshalParticipants(participants model.Participants) (string, error) { dbParticipants := make(map[model.Role][]participant) for role, artists := range participants { for _, artist := range artists { dbParticipants[role] = append(dbParticipants[role], participant{ID: artist.ID, SubRole: artist.SubRole, Name: artist.Name}) } } - res, _ := json.Marshal(dbParticipants) - return string(res) + res, err := json.Marshal(dbParticipants) + if err != nil { + return "", fmt.Errorf("marshalling participants: %w", err) + } + return string(res), nil } func unmarshalParticipants(data string) (model.Participants, error) { diff --git a/persistence/sql_tags.go b/persistence/sql_tags.go index 7e1978453..45c4d44af 100644 --- a/persistence/sql_tags.go +++ b/persistence/sql_tags.go @@ -36,7 +36,7 @@ func unmarshalTags(data string) (model.Tags, error) { return res, nil } -func marshalTags(tags model.Tags) string { +func marshalTags(tags model.Tags) (string, error) { dbTags := dbTags{} for name, values := range tags { for _, value := range values { @@ -44,8 +44,11 @@ func marshalTags(tags model.Tags) string { dbTags[name] = append(dbTags[name], dbTag{ID: t.ID, Value: value}) } } - res, _ := json.Marshal(dbTags) - return string(res) + res, err := json.Marshal(dbTags) + if err != nil { + return "", fmt.Errorf("marshalling tags: %w", err) + } + return string(res), nil } // indexedTagNames are the tag types materialized into the