refactor(server): final writeErr migration + ErrNotFound aliases (T3c)
This commit is contained in:
+17
-16
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user