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
+16 -16
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/quarantine"
@@ -48,7 +49,7 @@ func (h *handlers) handleListAdminQuarantine(w http.ResponseWriter, r *http.Requ
rows, err := h.quarantine.ListAdminQueue(r.Context())
if err != nil {
h.logger.Error("admin: list quarantine", "err", err)
writeAdminJSONErr(w, http.StatusInternalServerError, "server_error")
writeErr(w, apierror.Internal(err))
return
}
out := make([]adminQueueRowView, 0, len(rows))
@@ -90,22 +91,22 @@ type actionResultView struct {
func (h *handlers) handleResolveQuarantine(w http.ResponseWriter, r *http.Request) {
admin, ok := auth.UserFromContext(r.Context())
if !ok {
writeAdminJSONErr(w, http.StatusUnauthorized, "unauthorized")
writeErr(w, apierror.ErrUnauthorized)
return
}
id, ok := parseUUID(chi.URLParam(r, "track_id"))
if !ok {
writeAdminJSONErr(w, http.StatusBadRequest, "invalid_id")
writeErr(w, apierror.BadRequest("invalid_id", "invalid id"))
return
}
action, err := h.quarantine.Resolve(r.Context(), id, admin.ID)
if err != nil {
if errors.Is(err, quarantine.ErrTrackNotFound) {
writeAdminJSONErr(w, http.StatusNotFound, "track_not_found")
writeErr(w, apierror.NotFound("track"))
return
}
h.logger.Error("admin: resolve quarantine", "err", err)
writeAdminJSONErr(w, http.StatusInternalServerError, "server_error")
writeErr(w, apierror.Internal(err))
return
}
// Broadcast: affects every user who'd flagged this track.
@@ -121,18 +122,17 @@ func (h *handlers) handleResolveQuarantine(w http.ResponseWriter, r *http.Reques
func (h *handlers) handleDeleteQuarantineFile(w http.ResponseWriter, r *http.Request) {
admin, ok := auth.UserFromContext(r.Context())
if !ok {
writeAdminJSONErr(w, http.StatusUnauthorized, "unauthorized")
writeErr(w, apierror.ErrUnauthorized)
return
}
id, ok := parseUUID(chi.URLParam(r, "track_id"))
if !ok {
writeAdminJSONErr(w, http.StatusBadRequest, "invalid_id")
writeErr(w, apierror.BadRequest("invalid_id", "invalid id"))
return
}
action, err := h.quarantine.DeleteFile(r.Context(), id, admin.ID)
if err != nil {
// Written in the enveloped shape, not writeAdminJSONErr's bare code:
// the message is the part that tells an admin which directory and uid.
// The message is the part that tells an admin which directory and uid.
if apiErr, ok := fileRemoveAPIError(err); ok {
logFileRemoveFailure(h.logger, apiErr, "track_id", uuidToString(id))
writeErr(w, apiErr)
@@ -140,10 +140,10 @@ func (h *handlers) handleDeleteQuarantineFile(w http.ResponseWriter, r *http.Req
}
switch {
case errors.Is(err, quarantine.ErrTrackNotFound):
writeAdminJSONErr(w, http.StatusNotFound, "track_not_found")
writeErr(w, apierror.NotFound("track"))
default:
h.logger.Error("admin: delete 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
}
@@ -166,12 +166,12 @@ func (h *handlers) handleDeleteQuarantineFile(w http.ResponseWriter, r *http.Req
func (h *handlers) handleRemoveQuarantineFile(w http.ResponseWriter, r *http.Request) {
admin, ok := auth.UserFromContext(r.Context())
if !ok {
writeAdminJSONErr(w, http.StatusUnauthorized, "unauthorized")
writeErr(w, apierror.ErrUnauthorized)
return
}
id, ok := parseUUID(chi.URLParam(r, "track_id"))
if !ok {
writeAdminJSONErr(w, http.StatusBadRequest, "invalid_id")
writeErr(w, apierror.BadRequest("invalid_id", "invalid id"))
return
}
action, err := h.quarantine.RemoveFile(r.Context(), id, admin.ID)
@@ -183,10 +183,10 @@ func (h *handlers) handleRemoveQuarantineFile(w http.ResponseWriter, r *http.Req
}
switch {
case errors.Is(err, quarantine.ErrTrackNotFound):
writeAdminJSONErr(w, http.StatusNotFound, "track_not_found")
writeErr(w, apierror.NotFound("track"))
default:
h.logger.Error("admin: remove reported 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
}
@@ -229,7 +229,7 @@ func (h *handlers) handleListQuarantineActions(w http.ResponseWriter, r *http.Re
rows, err := dbq.New(h.pool).ListQuarantineActions(r.Context(), limit)
if err != nil {
h.logger.Error("admin: list actions", "err", err)
writeAdminJSONErr(w, http.StatusInternalServerError, "server_error")
writeErr(w, apierror.Internal(err))
return
}
out := make([]actionLogView, 0, len(rows))