refactor(server/api): migrate library/media handlers to writeErr(err) (T3b)
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"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/recommendation"
|
||||
)
|
||||
@@ -36,14 +37,14 @@ type seedContributionView struct {
|
||||
func (h *handlers) handleListSuggestions(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 := 12
|
||||
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
|
||||
@@ -52,7 +53,7 @@ func (h *handlers) handleListSuggestions(w http.ResponseWriter, r *http.Request)
|
||||
if v := r.URL.Query().Get("half_life_days"); v != "" {
|
||||
f, err := strconv.ParseFloat(v, 64)
|
||||
if err != nil || f <= 0 {
|
||||
writeErr(w, http.StatusBadRequest, "bad_request", "invalid half_life_days")
|
||||
writeErr(w, apierror.BadRequest("bad_request", "invalid half_life_days"))
|
||||
return
|
||||
}
|
||||
halfLife = f
|
||||
@@ -61,7 +62,7 @@ func (h *handlers) handleListSuggestions(w http.ResponseWriter, r *http.Request)
|
||||
suggestions, err := recommendation.SuggestArtists(r.Context(), h.pool, user.ID, halfLife, limit)
|
||||
if err != nil {
|
||||
h.logger.Error("api: list suggestions", "err", err)
|
||||
writeErr(w, http.StatusInternalServerError, "server_error", "failed to load suggestions")
|
||||
writeErr(w, apierror.InternalMsg("failed to load suggestions", err))
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user