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:
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/library"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/tracks"
|
||||
@@ -77,7 +78,7 @@ type adminMissingResponse struct {
|
||||
func (h *handlers) handleListMissingTracks(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
|
||||
}
|
||||
|
||||
@@ -85,7 +86,7 @@ func (h *handlers) handleListMissingTracks(w http.ResponseWriter, r *http.Reques
|
||||
total, err := q.CountMissingTracks(r.Context())
|
||||
if err != nil {
|
||||
h.logger.Error("admin: count missing tracks", "err", err)
|
||||
writeAdminJSONErr(w, http.StatusInternalServerError, "server_error")
|
||||
writeErr(w, apierror.Internal(err))
|
||||
return
|
||||
}
|
||||
rows, err := q.ListMissingTracks(r.Context(), dbq.ListMissingTracksParams{
|
||||
@@ -94,7 +95,7 @@ func (h *handlers) handleListMissingTracks(w http.ResponseWriter, r *http.Reques
|
||||
})
|
||||
if err != nil {
|
||||
h.logger.Error("admin: list missing tracks", "err", err)
|
||||
writeAdminJSONErr(w, http.StatusInternalServerError, "server_error")
|
||||
writeErr(w, apierror.Internal(err))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -185,14 +186,14 @@ func (h *handlers) handleForgetMissingTracks(w http.ResponseWriter, r *http.Requ
|
||||
}
|
||||
var body forgetMissingRequest
|
||||
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, forgetMissingRequestLimit)).Decode(&body); err != nil || len(body.TrackIDs) == 0 {
|
||||
writeAdminJSONErr(w, http.StatusBadRequest, "invalid_body")
|
||||
writeErr(w, apierror.BadRequest("invalid_body", "invalid request body"))
|
||||
return
|
||||
}
|
||||
ids := make([]pgtype.UUID, 0, len(body.TrackIDs))
|
||||
for _, raw := range body.TrackIDs {
|
||||
id, ok := parseUUID(raw)
|
||||
if !ok {
|
||||
writeAdminJSONErr(w, http.StatusBadRequest, "invalid_id")
|
||||
writeErr(w, apierror.BadRequest("invalid_id", "invalid id"))
|
||||
return
|
||||
}
|
||||
ids = append(ids, id)
|
||||
@@ -200,14 +201,14 @@ func (h *handlers) handleForgetMissingTracks(w http.ResponseWriter, r *http.Requ
|
||||
gone, err := h.tracks.ForgetTracks(r.Context(), ids, admin.ID)
|
||||
switch {
|
||||
case errors.Is(err, tracks.ErrNotFound):
|
||||
writeAdminJSONErr(w, http.StatusNotFound, "track_not_found")
|
||||
writeErr(w, apierror.NotFound("track"))
|
||||
return
|
||||
case errors.Is(err, library.ErrTrackPresent):
|
||||
writeAdminJSONErr(w, http.StatusConflict, "track_has_file")
|
||||
writeErr(w, apierror.Conflict("track_has_file", "the track's file is still on disk"))
|
||||
return
|
||||
case err != nil:
|
||||
h.logger.Error("admin: forget missing tracks", "err", err)
|
||||
writeAdminJSONErr(w, http.StatusInternalServerError, "server_error")
|
||||
writeErr(w, apierror.Internal(err))
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]int{"forgotten": len(gone)})
|
||||
@@ -230,13 +231,13 @@ func (h *handlers) handleExportMissingTracks(w http.ResponseWriter, r *http.Requ
|
||||
total, err := q.CountMissingTracks(r.Context())
|
||||
if err != nil {
|
||||
h.logger.Error("admin: count missing tracks", "err", err)
|
||||
writeAdminJSONErr(w, http.StatusInternalServerError, "server_error")
|
||||
writeErr(w, apierror.Internal(err))
|
||||
return
|
||||
}
|
||||
rows, err := q.ListMissingTracks(r.Context(), dbq.ListMissingTracksParams{PageLimit: int32(total), PageOffset: 0})
|
||||
if err != nil {
|
||||
h.logger.Error("admin: list missing tracks", "err", err)
|
||||
writeAdminJSONErr(w, http.StatusInternalServerError, "server_error")
|
||||
writeErr(w, apierror.Internal(err))
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/csv; charset=utf-8")
|
||||
|
||||
Reference in New Issue
Block a user