Don't panic on PostScan errors. Fix #3118

This commit is contained in:
Deluan 2024-06-25 17:14:17 -04:00
parent 3bc9e75b28
commit 7111535963
3 changed files with 30 additions and 8 deletions

View file

@ -5,6 +5,7 @@ import (
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"time" "time"
. "github.com/Masterminds/squirrel" . "github.com/Masterminds/squirrel"
@ -37,7 +38,10 @@ func (p dbPlaylist) PostMapArgs(args map[string]any) error {
var err error var err error
if p.Playlist.IsSmartPlaylist() { if p.Playlist.IsSmartPlaylist() {
args["rules"], err = json.Marshal(p.Playlist.Rules) args["rules"], err = json.Marshal(p.Playlist.Rules)
return err if err != nil {
return fmt.Errorf("invalid criteria expression: %w", err)
}
return nil
} }
delete(args, "rules") delete(args, "rules")
return nil return nil

View file

@ -120,7 +120,8 @@ var _ = Describe("PlaylistRepository", func() {
}, },
} }
}) })
It("Put/Get", func() { Context("valid rules", func() {
Specify("Put/Get", func() {
newPls := model.Playlist{Name: "Great!", OwnerID: "userid", Rules: rules} newPls := model.Playlist{Name: "Great!", OwnerID: "userid", Rules: rules}
Expect(repo.Put(&newPls)).To(Succeed()) Expect(repo.Put(&newPls)).To(Succeed())
@ -129,4 +130,18 @@ var _ = Describe("PlaylistRepository", func() {
Expect(savedPls.Rules).To(Equal(rules)) Expect(savedPls.Rules).To(Equal(rules))
}) })
}) })
Context("invalid rules", func() {
It("fails to Put it in the DB", func() {
rules = &criteria.Criteria{
// This is invalid because "contains" cannot have multiple fields
Expression: criteria.All{
criteria.Contains{"genre": "Hardcore", "filetype": "mp3"},
},
}
newPls := model.Playlist{Name: "Great!", OwnerID: "userid", Rules: rules}
Expect(repo.Put(&newPls)).To(MatchError(ContainSubstring("invalid criteria expression")))
})
})
})
}) })

View file

@ -275,7 +275,10 @@ func (r sqlRepository) count(countQuery SelectBuilder, options ...model.QueryOpt
} }
func (r sqlRepository) put(id string, m interface{}, colsToUpdate ...string) (newId string, err error) { func (r sqlRepository) put(id string, m interface{}, colsToUpdate ...string) (newId string, err error) {
values, _ := toSQLArgs(m) values, err := toSQLArgs(m)
if err != nil {
return "", fmt.Errorf("error preparing values to write to DB: %w", err)
}
// If there's an ID, try to update first // If there's an ID, try to update first
if id != "" { if id != "" {
updateValues := map[string]interface{}{} updateValues := map[string]interface{}{}