refactor(server): final writeErr migration + ErrNotFound aliases (T3c)

This commit is contained in:
2026-05-07 21:19:35 -04:00
parent 91f4f5c18a
commit 754a7955cc
14 changed files with 182 additions and 147 deletions
+10 -9
View File
@@ -6,6 +6,7 @@ import (
"net/http"
"strings"
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/lidarr"
)
@@ -40,24 +41,24 @@ func (h *handlers) handleLidarrSearch(w http.ResponseWriter, r *http.Request) {
case "artist", "album", "track":
// valid
default:
writeErr(w, http.StatusBadRequest, "bad_kind", "kind must be artist, album, or track")
writeErr(w, apierror.BadRequest("bad_kind", "kind must be artist, album, or track"))
return
}
q := strings.TrimSpace(r.URL.Query().Get("q"))
if q == "" {
writeErr(w, http.StatusBadRequest, "missing_query", "q is required")
writeErr(w, apierror.BadRequest("missing_query", "q is required"))
return
}
cfg, err := h.lidarrCfg.Get(r.Context())
if err != nil {
h.logger.Error("api: lidarr search: load config", "err", err)
writeErr(w, http.StatusInternalServerError, "lidarr_lookup_failed", "failed to load lidarr config")
writeErr(w, &apierror.Error{Status: 500, Code: "lidarr_lookup_failed", Message: "failed to load lidarr config", Cause: err})
return
}
if !cfg.Enabled {
writeErr(w, http.StatusServiceUnavailable, "lidarr_disabled", "Lidarr integration is not enabled")
writeErr(w, &apierror.Error{Status: 503, Code: "lidarr_disabled", Message: "Lidarr integration is not enabled"})
return
}
@@ -75,12 +76,12 @@ func (h *handlers) handleLidarrSearch(w http.ResponseWriter, r *http.Request) {
if err != nil {
switch {
case errors.Is(err, lidarr.ErrUnreachable):
writeErr(w, http.StatusServiceUnavailable, "lidarr_unreachable", "Lidarr is unreachable")
writeErr(w, &apierror.Error{Status: 503, Code: "lidarr_unreachable", Message: "Lidarr is unreachable", Cause: err})
case errors.Is(err, lidarr.ErrAuthFailed):
writeErr(w, http.StatusServiceUnavailable, "lidarr_auth_failed", "Lidarr authentication failed")
writeErr(w, &apierror.Error{Status: 503, Code: "lidarr_auth_failed", Message: "Lidarr authentication failed", Cause: err})
default:
h.logger.Error("api: lidarr search: lookup failed", "kind", kind, "err", err)
writeErr(w, http.StatusInternalServerError, "lidarr_lookup_failed", "Lidarr lookup failed")
writeErr(w, &apierror.Error{Status: 500, Code: "lidarr_lookup_failed", Message: "Lidarr lookup failed", Cause: err})
}
return
}
@@ -91,13 +92,13 @@ func (h *handlers) handleLidarrSearch(w http.ResponseWriter, r *http.Request) {
inLib, libErr := h.lidarrInLibrary(r.Context(), kind, res.MBID)
if libErr != nil {
h.logger.Error("api: lidarr search: in_library check failed", "err", libErr)
writeErr(w, http.StatusInternalServerError, "lidarr_lookup_failed", "library check failed")
writeErr(w, &apierror.Error{Status: 500, Code: "lidarr_lookup_failed", Message: "library check failed", Cause: libErr})
return
}
requested, reqErr := dbQ.HasNonTerminalRequestForMBID(r.Context(), res.MBID)
if reqErr != nil {
h.logger.Error("api: lidarr search: requested check failed", "err", reqErr)
writeErr(w, http.StatusInternalServerError, "lidarr_lookup_failed", "request check failed")
writeErr(w, &apierror.Error{Status: 500, Code: "lidarr_lookup_failed", Message: "request check failed", Cause: reqErr})
return
}
out = append(out, lidarrSearchResult{
+44 -43
View File
@@ -9,6 +9,7 @@ import (
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
"git.fabledsword.com/bvandeusen/minstrel/internal/auth"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/playevents"
@@ -23,28 +24,28 @@ type likedIDsResponse struct {
func (h *handlers) handleLikeTrack(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context())
if !ok {
writeErr(w, http.StatusUnauthorized, "unauthorized", "authentication required")
writeErr(w, apierror.ErrUnauthorized)
return
}
id, ok := parseUUID(chi.URLParam(r, "id"))
if !ok {
writeErr(w, http.StatusBadRequest, "bad_request", "invalid track id")
writeErr(w, apierror.BadRequest("bad_request", "invalid track id"))
return
}
q := dbq.New(h.pool)
if _, err := q.GetTrackByID(r.Context(), id); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
writeErr(w, http.StatusNotFound, "not_found", "track not found")
writeErr(w, apierror.NotFound("track"))
return
}
h.logger.Error("api: like track lookup", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "lookup failed")
writeErr(w, apierror.InternalMsg("lookup failed", err))
return
}
rows, err := q.LikeTrack(r.Context(), dbq.LikeTrackParams{UserID: user.ID, TrackID: id})
if err != nil {
h.logger.Error("api: like track insert", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "insert failed")
writeErr(w, apierror.InternalMsg("insert failed", err))
return
}
if rows == 1 {
@@ -56,18 +57,18 @@ func (h *handlers) handleLikeTrack(w http.ResponseWriter, r *http.Request) {
func (h *handlers) handleUnlikeTrack(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context())
if !ok {
writeErr(w, http.StatusUnauthorized, "unauthorized", "authentication required")
writeErr(w, apierror.ErrUnauthorized)
return
}
id, ok := parseUUID(chi.URLParam(r, "id"))
if !ok {
writeErr(w, http.StatusBadRequest, "bad_request", "invalid track id")
writeErr(w, apierror.BadRequest("bad_request", "invalid track id"))
return
}
q := dbq.New(h.pool)
if err := q.UnlikeTrack(r.Context(), dbq.UnlikeTrackParams{UserID: user.ID, TrackID: id}); err != nil {
h.logger.Error("api: unlike track", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "delete failed")
writeErr(w, apierror.InternalMsg("delete failed", err))
return
}
if err := playevents.SoftDeleteContextualLikes(r.Context(), q, user.ID, id); err != nil {
@@ -80,27 +81,27 @@ func (h *handlers) handleUnlikeTrack(w http.ResponseWriter, r *http.Request) {
func (h *handlers) handleLikeAlbum(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context())
if !ok {
writeErr(w, http.StatusUnauthorized, "unauthorized", "authentication required")
writeErr(w, apierror.ErrUnauthorized)
return
}
id, ok := parseUUID(chi.URLParam(r, "id"))
if !ok {
writeErr(w, http.StatusBadRequest, "bad_request", "invalid album id")
writeErr(w, apierror.BadRequest("bad_request", "invalid album id"))
return
}
q := dbq.New(h.pool)
if _, err := q.GetAlbumByID(r.Context(), id); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
writeErr(w, http.StatusNotFound, "not_found", "album not found")
writeErr(w, apierror.NotFound("album"))
return
}
h.logger.Error("api: like album lookup", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "lookup failed")
writeErr(w, apierror.InternalMsg("lookup failed", err))
return
}
if err := q.LikeAlbum(r.Context(), dbq.LikeAlbumParams{UserID: user.ID, AlbumID: id}); err != nil {
h.logger.Error("api: like album insert", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "insert failed")
writeErr(w, apierror.InternalMsg("insert failed", err))
return
}
w.WriteHeader(http.StatusNoContent)
@@ -109,17 +110,17 @@ func (h *handlers) handleLikeAlbum(w http.ResponseWriter, r *http.Request) {
func (h *handlers) handleUnlikeAlbum(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context())
if !ok {
writeErr(w, http.StatusUnauthorized, "unauthorized", "authentication required")
writeErr(w, apierror.ErrUnauthorized)
return
}
id, ok := parseUUID(chi.URLParam(r, "id"))
if !ok {
writeErr(w, http.StatusBadRequest, "bad_request", "invalid album id")
writeErr(w, apierror.BadRequest("bad_request", "invalid album id"))
return
}
if err := dbq.New(h.pool).UnlikeAlbum(r.Context(), dbq.UnlikeAlbumParams{UserID: user.ID, AlbumID: id}); err != nil {
h.logger.Error("api: unlike album", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "delete failed")
writeErr(w, apierror.InternalMsg("delete failed", err))
return
}
w.WriteHeader(http.StatusNoContent)
@@ -128,27 +129,27 @@ func (h *handlers) handleUnlikeAlbum(w http.ResponseWriter, r *http.Request) {
func (h *handlers) handleLikeArtist(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context())
if !ok {
writeErr(w, http.StatusUnauthorized, "unauthorized", "authentication required")
writeErr(w, apierror.ErrUnauthorized)
return
}
id, ok := parseUUID(chi.URLParam(r, "id"))
if !ok {
writeErr(w, http.StatusBadRequest, "bad_request", "invalid artist id")
writeErr(w, apierror.BadRequest("bad_request", "invalid artist id"))
return
}
q := dbq.New(h.pool)
if _, err := q.GetArtistByID(r.Context(), id); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
writeErr(w, http.StatusNotFound, "not_found", "artist not found")
writeErr(w, apierror.NotFound("artist"))
return
}
h.logger.Error("api: like artist lookup", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "lookup failed")
writeErr(w, apierror.InternalMsg("lookup failed", err))
return
}
if err := q.LikeArtist(r.Context(), dbq.LikeArtistParams{UserID: user.ID, ArtistID: id}); err != nil {
h.logger.Error("api: like artist insert", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "insert failed")
writeErr(w, apierror.InternalMsg("insert failed", err))
return
}
w.WriteHeader(http.StatusNoContent)
@@ -157,17 +158,17 @@ func (h *handlers) handleLikeArtist(w http.ResponseWriter, r *http.Request) {
func (h *handlers) handleUnlikeArtist(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context())
if !ok {
writeErr(w, http.StatusUnauthorized, "unauthorized", "authentication required")
writeErr(w, apierror.ErrUnauthorized)
return
}
id, ok := parseUUID(chi.URLParam(r, "id"))
if !ok {
writeErr(w, http.StatusBadRequest, "bad_request", "invalid artist id")
writeErr(w, apierror.BadRequest("bad_request", "invalid artist id"))
return
}
if err := dbq.New(h.pool).UnlikeArtist(r.Context(), dbq.UnlikeArtistParams{UserID: user.ID, ArtistID: id}); err != nil {
h.logger.Error("api: unlike artist", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "delete failed")
writeErr(w, apierror.InternalMsg("delete failed", err))
return
}
w.WriteHeader(http.StatusNoContent)
@@ -176,12 +177,12 @@ func (h *handlers) handleUnlikeArtist(w http.ResponseWriter, r *http.Request) {
func (h *handlers) handleListLikedTracks(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context())
if !ok {
writeErr(w, http.StatusUnauthorized, "unauthorized", "authentication required")
writeErr(w, apierror.ErrUnauthorized)
return
}
limit, offset, err := parsePaging(r.URL.Query())
if err != nil {
writeErr(w, http.StatusBadRequest, "bad_request", err.Error())
writeErr(w, apierror.BadRequest("bad_request", err.Error()))
return
}
q := dbq.New(h.pool)
@@ -190,18 +191,18 @@ func (h *handlers) handleListLikedTracks(w http.ResponseWriter, r *http.Request)
})
if err != nil {
h.logger.Error("api: list liked tracks", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "list failed")
writeErr(w, apierror.InternalMsg("list failed", err))
return
}
total, err := q.CountLikedTracks(r.Context(), user.ID)
if err != nil {
h.logger.Error("api: count liked tracks", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "count failed")
writeErr(w, apierror.InternalMsg("count failed", err))
return
}
items, err := h.resolveTrackRefs(r.Context(), q, rows)
if err != nil {
writeErr(w, http.StatusInternalServerError, "server_error", "list failed")
writeErr(w, apierror.InternalMsg("list failed", err))
return
}
writeJSON(w, http.StatusOK, Page[TrackRef]{Items: items, Total: int(total), Limit: limit, Offset: offset})
@@ -210,12 +211,12 @@ func (h *handlers) handleListLikedTracks(w http.ResponseWriter, r *http.Request)
func (h *handlers) handleListLikedAlbums(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context())
if !ok {
writeErr(w, http.StatusUnauthorized, "unauthorized", "authentication required")
writeErr(w, apierror.ErrUnauthorized)
return
}
limit, offset, err := parsePaging(r.URL.Query())
if err != nil {
writeErr(w, http.StatusBadRequest, "bad_request", err.Error())
writeErr(w, apierror.BadRequest("bad_request", err.Error()))
return
}
q := dbq.New(h.pool)
@@ -224,18 +225,18 @@ func (h *handlers) handleListLikedAlbums(w http.ResponseWriter, r *http.Request)
})
if err != nil {
h.logger.Error("api: list liked albums", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "list failed")
writeErr(w, apierror.InternalMsg("list failed", err))
return
}
total, err := q.CountLikedAlbums(r.Context(), user.ID)
if err != nil {
h.logger.Error("api: count liked albums", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "count failed")
writeErr(w, apierror.InternalMsg("count failed", err))
return
}
items, err := h.resolveAlbumRefs(r.Context(), q, rows)
if err != nil {
writeErr(w, http.StatusInternalServerError, "server_error", "list failed")
writeErr(w, apierror.InternalMsg("list failed", err))
return
}
writeJSON(w, http.StatusOK, Page[AlbumRef]{Items: items, Total: int(total), Limit: limit, Offset: offset})
@@ -244,12 +245,12 @@ func (h *handlers) handleListLikedAlbums(w http.ResponseWriter, r *http.Request)
func (h *handlers) handleListLikedArtists(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context())
if !ok {
writeErr(w, http.StatusUnauthorized, "unauthorized", "authentication required")
writeErr(w, apierror.ErrUnauthorized)
return
}
limit, offset, err := parsePaging(r.URL.Query())
if err != nil {
writeErr(w, http.StatusBadRequest, "bad_request", err.Error())
writeErr(w, apierror.BadRequest("bad_request", err.Error()))
return
}
q := dbq.New(h.pool)
@@ -258,13 +259,13 @@ func (h *handlers) handleListLikedArtists(w http.ResponseWriter, r *http.Request
})
if err != nil {
h.logger.Error("api: list liked artists", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "list failed")
writeErr(w, apierror.InternalMsg("list failed", err))
return
}
total, err := q.CountLikedArtists(r.Context(), user.ID)
if err != nil {
h.logger.Error("api: count liked artists", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "count failed")
writeErr(w, apierror.InternalMsg("count failed", err))
return
}
items := make([]ArtistRef, 0, len(rows))
@@ -272,7 +273,7 @@ func (h *handlers) handleListLikedArtists(w http.ResponseWriter, r *http.Request
albums, aerr := q.ListAlbumsByArtist(r.Context(), a.ID)
if aerr != nil {
h.logger.Error("api: list albums for artist", "err", aerr)
writeErr(w, http.StatusInternalServerError, "server_error", "list failed")
writeErr(w, apierror.InternalMsg("list failed", aerr))
return
}
items = append(items, artistRefFrom(a, len(albums)))
@@ -283,26 +284,26 @@ func (h *handlers) handleListLikedArtists(w http.ResponseWriter, r *http.Request
func (h *handlers) handleGetLikedIDs(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context())
if !ok {
writeErr(w, http.StatusUnauthorized, "unauthorized", "authentication required")
writeErr(w, apierror.ErrUnauthorized)
return
}
q := dbq.New(h.pool)
trackIDs, err := q.ListLikedTrackIDs(r.Context(), user.ID)
if err != nil {
h.logger.Error("api: list liked track ids", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "list failed")
writeErr(w, apierror.InternalMsg("list failed", err))
return
}
albumIDs, err := q.ListLikedAlbumIDs(r.Context(), user.ID)
if err != nil {
h.logger.Error("api: list liked album ids", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "list failed")
writeErr(w, apierror.InternalMsg("list failed", err))
return
}
artistIDs, err := q.ListLikedArtistIDs(r.Context(), user.ID)
if err != nil {
h.logger.Error("api: list liked artist ids", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "list failed")
writeErr(w, apierror.InternalMsg("list failed", err))
return
}
resp := likedIDsResponse{
+10 -9
View File
@@ -10,6 +10,7 @@ import (
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
"git.fabledsword.com/bvandeusen/minstrel/internal/auth"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
)
@@ -26,13 +27,13 @@ type LBStatus struct {
func (h *handlers) handleGetListenBrainz(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context())
if !ok {
writeErr(w, http.StatusUnauthorized, "unauthorized", "authentication required")
writeErr(w, apierror.ErrUnauthorized)
return
}
resp, err := h.buildLBStatus(r.Context(), user.ID)
if err != nil {
h.logger.Error("api: get listenbrainz", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "lookup failed")
writeErr(w, apierror.InternalMsg("lookup failed", err))
return
}
writeJSON(w, http.StatusOK, resp)
@@ -46,12 +47,12 @@ type putLBBody struct {
func (h *handlers) handlePutListenBrainz(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context())
if !ok {
writeErr(w, http.StatusUnauthorized, "unauthorized", "authentication required")
writeErr(w, apierror.ErrUnauthorized)
return
}
var body putLBBody
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
writeErr(w, http.StatusBadRequest, "bad_request", "invalid json")
writeErr(w, apierror.BadRequest("bad_request", "invalid json"))
return
}
@@ -66,7 +67,7 @@ func (h *handlers) handlePutListenBrainz(w http.ResponseWriter, r *http.Request)
ID: user.ID, ListenbrainzToken: tokenPtr,
}); err != nil {
h.logger.Error("api: set listenbrainz token", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "update failed")
writeErr(w, apierror.InternalMsg("update failed", err))
return
}
}
@@ -76,11 +77,11 @@ func (h *handlers) handlePutListenBrainz(w http.ResponseWriter, r *http.Request)
cfg, err := q.GetListenBrainzConfig(r.Context(), user.ID)
if err != nil {
h.logger.Error("api: get listenbrainz config (pre-enable)", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "lookup failed")
writeErr(w, apierror.InternalMsg("lookup failed", err))
return
}
if cfg.ListenbrainzToken == nil || *cfg.ListenbrainzToken == "" {
writeErr(w, http.StatusBadRequest, "bad_request", "cannot enable without a token")
writeErr(w, apierror.BadRequest("bad_request", "cannot enable without a token"))
return
}
}
@@ -89,7 +90,7 @@ func (h *handlers) handlePutListenBrainz(w http.ResponseWriter, r *http.Request)
ID: user.ID, ListenbrainzEnabled: *body.Enabled,
}); err != nil {
h.logger.Error("api: set listenbrainz enabled", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "update failed")
writeErr(w, apierror.InternalMsg("update failed", err))
return
}
}
@@ -97,7 +98,7 @@ func (h *handlers) handlePutListenBrainz(w http.ResponseWriter, r *http.Request)
resp, err := h.buildLBStatus(r.Context(), user.ID)
if err != nil {
h.logger.Error("api: put listenbrainz refresh status", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "lookup failed")
writeErr(w, apierror.InternalMsg("lookup failed", err))
return
}
writeJSON(w, http.StatusOK, resp)
+32 -31
View File
@@ -15,6 +15,7 @@ import (
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
"git.fabledsword.com/bvandeusen/minstrel/internal/auth"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/playlists"
@@ -89,7 +90,7 @@ type listPlaylistsResponse struct {
func (h *handlers) handleListPlaylists(w http.ResponseWriter, r *http.Request) {
caller, ok := auth.UserFromContext(r.Context())
if !ok {
writeErr(w, http.StatusUnauthorized, "unauthenticated", "no session")
writeErr(w, apierror.Unauthorized("unauthenticated", "no session"))
return
}
@@ -101,7 +102,7 @@ func (h *handlers) handleListPlaylists(w http.ResponseWriter, r *http.Request) {
case "user", "system", "all":
// valid
default:
writeErr(w, http.StatusBadRequest, "bad_kind", "kind must be one of: user, system, all")
writeErr(w, apierror.BadRequest("bad_kind", "kind must be one of: user, system, all"))
return
}
@@ -114,7 +115,7 @@ func (h *handlers) handleListPlaylists(w http.ResponseWriter, r *http.Request) {
rows, err := h.playlists.List(r.Context(), caller.ID)
if err != nil {
h.logger.Error("api: list playlists failed", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "list failed")
writeErr(w, apierror.InternalMsg("list failed", err))
return
}
resp := listPlaylistsResponse{Owned: []playlistRowView{}, Public: []playlistRowView{}}
@@ -176,12 +177,12 @@ type createPlaylistBody struct {
func (h *handlers) handleCreatePlaylist(w http.ResponseWriter, r *http.Request) {
caller, ok := auth.UserFromContext(r.Context())
if !ok {
writeErr(w, http.StatusUnauthorized, "unauthenticated", "no session")
writeErr(w, apierror.Unauthorized("unauthenticated", "no session"))
return
}
var body createPlaylistBody
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
writeErr(w, http.StatusBadRequest, "bad_request", "invalid JSON body")
writeErr(w, apierror.BadRequest("bad_request", "invalid JSON body"))
return
}
row, err := h.playlists.Create(r.Context(), caller.ID, body.Name, body.Description, body.IsPublic)
@@ -198,12 +199,12 @@ func (h *handlers) handleCreatePlaylist(w http.ResponseWriter, r *http.Request)
func (h *handlers) handleGetPlaylist(w http.ResponseWriter, r *http.Request) {
caller, ok := auth.UserFromContext(r.Context())
if !ok {
writeErr(w, http.StatusUnauthorized, "unauthenticated", "no session")
writeErr(w, apierror.Unauthorized("unauthenticated", "no session"))
return
}
playlistID, ok := parseUUID(chi.URLParam(r, "id"))
if !ok {
writeErr(w, http.StatusBadRequest, "bad_request", "invalid playlist id")
writeErr(w, apierror.BadRequest("bad_request", "invalid playlist id"))
return
}
detail, err := h.playlists.Get(r.Context(), caller.ID, playlistID)
@@ -227,17 +228,17 @@ type updatePlaylistBody struct {
func (h *handlers) handleUpdatePlaylist(w http.ResponseWriter, r *http.Request) {
caller, ok := auth.UserFromContext(r.Context())
if !ok {
writeErr(w, http.StatusUnauthorized, "unauthenticated", "no session")
writeErr(w, apierror.Unauthorized("unauthenticated", "no session"))
return
}
playlistID, ok := parseUUID(chi.URLParam(r, "id"))
if !ok {
writeErr(w, http.StatusBadRequest, "bad_request", "invalid playlist id")
writeErr(w, apierror.BadRequest("bad_request", "invalid playlist id"))
return
}
var body updatePlaylistBody
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
writeErr(w, http.StatusBadRequest, "bad_request", "invalid JSON body")
writeErr(w, apierror.BadRequest("bad_request", "invalid JSON body"))
return
}
row, err := h.playlists.Update(r.Context(), caller.ID, playlistID, playlists.UpdateInput{
@@ -257,12 +258,12 @@ func (h *handlers) handleUpdatePlaylist(w http.ResponseWriter, r *http.Request)
func (h *handlers) handleDeletePlaylist(w http.ResponseWriter, r *http.Request) {
caller, ok := auth.UserFromContext(r.Context())
if !ok {
writeErr(w, http.StatusUnauthorized, "unauthenticated", "no session")
writeErr(w, apierror.Unauthorized("unauthenticated", "no session"))
return
}
playlistID, ok := parseUUID(chi.URLParam(r, "id"))
if !ok {
writeErr(w, http.StatusBadRequest, "bad_request", "invalid playlist id")
writeErr(w, apierror.BadRequest("bad_request", "invalid playlist id"))
return
}
if err := h.playlists.Delete(r.Context(), caller.ID, playlistID); err != nil {
@@ -282,24 +283,24 @@ type appendTracksBody struct {
func (h *handlers) handleAppendTracks(w http.ResponseWriter, r *http.Request) {
caller, ok := auth.UserFromContext(r.Context())
if !ok {
writeErr(w, http.StatusUnauthorized, "unauthenticated", "no session")
writeErr(w, apierror.Unauthorized("unauthenticated", "no session"))
return
}
playlistID, ok := parseUUID(chi.URLParam(r, "id"))
if !ok {
writeErr(w, http.StatusBadRequest, "bad_request", "invalid playlist id")
writeErr(w, apierror.BadRequest("bad_request", "invalid playlist id"))
return
}
var body appendTracksBody
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
writeErr(w, http.StatusBadRequest, "bad_request", "invalid JSON body")
writeErr(w, apierror.BadRequest("bad_request", "invalid JSON body"))
return
}
trackIDs := make([]pgtype.UUID, 0, len(body.TrackIDs))
for _, s := range body.TrackIDs {
u, ok := parseUUID(s)
if !ok {
writeErr(w, http.StatusBadRequest, "bad_request", fmt.Sprintf("invalid track id %q", s))
writeErr(w, apierror.BadRequest("bad_request", fmt.Sprintf("invalid track id %q", s)))
return
}
trackIDs = append(trackIDs, u)
@@ -321,18 +322,18 @@ func (h *handlers) handleAppendTracks(w http.ResponseWriter, r *http.Request) {
func (h *handlers) handleRemovePlaylistTrack(w http.ResponseWriter, r *http.Request) {
caller, ok := auth.UserFromContext(r.Context())
if !ok {
writeErr(w, http.StatusUnauthorized, "unauthenticated", "no session")
writeErr(w, apierror.Unauthorized("unauthenticated", "no session"))
return
}
playlistID, ok := parseUUID(chi.URLParam(r, "id"))
if !ok {
writeErr(w, http.StatusBadRequest, "bad_request", "invalid playlist id")
writeErr(w, apierror.BadRequest("bad_request", "invalid playlist id"))
return
}
posStr := chi.URLParam(r, "position")
pos64, err := strconv.ParseInt(posStr, 10, 32)
if err != nil || pos64 < 0 {
writeErr(w, http.StatusBadRequest, "bad_request", "invalid position")
writeErr(w, apierror.BadRequest("bad_request", "invalid position"))
return
}
if err := h.playlists.RemoveTrack(r.Context(), caller.ID, playlistID, int32(pos64)); err != nil {
@@ -357,17 +358,17 @@ type reorderTracksBody struct {
func (h *handlers) handleReorderPlaylist(w http.ResponseWriter, r *http.Request) {
caller, ok := auth.UserFromContext(r.Context())
if !ok {
writeErr(w, http.StatusUnauthorized, "unauthenticated", "no session")
writeErr(w, apierror.Unauthorized("unauthenticated", "no session"))
return
}
playlistID, ok := parseUUID(chi.URLParam(r, "id"))
if !ok {
writeErr(w, http.StatusBadRequest, "bad_request", "invalid playlist id")
writeErr(w, apierror.BadRequest("bad_request", "invalid playlist id"))
return
}
var body reorderTracksBody
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
writeErr(w, http.StatusBadRequest, "bad_request", "invalid JSON body")
writeErr(w, apierror.BadRequest("bad_request", "invalid JSON body"))
return
}
if err := h.playlists.Reorder(r.Context(), caller.ID, playlistID, body.OrderedPositions); err != nil {
@@ -389,12 +390,12 @@ func (h *handlers) handleReorderPlaylist(w http.ResponseWriter, r *http.Request)
func (h *handlers) handleGetPlaylistCover(w http.ResponseWriter, r *http.Request) {
caller, ok := auth.UserFromContext(r.Context())
if !ok {
writeErr(w, http.StatusUnauthorized, "unauthenticated", "no session")
writeErr(w, apierror.Unauthorized("unauthenticated", "no session"))
return
}
playlistID, ok := parseUUID(chi.URLParam(r, "id"))
if !ok {
writeErr(w, http.StatusBadRequest, "bad_request", "invalid playlist id")
writeErr(w, apierror.BadRequest("bad_request", "invalid playlist id"))
return
}
detail, err := h.playlists.Get(r.Context(), caller.ID, playlistID)
@@ -403,7 +404,7 @@ func (h *handlers) handleGetPlaylistCover(w http.ResponseWriter, r *http.Request
return
}
if detail.CoverPath == nil || *detail.CoverPath == "" {
writeErr(w, http.StatusNotFound, "not_found", "no cover")
writeErr(w, &apierror.Error{Status: 404, Code: "not_found", Message: "no cover"})
return
}
full := filepath.Join(h.dataDir, *detail.CoverPath)
@@ -417,18 +418,18 @@ func (h *handlers) handleGetPlaylistCover(w http.ResponseWriter, r *http.Request
func (h *handlers) writePlaylistErr(w http.ResponseWriter, err error, op string) {
switch {
case errors.Is(err, playlists.ErrNotFound):
writeErr(w, http.StatusNotFound, "not_found", "playlist not found")
writeErr(w, apierror.NotFound("playlist"))
case errors.Is(err, playlists.ErrForbidden):
writeErr(w, http.StatusForbidden, "not_authorized", "you don't own this playlist")
writeErr(w, apierror.Forbidden("not_authorized", "you don't own this playlist"))
case errors.Is(err, playlists.ErrSystemPlaylistReadonly):
writeErr(w, http.StatusForbidden, "system_playlist_readonly", "system-generated playlists cannot be edited")
writeErr(w, apierror.Forbidden("system_playlist_readonly", "system-generated playlists cannot be edited"))
case errors.Is(err, playlists.ErrInvalidInput):
writeErr(w, http.StatusBadRequest, "bad_request", err.Error())
writeErr(w, apierror.BadRequest("bad_request", err.Error()))
case errors.Is(err, playlists.ErrTrackNotFound):
writeErr(w, http.StatusBadRequest, "bad_request", "one of the supplied track ids does not exist")
writeErr(w, apierror.BadRequest("bad_request", "one of the supplied track ids does not exist"))
default:
h.logger.Error("api: playlist op failed", "op", op, "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", op+" failed")
writeErr(w, apierror.InternalMsg(op+" failed", err))
}
}
+4 -3
View File
@@ -7,6 +7,7 @@ import (
"github.com/jackc/pgx/v5"
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
"git.fabledsword.com/bvandeusen/minstrel/internal/auth"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/playlists"
@@ -32,13 +33,13 @@ type discoverRefreshResp struct {
func (h *handlers) handleDiscoverRefresh(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context())
if !ok {
writeErr(w, http.StatusUnauthorized, "auth_required", "")
writeErr(w, apierror.Unauthorized("auth_required", ""))
return
}
if err := playlists.BuildSystemPlaylists(r.Context(), h.pool, h.logger, user.ID, time.Now(), h.dataDir); err != nil {
h.logger.Error("discover refresh: build failed",
"user_id", uuidToString(user.ID), "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "build failed")
writeErr(w, apierror.InternalMsg("build failed", err))
return
}
@@ -58,7 +59,7 @@ func (h *handlers) handleDiscoverRefresh(w http.ResponseWriter, r *http.Request)
}
h.logger.Error("discover refresh: lookup failed",
"user_id", uuidToString(user.ID), "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "lookup failed")
writeErr(w, apierror.InternalMsg("lookup failed", err))
return
}
+5 -4
View File
@@ -7,6 +7,7 @@ import (
"github.com/jackc/pgx/v5"
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
"git.fabledsword.com/bvandeusen/minstrel/internal/auth"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/playlists"
@@ -37,13 +38,13 @@ type foryouRefreshResp struct {
func (h *handlers) handleForYouRefresh(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context())
if !ok {
writeErr(w, http.StatusUnauthorized, "auth_required", "")
writeErr(w, apierror.Unauthorized("auth_required", ""))
return
}
if err := playlists.BuildSystemPlaylists(r.Context(), h.pool, h.logger, user.ID, time.Now(), h.dataDir); err != nil {
h.logger.Error("for-you refresh: build failed",
"user_id", uuidToString(user.ID), "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "build failed")
writeErr(w, apierror.InternalMsg("build failed", err))
return
}
@@ -66,7 +67,7 @@ func (h *handlers) handleForYouRefresh(w http.ResponseWriter, r *http.Request) {
}
h.logger.Error("for-you refresh: lookup failed",
"user_id", uuidToString(user.ID), "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "lookup failed")
writeErr(w, apierror.InternalMsg("lookup failed", err))
return
}
@@ -76,7 +77,7 @@ func (h *handlers) handleForYouRefresh(w http.ResponseWriter, r *http.Request) {
if err != nil {
h.logger.Error("for-you refresh: list tracks failed",
"playlist_id", uuidToString(pl.ID), "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "list tracks failed")
writeErr(w, apierror.InternalMsg("list tracks failed", err))
return
}
trackIDs := make([]string, 0, len(rows))
+13 -12
View File
@@ -8,6 +8,7 @@ import (
"github.com/go-chi/chi/v5"
"github.com/jackc/pgx/v5/pgtype"
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
"git.fabledsword.com/bvandeusen/minstrel/internal/auth"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/lidarrquarantine"
@@ -45,29 +46,29 @@ type flagBody struct {
func (h *handlers) handleFlag(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context())
if !ok {
writeErr(w, http.StatusUnauthorized, "unauthorized", "authentication required")
writeErr(w, apierror.ErrUnauthorized)
return
}
var body flagBody
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
writeErr(w, http.StatusBadRequest, "bad_request", "invalid JSON body")
writeErr(w, apierror.BadRequest("bad_request", "invalid JSON body"))
return
}
trackID, ok := parseUUID(body.TrackID)
if !ok {
writeErr(w, http.StatusBadRequest, "bad_request", "invalid track id")
writeErr(w, apierror.BadRequest("bad_request", "invalid track id"))
return
}
row, err := h.lidarrQuarantine.Flag(r.Context(), user.ID, trackID, body.Reason, body.Notes)
if err != nil {
switch {
case errors.Is(err, lidarrquarantine.ErrBadReason):
writeErr(w, http.StatusBadRequest, "bad_reason", "invalid reason")
writeErr(w, apierror.BadRequest("bad_reason", "invalid reason"))
case errors.Is(err, lidarrquarantine.ErrTrackNotFound):
writeErr(w, http.StatusNotFound, "track_not_found", "track does not exist")
writeErr(w, &apierror.Error{Status: 404, Code: "track_not_found", Message: "track does not exist"})
default:
h.logger.Error("api: flag", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "flag failed")
writeErr(w, apierror.InternalMsg("flag failed", err))
}
return
}
@@ -78,21 +79,21 @@ func (h *handlers) handleFlag(w http.ResponseWriter, r *http.Request) {
func (h *handlers) handleUnflag(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context())
if !ok {
writeErr(w, http.StatusUnauthorized, "unauthorized", "authentication required")
writeErr(w, apierror.ErrUnauthorized)
return
}
id, ok := parseUUID(chi.URLParam(r, "track_id"))
if !ok {
writeErr(w, http.StatusBadRequest, "bad_request", "invalid track id")
writeErr(w, apierror.BadRequest("bad_request", "invalid track id"))
return
}
if err := h.lidarrQuarantine.Unflag(r.Context(), user.ID, id); err != nil {
if errors.Is(err, lidarrquarantine.ErrQuarantineNotFound) {
writeErr(w, http.StatusNotFound, "quarantine_not_found", "no quarantine for that track")
writeErr(w, &apierror.Error{Status: 404, Code: "quarantine_not_found", Message: "no quarantine for that track"})
return
}
h.logger.Error("api: unflag", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "unflag failed")
writeErr(w, apierror.InternalMsg("unflag failed", err))
return
}
w.WriteHeader(http.StatusNoContent)
@@ -119,13 +120,13 @@ type quarantineMineView struct {
func (h *handlers) handleListMyQuarantine(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context())
if !ok {
writeErr(w, http.StatusUnauthorized, "unauthorized", "authentication required")
writeErr(w, apierror.ErrUnauthorized)
return
}
rows, err := h.lidarrQuarantine.ListMine(r.Context(), user.ID)
if err != nil {
h.logger.Error("api: list mine", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "list failed")
writeErr(w, apierror.InternalMsg("list failed", err))
return
}
out := make([]quarantineMineView, 0, len(rows))
+12 -11
View File
@@ -12,6 +12,7 @@ import (
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
"git.fabledsword.com/bvandeusen/minstrel/internal/auth"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/recommendation"
@@ -31,24 +32,24 @@ type RadioResponse struct {
func (h *handlers) handleRadio(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context())
if !ok {
writeErr(w, http.StatusUnauthorized, "unauthorized", "authentication required")
writeErr(w, apierror.ErrUnauthorized)
return
}
raw := strings.TrimSpace(r.URL.Query().Get("seed_track"))
if raw == "" {
writeErr(w, http.StatusBadRequest, "bad_request", "seed_track is required")
writeErr(w, apierror.BadRequest("bad_request", "seed_track is required"))
return
}
seedID, ok := parseUUID(raw)
if !ok {
writeErr(w, http.StatusBadRequest, "bad_request", "invalid seed_track id")
writeErr(w, apierror.BadRequest("bad_request", "invalid seed_track id"))
return
}
limit := h.recCfg.RadioSize
if v := r.URL.Query().Get("limit"); v != "" {
n, err := strconv.Atoi(v)
if err != nil || n < 1 {
writeErr(w, http.StatusBadRequest, "bad_request", "invalid limit")
writeErr(w, apierror.BadRequest("bad_request", "invalid limit"))
return
}
limit = n
@@ -60,23 +61,23 @@ func (h *handlers) handleRadio(w http.ResponseWriter, r *http.Request) {
track, err := q.GetTrackByID(r.Context(), seedID)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
writeErr(w, http.StatusNotFound, "not_found", "seed_track not found")
writeErr(w, apierror.NotFound("seed_track"))
return
}
h.logger.Error("api: get radio seed track failed", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "lookup failed")
writeErr(w, apierror.InternalMsg("lookup failed", err))
return
}
album, err := q.GetAlbumByID(r.Context(), track.AlbumID)
if err != nil {
h.logger.Error("api: get radio seed album failed", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "lookup failed")
writeErr(w, apierror.InternalMsg("lookup failed", err))
return
}
artist, err := q.GetArtistByID(r.Context(), track.ArtistID)
if err != nil {
h.logger.Error("api: get radio seed artist failed", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "lookup failed")
writeErr(w, apierror.InternalMsg("lookup failed", err))
return
}
@@ -96,7 +97,7 @@ func (h *handlers) handleRadio(w http.ResponseWriter, r *http.Request) {
)
if err != nil {
h.logger.Error("api: radio: load candidates fallback failed", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "candidate load failed")
writeErr(w, apierror.InternalMsg("candidate load failed", err))
return
}
}
@@ -118,13 +119,13 @@ func (h *handlers) handleRadio(w http.ResponseWriter, r *http.Request) {
al, err := q.GetAlbumByID(r.Context(), p.Track.AlbumID)
if err != nil {
h.logger.Error("api: radio: resolve album", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "resolve failed")
writeErr(w, apierror.InternalMsg("resolve failed", err))
return
}
ar, err := q.GetArtistByID(r.Context(), p.Track.ArtistID)
if err != nil {
h.logger.Error("api: radio: resolve artist", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "resolve failed")
writeErr(w, apierror.InternalMsg("resolve failed", err))
return
}
out = append(out, trackRefFrom(p.Track, al.Title, ar.Name))
+17 -16
View File
@@ -10,6 +10,7 @@ import (
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
"git.fabledsword.com/bvandeusen/minstrel/internal/auth"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/lidarrrequests"
@@ -125,13 +126,13 @@ type createRequestBody struct {
func (h *handlers) handleCreateRequest(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context())
if !ok {
writeErr(w, http.StatusUnauthorized, "unauthorized", "authentication required")
writeErr(w, apierror.ErrUnauthorized)
return
}
var body createRequestBody
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
writeErr(w, http.StatusBadRequest, "bad_request", "invalid JSON body")
writeErr(w, apierror.BadRequest("bad_request", "invalid JSON body"))
return
}
@@ -146,11 +147,11 @@ func (h *handlers) handleCreateRequest(w http.ResponseWriter, r *http.Request) {
})
if err != nil {
if errors.Is(err, lidarrrequests.ErrInvalidKindFields) {
writeErr(w, http.StatusBadRequest, "mbid_required", err.Error())
writeErr(w, apierror.BadRequest("mbid_required", err.Error()))
return
}
h.logger.Error("api: create request", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "create failed")
writeErr(w, apierror.InternalMsg("create failed", err))
return
}
@@ -187,14 +188,14 @@ func (h *handlers) handleCreateRequest(w http.ResponseWriter, r *http.Request) {
func (h *handlers) handleListRequests(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context())
if !ok {
writeErr(w, http.StatusUnauthorized, "unauthorized", "authentication required")
writeErr(w, apierror.ErrUnauthorized)
return
}
rows, err := h.lidarrRequests.ListForUser(r.Context(), user.ID, 100)
if err != nil {
h.logger.Error("api: list requests", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "list failed")
writeErr(w, apierror.InternalMsg("list failed", err))
return
}
@@ -213,30 +214,30 @@ func (h *handlers) handleListRequests(w http.ResponseWriter, r *http.Request) {
func (h *handlers) handleGetRequest(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context())
if !ok {
writeErr(w, http.StatusUnauthorized, "unauthorized", "authentication required")
writeErr(w, apierror.ErrUnauthorized)
return
}
id, ok := parseUUID(chi.URLParam(r, "id"))
if !ok {
writeErr(w, http.StatusBadRequest, "bad_request", "invalid request id")
writeErr(w, apierror.BadRequest("bad_request", "invalid request id"))
return
}
row, err := dbq.New(h.pool).GetLidarrRequestByID(r.Context(), id)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
writeErr(w, http.StatusNotFound, "request_not_found", "request not found")
writeErr(w, apierror.NotFound("request"))
return
}
h.logger.Error("api: get request", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "get failed")
writeErr(w, apierror.InternalMsg("get failed", err))
return
}
// Allow if caller owns the request or is an admin.
if row.UserID != user.ID && !user.IsAdmin {
writeErr(w, http.StatusNotFound, "request_not_found", "request not found")
writeErr(w, apierror.NotFound("request"))
return
}
@@ -250,13 +251,13 @@ func (h *handlers) handleGetRequest(w http.ResponseWriter, r *http.Request) {
func (h *handlers) handleCancelRequest(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context())
if !ok {
writeErr(w, http.StatusUnauthorized, "unauthorized", "authentication required")
writeErr(w, apierror.ErrUnauthorized)
return
}
id, ok := parseUUID(chi.URLParam(r, "id"))
if !ok {
writeErr(w, http.StatusBadRequest, "bad_request", "invalid request id")
writeErr(w, apierror.BadRequest("bad_request", "invalid request id"))
return
}
@@ -264,12 +265,12 @@ func (h *handlers) handleCancelRequest(w http.ResponseWriter, r *http.Request) {
if err != nil {
switch {
case errors.Is(err, lidarrrequests.ErrNotPending):
writeErr(w, http.StatusConflict, "request_not_pending", "request is not pending")
writeErr(w, apierror.Conflict("request_not_pending", "request is not pending"))
case errors.Is(err, lidarrrequests.ErrNotFound):
writeErr(w, http.StatusNotFound, "request_not_found", "request not found")
writeErr(w, apierror.NotFound("request"))
default:
h.logger.Error("api: cancel request", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "cancel failed")
writeErr(w, apierror.InternalMsg("cancel failed", err))
}
return
}
+7 -1
View File
@@ -5,6 +5,8 @@ import (
"errors"
"fmt"
"sync"
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
)
// Provider is the base shape every cover-art source implements.
@@ -112,7 +114,11 @@ var ErrProviderNotFound = errors.New("coverart: provider not registered")
// this MBID — a terminal-this-pass result. The enricher treats this
// as "try the next provider in the chain"; if all providers return
// ErrNotFound, the row settles to cover_art_source='none'.
var ErrNotFound = errors.New("coverart: not found")
//
// Aliased to apierror.ErrNotFound so handler error mapping (errors.Is
// against the shared sentinel) and existing per-package callsites both
// resolve to the same pointer.
var ErrNotFound = apierror.ErrNotFound
// ErrTransient indicates a temporary failure (5xx, network, timeout,
// auth issue). The enricher leaves the row's source NULL so the next
+10 -2
View File
@@ -1,6 +1,10 @@
package lidarr
import "errors"
import (
"errors"
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
)
// Sentinel errors. Callers branch on these via errors.Is, not on
// HTTP status codes — the client maps codes to errors.
@@ -15,5 +19,9 @@ var (
// Lidarr's monitored set. Distinguished from network/auth errors so admin
// handlers can surface it as `lidarr_album_lookup_failed` (502) instead
// of `lidarr_unreachable` (503).
ErrNotFound = errors.New("lidarr: not found")
//
// Aliased to apierror.ErrNotFound so handler error mapping (errors.Is
// against the shared sentinel) and existing per-package callsites both
// resolve to the same pointer.
ErrNotFound = apierror.ErrNotFound
)
+6 -2
View File
@@ -13,6 +13,7 @@ import (
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/lidarr"
"git.fabledsword.com/bvandeusen/minstrel/internal/lidarrconfig"
@@ -22,8 +23,11 @@ import (
var (
ErrInvalidKindFields = errors.New("lidarrrequests: missing required fields for kind")
ErrNotPending = errors.New("lidarrrequests: request is not pending")
ErrNotFound = errors.New("lidarrrequests: request not found")
ErrLidarrDisabled = errors.New("lidarrrequests: lidarr not configured")
// ErrNotFound aliases apierror.ErrNotFound so handlers can errors.Is
// against the shared sentinel; existing lidarrrequests.ErrNotFound
// callsites still resolve to the same pointer.
ErrNotFound = apierror.ErrNotFound
ErrLidarrDisabled = errors.New("lidarrrequests: lidarr not configured")
// ErrDefaultsIncomplete fires when Lidarr is enabled but the operator
// hasn't picked a default quality profile or root folder. Without those,
// Lidarr's add-artist/add-album endpoints reject the payload with a
+6 -1
View File
@@ -24,13 +24,18 @@ import (
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
)
// Typed errors. The API layer maps each to its wire status code; tests
// use errors.Is to assert the right path was taken.
//
// ErrNotFound aliases apierror.ErrNotFound so handlers can errors.Is
// against the shared sentinel; existing playlists.ErrNotFound callsites
// still resolve to the same pointer.
var (
ErrNotFound = errors.New("playlists: playlist not found")
ErrNotFound = apierror.ErrNotFound
ErrForbidden = errors.New("playlists: forbidden")
ErrInvalidInput = errors.New("playlists: invalid input")
ErrTrackNotFound = errors.New("playlists: track not found")
+6 -3
View File
@@ -25,13 +25,16 @@ import (
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
"git.fabledsword.com/bvandeusen/minstrel/internal/coverart"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
)
// ErrNotFound is returned when the track id doesn't resolve. Handler
// maps to 404 `not_found`.
var ErrNotFound = errors.New("tracks: not found")
// ErrNotFound is returned when the track id doesn't resolve. Aliased
// to apierror.ErrNotFound so handlers can errors.Is against the shared
// sentinel; existing tracks.ErrNotFound callsites still resolve to the
// same pointer.
var ErrNotFound = apierror.ErrNotFound
// LidarrUnmonitorer is the subset of *lidarr.Client RemoveTrack uses.
// Defined as an interface so tests can stub without spinning up a Lidarr