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:
@@ -11,6 +11,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"
|
||||
@@ -107,7 +108,7 @@ type adminDuplicatesResponse struct {
|
||||
func (h *handlers) handleListDuplicates(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
|
||||
}
|
||||
view := r.URL.Query().Get("view")
|
||||
@@ -115,7 +116,7 @@ func (h *handlers) handleListDuplicates(w http.ResponseWriter, r *http.Request)
|
||||
view = duplicateViewReview
|
||||
}
|
||||
if view != duplicateViewReview && view != duplicateViewCrossRelease {
|
||||
writeAdminJSONErr(w, http.StatusBadRequest, "invalid_view")
|
||||
writeErr(w, apierror.BadRequest("invalid_view", "unknown view"))
|
||||
return
|
||||
}
|
||||
ctx := r.Context()
|
||||
@@ -128,20 +129,20 @@ func (h *handlers) handleListDuplicates(w http.ResponseWriter, r *http.Request)
|
||||
sweep = duplicateSweepViewOf(last)
|
||||
case !errors.Is(err, pgx.ErrNoRows):
|
||||
h.logger.Error("admin: latest duplicate sweep", "err", err)
|
||||
writeAdminJSONErr(w, http.StatusInternalServerError, "server_error")
|
||||
writeErr(w, apierror.Internal(err))
|
||||
return
|
||||
}
|
||||
|
||||
cov, err := h.fingerprintCoverage(ctx)
|
||||
if err != nil {
|
||||
h.logger.Error("admin: fingerprint coverage", "err", err)
|
||||
writeAdminJSONErr(w, http.StatusInternalServerError, "server_error")
|
||||
writeErr(w, apierror.Internal(err))
|
||||
return
|
||||
}
|
||||
counts, err := duplicateCounts(ctx, q)
|
||||
if err != nil {
|
||||
h.logger.Error("admin: count duplicate groups", "err", err)
|
||||
writeAdminJSONErr(w, http.StatusInternalServerError, "server_error")
|
||||
writeErr(w, apierror.Internal(err))
|
||||
return
|
||||
}
|
||||
total := counts.Review
|
||||
@@ -153,7 +154,7 @@ func (h *handlers) handleListDuplicates(w http.ResponseWriter, r *http.Request)
|
||||
})
|
||||
if err != nil {
|
||||
h.logger.Error("admin: list duplicate groups", "err", err)
|
||||
writeAdminJSONErr(w, http.StatusInternalServerError, "server_error")
|
||||
writeErr(w, apierror.Internal(err))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -209,14 +210,14 @@ type resolvedActivityResponse struct {
|
||||
func (h *handlers) handleListResolvedDuplicates(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
|
||||
}
|
||||
q := dbq.New(h.pool)
|
||||
total, err := q.CountAuditLogByActions(r.Context(), dbq.CountAuditLogByActionsParams{Actions: resolvedActions, SystemOnly: true})
|
||||
if err != nil {
|
||||
h.logger.Error("admin: count resolved duplicates", "err", err)
|
||||
writeAdminJSONErr(w, http.StatusInternalServerError, "server_error")
|
||||
writeErr(w, apierror.Internal(err))
|
||||
return
|
||||
}
|
||||
rows, err := q.ListAuditLogByActions(r.Context(), dbq.ListAuditLogByActionsParams{
|
||||
@@ -224,7 +225,7 @@ func (h *handlers) handleListResolvedDuplicates(w http.ResponseWriter, r *http.R
|
||||
})
|
||||
if err != nil {
|
||||
h.logger.Error("admin: list resolved duplicates", "err", err)
|
||||
writeAdminJSONErr(w, http.StatusInternalServerError, "server_error")
|
||||
writeErr(w, apierror.Internal(err))
|
||||
return
|
||||
}
|
||||
items := make([]resolvedActivityView, 0, len(rows))
|
||||
@@ -320,11 +321,11 @@ func (h *handlers) handleRunDuplicateSweep(w http.ResponseWriter, _ *http.Reques
|
||||
)
|
||||
if err != nil {
|
||||
h.logger.Error("admin: start duplicate sweep", "err", err)
|
||||
writeAdminJSONErr(w, http.StatusInternalServerError, "server_error")
|
||||
writeErr(w, apierror.Internal(err))
|
||||
return
|
||||
}
|
||||
if !started {
|
||||
writeAdminJSONErr(w, http.StatusConflict, "sweep_in_progress")
|
||||
writeErr(w, apierror.Conflict("sweep_in_progress", "a duplicate sweep is already running"))
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusAccepted, map[string]bool{"started": true})
|
||||
@@ -339,17 +340,17 @@ func (h *handlers) handleRunDuplicateSweep(w http.ResponseWriter, _ *http.Reques
|
||||
func (h *handlers) handleDismissDuplicateGroup(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
|
||||
}
|
||||
n, err := h.dismissDuplicateGroup(r.Context(), id)
|
||||
if err != nil {
|
||||
h.logger.Error("admin: dismiss duplicate group", "err", err)
|
||||
writeAdminJSONErr(w, http.StatusInternalServerError, "server_error")
|
||||
writeErr(w, apierror.Internal(err))
|
||||
return
|
||||
}
|
||||
if n == 0 {
|
||||
writeAdminJSONErr(w, http.StatusNotFound, "duplicate_group_not_pending")
|
||||
writeErr(w, apierror.New(http.StatusNotFound, "duplicate_group_not_pending", "that duplicate group is no longer waiting for a decision"))
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]string{"status": "dismissed"})
|
||||
@@ -407,18 +408,18 @@ func (h *handlers) handleMergeDuplicateGroup(w http.ResponseWriter, r *http.Requ
|
||||
}
|
||||
groupID, ok := parseUUID(chi.URLParam(r, "id"))
|
||||
if !ok {
|
||||
writeAdminJSONErr(w, http.StatusBadRequest, "invalid_id")
|
||||
writeErr(w, apierror.BadRequest("invalid_id", "invalid id"))
|
||||
return
|
||||
}
|
||||
var body mergeDuplicateRequest
|
||||
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, mergeRequestBodyLimit)).Decode(&body); err != nil && !errors.Is(err, io.EOF) {
|
||||
writeAdminJSONErr(w, http.StatusBadRequest, "invalid_body")
|
||||
writeErr(w, apierror.BadRequest("invalid_body", "invalid request body"))
|
||||
return
|
||||
}
|
||||
var survivorID pgtype.UUID // invalid: keep the proposal
|
||||
if body.SurvivorTrackID != "" {
|
||||
if survivorID, ok = parseUUID(body.SurvivorTrackID); !ok {
|
||||
writeAdminJSONErr(w, http.StatusBadRequest, "invalid_id")
|
||||
writeErr(w, apierror.BadRequest("invalid_id", "invalid id"))
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -432,12 +433,12 @@ func (h *handlers) handleMergeDuplicateGroup(w http.ResponseWriter, r *http.Requ
|
||||
}
|
||||
switch {
|
||||
case errors.Is(err, library.ErrDuplicateGroupNotPending):
|
||||
writeAdminJSONErr(w, http.StatusNotFound, "duplicate_group_not_pending")
|
||||
writeErr(w, apierror.New(http.StatusNotFound, "duplicate_group_not_pending", "that duplicate group is no longer waiting for a decision"))
|
||||
case errors.Is(err, library.ErrSurvivorNotInGroup):
|
||||
writeAdminJSONErr(w, http.StatusBadRequest, "survivor_not_in_group")
|
||||
writeErr(w, apierror.BadRequest("survivor_not_in_group", "the copy to keep is not in this group"))
|
||||
default:
|
||||
h.logger.Error("admin: merge duplicate group", "group_id", uuidToString(groupID), "err", err)
|
||||
writeAdminJSONErr(w, http.StatusInternalServerError, "server_error")
|
||||
writeErr(w, apierror.Internal(err))
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -486,18 +487,18 @@ func (h *handlers) handleRemoveWrongSlotFiles(w http.ResponseWriter, r *http.Req
|
||||
}
|
||||
groupID, ok := parseUUID(chi.URLParam(r, "id"))
|
||||
if !ok {
|
||||
writeAdminJSONErr(w, http.StatusBadRequest, "invalid_id")
|
||||
writeErr(w, apierror.BadRequest("invalid_id", "invalid id"))
|
||||
return
|
||||
}
|
||||
var body wrongSlotRequest
|
||||
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, mergeRequestBodyLimit)).Decode(&body); err != nil && !errors.Is(err, io.EOF) {
|
||||
writeAdminJSONErr(w, http.StatusBadRequest, "invalid_body")
|
||||
writeErr(w, apierror.BadRequest("invalid_body", "invalid request body"))
|
||||
return
|
||||
}
|
||||
var keepID pgtype.UUID // invalid: every copy goes
|
||||
if body.KeepTrackID != "" {
|
||||
if keepID, ok = parseUUID(body.KeepTrackID); !ok {
|
||||
writeAdminJSONErr(w, http.StatusBadRequest, "invalid_id")
|
||||
writeErr(w, apierror.BadRequest("invalid_id", "invalid id"))
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -511,14 +512,14 @@ func (h *handlers) handleRemoveWrongSlotFiles(w http.ResponseWriter, r *http.Req
|
||||
}
|
||||
switch {
|
||||
case errors.Is(err, library.ErrDuplicateGroupNotPending):
|
||||
writeAdminJSONErr(w, http.StatusNotFound, "duplicate_group_not_pending")
|
||||
writeErr(w, apierror.New(http.StatusNotFound, "duplicate_group_not_pending", "that duplicate group is no longer waiting for a decision"))
|
||||
case errors.Is(err, library.ErrNotWrongSlot):
|
||||
writeAdminJSONErr(w, http.StatusBadRequest, "not_wrong_slot")
|
||||
writeErr(w, apierror.BadRequest("not_wrong_slot", "this group is not a wrong-slot match"))
|
||||
case errors.Is(err, library.ErrSurvivorNotInGroup):
|
||||
writeAdminJSONErr(w, http.StatusBadRequest, "survivor_not_in_group")
|
||||
writeErr(w, apierror.BadRequest("survivor_not_in_group", "the copy to keep is not in this group"))
|
||||
default:
|
||||
h.logger.Error("admin: remove wrong-slot files", "group_id", uuidToString(groupID), "err", err)
|
||||
writeAdminJSONErr(w, http.StatusInternalServerError, "server_error")
|
||||
writeErr(w, apierror.Internal(err))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user