refactor(server/api): writeErr accepts error; migrate admin handlers (1/3)
Rewrite writeErr(w, err) to wrap *apierror.Error via apierror.From,
preserving the existing {"error": {"code", "message"}} wire envelope.
Add writeErrWithLog helper for 500-class errors that need an operator
log line. Migrate all 13 admin_*.go handler files (~76 call sites) to
the new signature; T3 will sweep the remaining api package.
The old 4-arg writeErr is removed, so non-admin call sites in
internal/api will not compile until T3 lands. This is by design — T2
and T3 are paired.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/coverart"
|
||||
)
|
||||
|
||||
@@ -75,7 +76,7 @@ func (h *handlers) handleUpdateCoverSource(w http.ResponseWriter, r *http.Reques
|
||||
|
||||
var body updateCoverSourceReq
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
writeErr(w, http.StatusBadRequest, "bad_body", "invalid JSON")
|
||||
writeErr(w, apierror.BadRequest("bad_body", "invalid JSON"))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -85,11 +86,11 @@ func (h *handlers) handleUpdateCoverSource(w http.ResponseWriter, r *http.Reques
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, coverart.ErrProviderNotFound) {
|
||||
writeErr(w, http.StatusNotFound, "not_found", "no such provider")
|
||||
writeErr(w, &apierror.Error{Status: http.StatusNotFound, Code: "not_found", Message: "no such provider"})
|
||||
return
|
||||
}
|
||||
h.logger.Error("admin: update cover source", "id", id, "err", err)
|
||||
writeErr(w, http.StatusInternalServerError, "server_error", "update failed")
|
||||
writeErr(w, apierror.Internal(err))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -141,7 +142,7 @@ func (h *handlers) handleTestCoverSource(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
|
||||
if errors.Is(err, coverart.ErrProviderNotFound) {
|
||||
writeErr(w, http.StatusNotFound, "not_found", "no such provider")
|
||||
writeErr(w, &apierror.Error{Status: http.StatusNotFound, Code: "not_found", Message: "no such provider"})
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user