Fix error comparisons

This commit is contained in:
Deluan
2022-09-30 18:54:25 -04:00
parent 7b0a8f47de
commit db67c1277e
27 changed files with 93 additions and 73 deletions
+4 -3
View File
@@ -2,6 +2,7 @@ package persistence
import (
"context"
"errors"
. "github.com/Masterminds/squirrel"
"github.com/beego/beego/v2/client/orm"
@@ -102,7 +103,7 @@ func (r *playerRepository) Save(entity interface{}) (string, error) {
return "", rest.ErrPermissionDenied
}
id, err := r.put(t.ID, t)
if err == model.ErrNotFound {
if errors.Is(err, model.ErrNotFound) {
return "", rest.ErrNotFound
}
return id, err
@@ -115,7 +116,7 @@ func (r *playerRepository) Update(id string, entity interface{}, cols ...string)
return rest.ErrPermissionDenied
}
_, err := r.put(id, t, cols...)
if err == model.ErrNotFound {
if errors.Is(err, model.ErrNotFound) {
return rest.ErrNotFound
}
return err
@@ -124,7 +125,7 @@ func (r *playerRepository) Update(id string, entity interface{}, cols ...string)
func (r *playerRepository) Delete(id string) error {
filter := r.addRestriction(And{Eq{"id": id}})
err := r.delete(filter)
if err == model.ErrNotFound {
if errors.Is(err, model.ErrNotFound) {
return rest.ErrNotFound
}
return err
+2 -1
View File
@@ -3,6 +3,7 @@ package persistence
import (
"context"
"encoding/json"
"errors"
"strings"
"time"
@@ -395,7 +396,7 @@ func (r *playlistRepository) Update(id string, entity interface{}, cols ...strin
pls.ID = id
pls.UpdatedAt = time.Now()
_, err = r.put(id, pls, append(cols, "updatedAt")...)
if err == model.ErrNotFound {
if errors.Is(err, model.ErrNotFound) {
return rest.ErrNotFound
}
return err
+2 -1
View File
@@ -2,6 +2,7 @@ package persistence
import (
"context"
"errors"
. "github.com/Masterminds/squirrel"
"github.com/beego/beego/v2/client/orm"
@@ -48,7 +49,7 @@ func (r propertyRepository) Get(id string) (string, error) {
func (r propertyRepository) DefaultGet(id string, defaultValue string) (string, error) {
value, err := r.Get(id)
if err == model.ErrNotFound {
if errors.Is(err, model.ErrNotFound) {
return defaultValue, nil
}
if err != nil {
+2 -1
View File
@@ -2,6 +2,7 @@ package persistence
import (
"context"
"errors"
"time"
. "github.com/Masterminds/squirrel"
@@ -59,7 +60,7 @@ func (r *scrobbleBufferRepository) Next(service string, userId string) (*model.S
res := model.ScrobbleEntries{}
// TODO Rewrite queryOne to use QueryRows, to workaround the recursive embedded structs issue
err := r.queryAll(sql, &res)
if err == model.ErrNotFound || len(res) == 0 {
if errors.Is(err, model.ErrNotFound) || len(res) == 0 {
return nil, nil
}
if err != nil {
+4 -3
View File
@@ -2,6 +2,7 @@ package persistence
import (
"context"
"errors"
. "github.com/Masterminds/squirrel"
"github.com/beego/beego/v2/client/orm"
@@ -24,7 +25,7 @@ func NewShareRepository(ctx context.Context, o orm.QueryExecutor) model.ShareRep
func (r *shareRepository) Delete(id string) error {
err := r.delete(Eq{"id": id})
if err == model.ErrNotFound {
if errors.Is(err, model.ErrNotFound) {
return rest.ErrNotFound
}
return err
@@ -50,7 +51,7 @@ func (r *shareRepository) Update(id string, entity interface{}, cols ...string)
s := entity.(*model.Share)
s.ID = id
_, err := r.put(id, s, cols...)
if err == model.ErrNotFound {
if errors.Is(err, model.ErrNotFound) {
return rest.ErrNotFound
}
return err
@@ -59,7 +60,7 @@ func (r *shareRepository) Update(id string, entity interface{}, cols ...string)
func (r *shareRepository) Save(entity interface{}) (string, error) {
s := entity.(*model.Share)
id, err := r.put(s.ID, s)
if err == model.ErrNotFound {
if errors.Is(err, model.ErrNotFound) {
return "", rest.ErrNotFound
}
return id, err
+3 -2
View File
@@ -1,6 +1,7 @@
package persistence
import (
"errors"
"time"
. "github.com/Masterminds/squirrel"
@@ -35,7 +36,7 @@ func (r sqlRepository) annUpsert(values map[string]interface{}, itemIDs ...strin
upd = upd.Set(f, v)
}
c, err := r.executeSQL(upd)
if c == 0 || err == orm.ErrNoRows {
if c == 0 || errors.Is(err, orm.ErrNoRows) {
for _, itemID := range itemIDs {
values["ann_id"] = uuid.NewString()
values["user_id"] = userId(r.ctx)
@@ -66,7 +67,7 @@ func (r sqlRepository) IncPlayCount(itemID string, ts time.Time) error {
Set("play_date", ts)
c, err := r.executeSQL(upd)
if c == 0 || err == orm.ErrNoRows {
if c == 0 || errors.Is(err, orm.ErrNoRows) {
values := map[string]interface{}{}
values["ann_id"] = uuid.NewString()
values["user_id"] = userId(r.ctx)
+4 -3
View File
@@ -2,6 +2,7 @@ package persistence
import (
"context"
"errors"
"fmt"
"strings"
"time"
@@ -146,7 +147,7 @@ func (r sqlRepository) queryOne(sq Sqlizer, response interface{}) error {
}
start := time.Now()
err = r.ormer.Raw(query, args...).QueryRow(response)
if err == orm.ErrNoRows {
if errors.Is(err, orm.ErrNoRows) {
r.logSQL(query, args, nil, 0, start)
return model.ErrNotFound
}
@@ -161,7 +162,7 @@ func (r sqlRepository) queryAll(sq Sqlizer, response interface{}) error {
}
start := time.Now()
c, err := r.ormer.Raw(query, args...).QueryRows(response)
if err == orm.ErrNoRows {
if errors.Is(err, orm.ErrNoRows) {
r.logSQL(query, args, nil, c, start)
return model.ErrNotFound
}
@@ -224,7 +225,7 @@ func (r sqlRepository) put(id string, m interface{}, colsToUpdate ...string) (ne
func (r sqlRepository) delete(cond Sqlizer) error {
del := Delete(r.tableName).Where(cond)
_, err := r.executeSQL(del)
if err == orm.ErrNoRows {
if errors.Is(err, orm.ErrNoRows) {
return model.ErrNotFound
}
return err
+2 -1
View File
@@ -1,6 +1,7 @@
package persistence
import (
"errors"
"time"
. "github.com/Masterminds/squirrel"
@@ -44,7 +45,7 @@ func (r sqlRepository) bmkUpsert(itemID, comment string, position int64) error {
if err == nil {
log.Debug(r.ctx, "Updated bookmark", "id", itemID, "user", user.UserName, "position", position, "comment", comment)
}
if c == 0 || err == orm.ErrNoRows {
if c == 0 || errors.Is(err, orm.ErrNoRows) {
values["user_id"] = user.ID
values["item_type"] = r.tableName
values["item_id"] = itemID
+4 -3
View File
@@ -2,6 +2,7 @@ package persistence
import (
"context"
"errors"
. "github.com/Masterminds/squirrel"
"github.com/beego/beego/v2/client/orm"
@@ -71,7 +72,7 @@ func (r *transcodingRepository) NewInstance() interface{} {
func (r *transcodingRepository) Save(entity interface{}) (string, error) {
t := entity.(*model.Transcoding)
id, err := r.put(t.ID, t)
if err == model.ErrNotFound {
if errors.Is(err, model.ErrNotFound) {
return "", rest.ErrNotFound
}
return id, err
@@ -81,7 +82,7 @@ func (r *transcodingRepository) Update(id string, entity interface{}, cols ...st
t := entity.(*model.Transcoding)
t.ID = id
_, err := r.put(id, t)
if err == model.ErrNotFound {
if errors.Is(err, model.ErrNotFound) {
return rest.ErrNotFound
}
return err
@@ -89,7 +90,7 @@ func (r *transcodingRepository) Update(id string, entity interface{}, cols ...st
func (r *transcodingRepository) Delete(id string) error {
err := r.delete(Eq{"id": id})
if err == model.ErrNotFound {
if errors.Is(err, model.ErrNotFound) {
return rest.ErrNotFound
}
return err
+2 -1
View File
@@ -2,6 +2,7 @@ package persistence
import (
"context"
"errors"
. "github.com/Masterminds/squirrel"
"github.com/beego/beego/v2/client/orm"
@@ -48,7 +49,7 @@ func (r userPropsRepository) Get(userId, key string) (string, error) {
func (r userPropsRepository) DefaultGet(userId, key string, defaultValue string) (string, error) {
value, err := r.Get(userId, key)
if err == model.ErrNotFound {
if errors.Is(err, model.ErrNotFound) {
return defaultValue, nil
}
if err != nil {
+4 -4
View File
@@ -131,7 +131,7 @@ func (r *userRepository) Read(id string) (interface{}, error) {
return nil, rest.ErrPermissionDenied
}
usr, err := r.Get(id)
if err == model.ErrNotFound {
if errors.Is(err, model.ErrNotFound) {
return nil, rest.ErrNotFound
}
return usr, err
@@ -195,7 +195,7 @@ func (r *userRepository) Update(id string, entity interface{}, cols ...string) e
return err
}
err := r.Put(u)
if err == model.ErrNotFound {
if errors.Is(err, model.ErrNotFound) {
return rest.ErrNotFound
}
return err
@@ -225,7 +225,7 @@ func validatePasswordChange(newUser *model.User, logged *model.User) error {
func validateUsernameUnique(r model.UserRepository, u *model.User) error {
usr, err := r.FindByUsername(u.UserName)
if err == model.ErrNotFound {
if errors.Is(err, model.ErrNotFound) {
return nil
}
if err != nil {
@@ -243,7 +243,7 @@ func (r *userRepository) Delete(id string) error {
return rest.ErrPermissionDenied
}
err := r.delete(Eq{"id": id})
if err == model.ErrNotFound {
if errors.Is(err, model.ErrNotFound) {
return rest.ErrNotFound
}
return err