fix(api): every /api error answers in one envelope, so Android shows the real reason (#8002)

Three error shapes were on the wire: {"error":{"code","message"}} from
writeErr, a flat {"error":"code"} from the admin queues, playback errors
and RequireAdmin, and plain text from RequireUser's 401s. Android reads
only the first, so an admin's quarantine, duplicate or suspect-file
error, and "you're not an admin", all showed "Something went wrong."

apierror.Write is now the one writer, and api.writeErr, RequireAdmin,
RequireUser, the client-APK endpoints, the events stream, the unknown-
route 404 and the admin scan all go through it. The flat writers are
gone, and the web client drops its second branch. The admin codes keep
their names; each now carries a message.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-26 12:43:27 -05:00
co-authored by Claude Opus 5.5
parent 125099ede2
commit 0811775af6
20 changed files with 195 additions and 182 deletions
+14 -13
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/audit"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/library"
@@ -62,7 +63,7 @@ type adminSuspectSourcesResponse struct {
func (h *handlers) handleListSuspectSources(w http.ResponseWriter, r *http.Request) {
limit, offset, err := parsePaging(r.URL.Query())
if err != nil {
writeAdminJSONErr(w, http.StatusBadRequest, "invalid_paging")
writeErr(w, apierror.BadRequest("invalid_paging", "invalid limit or offset"))
return
}
@@ -70,7 +71,7 @@ func (h *handlers) handleListSuspectSources(w http.ResponseWriter, r *http.Reque
total, err := q.CountSuspectSourceTracks(r.Context(), library.SuspectSourcePattern)
if err != nil {
h.logger.Error("admin: count suspect-source tracks", "err", err)
writeAdminJSONErr(w, http.StatusInternalServerError, "server_error")
writeErr(w, apierror.Internal(err))
return
}
rows, err := q.ListSuspectSourceTracks(r.Context(), dbq.ListSuspectSourceTracksParams{
@@ -80,7 +81,7 @@ func (h *handlers) handleListSuspectSources(w http.ResponseWriter, r *http.Reque
})
if err != nil {
h.logger.Error("admin: list suspect-source tracks", "err", err)
writeAdminJSONErr(w, http.StatusInternalServerError, "server_error")
writeErr(w, apierror.Internal(err))
return
}
@@ -139,26 +140,26 @@ type suspectVerdictRequest struct {
func (h *handlers) handleSetSuspectSourceVerdict(w http.ResponseWriter, r *http.Request) {
id, ok := parseUUID(chi.URLParam(r, "id"))
if !ok {
writeAdminJSONErr(w, http.StatusBadRequest, "invalid_id")
writeErr(w, apierror.BadRequest("invalid_id", "invalid id"))
return
}
var req suspectVerdictRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeAdminJSONErr(w, http.StatusBadRequest, "invalid_body")
writeErr(w, apierror.BadRequest("invalid_body", "invalid request body"))
return
}
if req.Verdict != "fine" && req.Verdict != "suspect" {
writeAdminJSONErr(w, http.StatusBadRequest, "invalid_verdict")
writeErr(w, apierror.BadRequest("invalid_verdict", "verdict must be fine or suspect"))
return
}
n, err := dbq.New(h.pool).SetTrackSourceVerdict(r.Context(), dbq.SetTrackSourceVerdictParams{Verdict: req.Verdict, ID: id})
if err != nil {
h.logger.Error("admin: set suspect-source verdict", "err", err)
writeAdminJSONErr(w, http.StatusInternalServerError, "server_error")
writeErr(w, apierror.Internal(err))
return
}
if n == 0 {
writeAdminJSONErr(w, http.StatusNotFound, "suspect_track_not_found")
writeErr(w, apierror.New(http.StatusNotFound, "suspect_track_not_found", "that track is not on the suspect list"))
return
}
writeJSON(w, http.StatusOK, map[string]string{"verdict": req.Verdict})
@@ -180,17 +181,17 @@ func (h *handlers) handleRemoveSuspectSourceFile(w http.ResponseWriter, r *http.
}
id, ok := parseUUID(chi.URLParam(r, "id"))
if !ok {
writeAdminJSONErr(w, http.StatusBadRequest, "invalid_id")
writeErr(w, apierror.BadRequest("invalid_id", "invalid id"))
return
}
track, err := dbq.New(h.pool).GetTrackByID(r.Context(), id)
if errors.Is(err, pgx.ErrNoRows) || (err == nil && (track.MissingSince.Valid || len(library.SourceMarkersFor(track.FilePath)) == 0)) {
writeAdminJSONErr(w, http.StatusNotFound, "suspect_track_not_found")
writeErr(w, apierror.New(http.StatusNotFound, "suspect_track_not_found", "that track is not on the suspect list"))
return
}
if err != nil {
h.logger.Error("admin: read suspect track", "err", err)
writeAdminJSONErr(w, http.StatusInternalServerError, "server_error")
writeErr(w, apierror.Internal(err))
return
}
if _, err := library.RemoveFile(r.Context(), h.pool, h.logger, library.RemoveFileParams{
@@ -202,11 +203,11 @@ func (h *handlers) handleRemoveSuspectSourceFile(w http.ResponseWriter, r *http.
return
}
if errors.Is(err, library.ErrTrackNotFound) {
writeAdminJSONErr(w, http.StatusNotFound, "suspect_track_not_found")
writeErr(w, apierror.New(http.StatusNotFound, "suspect_track_not_found", "that track is not on the suspect list"))
return
}
h.logger.Error("admin: remove suspect file", "err", err)
writeAdminJSONErr(w, http.StatusInternalServerError, "file_delete_failed")
writeErr(w, apierror.New(http.StatusInternalServerError, "file_delete_failed", "the file could not be deleted"))
return
}
audit.WriteOrLog(r.Context(), h.pool, h.logger, admin.ID, pgtype.UUID{}, audit.ActionSuspectFileRemove, map[string]any{