Fix error comparisons
This commit is contained in:
@@ -3,6 +3,7 @@ package subsonic
|
||||
import (
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"runtime"
|
||||
@@ -183,7 +184,7 @@ func h(r chi.Router, path string, f handler) {
|
||||
if err != nil {
|
||||
// If it is not a Subsonic error, convert it to an ErrorGeneric
|
||||
if _, ok := err.(subError); !ok {
|
||||
if err == model.ErrNotFound {
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
err = newError(responses.ErrorDataNotFound, "data not found")
|
||||
} else {
|
||||
err = newError(responses.ErrorGeneric, "Internal Error")
|
||||
|
||||
+13
-12
@@ -2,6 +2,7 @@ package subsonic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
@@ -104,11 +105,11 @@ func (c *BrowsingController) GetMusicDirectory(w http.ResponseWriter, r *http.Re
|
||||
ctx := r.Context()
|
||||
|
||||
entity, err := core.GetEntityByID(ctx, c.ds, id)
|
||||
switch {
|
||||
case err == model.ErrNotFound:
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
log.Error(r, "Requested ID not found ", "id", id)
|
||||
return nil, newError(responses.ErrorDataNotFound, "Directory not found")
|
||||
case err != nil:
|
||||
}
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
@@ -140,11 +141,11 @@ func (c *BrowsingController) GetArtist(w http.ResponseWriter, r *http.Request) (
|
||||
ctx := r.Context()
|
||||
|
||||
artist, err := c.ds.Artist(ctx).Get(id)
|
||||
switch {
|
||||
case err == model.ErrNotFound:
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
log.Error(ctx, "Requested ArtistID not found ", "id", id)
|
||||
return nil, newError(responses.ErrorDataNotFound, "Artist not found")
|
||||
case err != nil:
|
||||
}
|
||||
if err != nil {
|
||||
log.Error(ctx, "Error retrieving artist", "id", id, err)
|
||||
return nil, err
|
||||
}
|
||||
@@ -165,11 +166,11 @@ func (c *BrowsingController) GetAlbum(w http.ResponseWriter, r *http.Request) (*
|
||||
ctx := r.Context()
|
||||
|
||||
album, err := c.ds.Album(ctx).Get(id)
|
||||
switch {
|
||||
case err == model.ErrNotFound:
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
log.Error(ctx, "Requested AlbumID not found ", "id", id)
|
||||
return nil, newError(responses.ErrorDataNotFound, "Album not found")
|
||||
case err != nil:
|
||||
}
|
||||
if err != nil {
|
||||
log.Error(ctx, "Error retrieving album", "id", id, err)
|
||||
return nil, err
|
||||
}
|
||||
@@ -190,11 +191,11 @@ func (c *BrowsingController) GetSong(w http.ResponseWriter, r *http.Request) (*r
|
||||
ctx := r.Context()
|
||||
|
||||
mf, err := c.ds.MediaFile(ctx).Get(id)
|
||||
switch {
|
||||
case err == model.ErrNotFound:
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
log.Error(r, "Requested MediaFileID not found ", "id", id)
|
||||
return nil, newError(responses.ErrorDataNotFound, "Song not found")
|
||||
case err != nil:
|
||||
}
|
||||
if err != nil {
|
||||
log.Error(r, "Error retrieving MediaFile", "id", id, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package subsonic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
@@ -39,11 +40,11 @@ func (c *MediaAnnotationController) SetRating(w http.ResponseWriter, r *http.Req
|
||||
log.Debug(r, "Setting rating", "rating", rating, "id", id)
|
||||
err = c.setRating(r.Context(), id, rating)
|
||||
|
||||
switch {
|
||||
case err == model.ErrNotFound:
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
log.Error(r, err)
|
||||
return nil, newError(responses.ErrorDataNotFound, "ID not found")
|
||||
case err != nil:
|
||||
}
|
||||
if err != nil {
|
||||
log.Error(r, err)
|
||||
return nil, err
|
||||
}
|
||||
@@ -161,11 +162,11 @@ func (c *MediaAnnotationController) setStar(ctx context.Context, star bool, ids
|
||||
return nil
|
||||
})
|
||||
|
||||
switch {
|
||||
case err == model.ErrNotFound:
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
log.Error(ctx, err)
|
||||
return newError(responses.ErrorDataNotFound, "ID not found")
|
||||
case err != nil:
|
||||
}
|
||||
if err != nil {
|
||||
log.Error(ctx, err)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package subsonic
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"regexp"
|
||||
@@ -66,11 +67,11 @@ func (c *MediaRetrievalController) GetCoverArt(w http.ResponseWriter, r *http.Re
|
||||
w.Header().Set("cache-control", "public, max-age=315360000")
|
||||
|
||||
imgReader, err := c.artwork.Get(r.Context(), id, size)
|
||||
switch {
|
||||
case err == model.ErrNotFound:
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
log.Error(r, "Couldn't find coverArt", "id", id, err)
|
||||
return nil, newError(responses.ErrorDataNotFound, "Artwork not found")
|
||||
case err != nil:
|
||||
}
|
||||
if err != nil {
|
||||
log.Error(r, "Error retrieving coverArt", "id", id, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
@@ -78,7 +79,7 @@ func authenticate(ds model.DataStore) func(next http.Handler) http.Handler {
|
||||
jwt := utils.ParamString(r, "jwt")
|
||||
|
||||
usr, err := validateUser(ctx, ds, username, pass, token, salt, jwt)
|
||||
if err == model.ErrInvalidAuth {
|
||||
if errors.Is(err, model.ErrInvalidAuth) {
|
||||
log.Warn(ctx, "API: Invalid login", "username", username, "remoteAddr", r.RemoteAddr, err)
|
||||
} else if err != nil {
|
||||
log.Error(ctx, "API: Error authenticating username", "username", username, "remoteAddr", r.RemoteAddr, err)
|
||||
@@ -107,7 +108,7 @@ func authenticate(ds model.DataStore) func(next http.Handler) http.Handler {
|
||||
|
||||
func validateUser(ctx context.Context, ds model.DataStore, username, pass, token, salt, jwt string) (*model.User, error) {
|
||||
user, err := ds.User(ctx).FindByUsernameWithPassword(username)
|
||||
if err == model.ErrNotFound {
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
return nil, model.ErrInvalidAuth
|
||||
}
|
||||
if err != nil {
|
||||
|
||||
@@ -49,11 +49,11 @@ func (c *PlaylistsController) GetPlaylist(w http.ResponseWriter, r *http.Request
|
||||
|
||||
func (c *PlaylistsController) getPlaylist(ctx context.Context, id string) (*responses.Subsonic, error) {
|
||||
pls, err := c.ds.Playlist(ctx).GetWithTracks(id)
|
||||
switch {
|
||||
case err == model.ErrNotFound:
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
log.Error(ctx, err.Error(), "id", id)
|
||||
return nil, newError(responses.ErrorDataNotFound, "Directory not found")
|
||||
case err != nil:
|
||||
}
|
||||
if err != nil {
|
||||
log.Error(ctx, err)
|
||||
return nil, err
|
||||
}
|
||||
@@ -113,7 +113,7 @@ func (c *PlaylistsController) DeletePlaylist(w http.ResponseWriter, r *http.Requ
|
||||
return nil, err
|
||||
}
|
||||
err = c.ds.Playlist(r.Context()).Delete(id)
|
||||
if err == model.ErrNotAuthorized {
|
||||
if errors.Is(err, model.ErrNotAuthorized) {
|
||||
return nil, newError(responses.ErrorAuthorizationFail)
|
||||
}
|
||||
if err != nil {
|
||||
@@ -152,7 +152,7 @@ func (c *PlaylistsController) UpdatePlaylist(w http.ResponseWriter, r *http.Requ
|
||||
log.Trace(r, fmt.Sprintf("-- Removing: '%v'", songIndexesToRemove))
|
||||
|
||||
err = c.pls.Update(r.Context(), playlistId, plsName, comment, public, songsToAdd, songIndexesToRemove)
|
||||
if err == model.ErrNotAuthorized {
|
||||
if errors.Is(err, model.ErrNotAuthorized) {
|
||||
return nil, newError(responses.ErrorAuthorizationFail)
|
||||
}
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user