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:
@@ -25,11 +25,11 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/auth"
|
||||
syncpkg "git.fabledsword.com/bvandeusen/minstrel/internal/sync"
|
||||
)
|
||||
@@ -160,6 +160,10 @@ type clientVersionResponse struct {
|
||||
SHA256 string `json:"sha256"`
|
||||
}
|
||||
|
||||
// errNoClientAPK answers both client endpoints when this image carries no
|
||||
// Android app.
|
||||
var errNoClientAPK = apierror.New(http.StatusNotFound, "no_client_apk", "no bundled client apk")
|
||||
|
||||
// handleClientVersion returns the bundled Android client version + a
|
||||
// URL to fetch the APK. 404 when no APK is bundled.
|
||||
func (h *handlers) handleClientVersion(w http.ResponseWriter, _ *http.Request) {
|
||||
@@ -169,7 +173,7 @@ func (h *handlers) handleClientVersion(w http.ResponseWriter, _ *http.Request) {
|
||||
|
||||
stat, err := os.Stat(apkPath)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
http.Error(w, `{"error":{"code":"no_client_apk","message":"no bundled client apk"}}`, http.StatusNotFound)
|
||||
writeErr(w, errNoClientAPK)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
@@ -179,7 +183,7 @@ func (h *handlers) handleClientVersion(w http.ResponseWriter, _ *http.Request) {
|
||||
|
||||
versionBytes, err := os.ReadFile(versionPath)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
http.Error(w, `{"error":{"code":"no_client_version","message":"apk present but version file missing"}}`, http.StatusNotFound)
|
||||
writeErr(w, apierror.New(http.StatusNotFound, "no_client_version", "apk present but version file missing"))
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
@@ -198,7 +202,7 @@ func (h *handlers) handleClientVersion(w http.ResponseWriter, _ *http.Request) {
|
||||
return
|
||||
}
|
||||
if sidecar.Name == "" {
|
||||
http.Error(w, `{"error":{"code":"bad_client_version","message":"version sidecar has no name"}}`, http.StatusInternalServerError)
|
||||
writeErr(w, apierror.New(http.StatusInternalServerError, "bad_client_version", "version sidecar has no name"))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -224,20 +228,19 @@ func (h *handlers) handleClientVersion(w http.ResponseWriter, _ *http.Request) {
|
||||
func (h *handlers) handleClientAPK(w http.ResponseWriter, r *http.Request) {
|
||||
user, ok := auth.UserFromContext(r.Context())
|
||||
if !ok {
|
||||
http.Error(w, `{"error":{"code":"unauthenticated","message":"login required"}}`, http.StatusUnauthorized)
|
||||
writeErr(w, apierror.Unauthorized("unauthenticated", "sign in to continue"))
|
||||
return
|
||||
}
|
||||
userID := syncpkg.FormatUUID(user.ID)
|
||||
if wait := clientAPKAllowDownload(userID, time.Now()); wait > 0 {
|
||||
w.Header().Set("Retry-After", strconv.Itoa(int(wait.Seconds())+1))
|
||||
http.Error(w, `{"error":{"code":"rate_limited","message":"too many downloads; try again shortly"}}`, http.StatusTooManyRequests)
|
||||
writeRateLimited(w, wait)
|
||||
return
|
||||
}
|
||||
|
||||
apkPath := filepath.Join(clientAPKDir(), clientAPKFilename)
|
||||
f, err := os.Open(apkPath)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
http.Error(w, `{"error":{"code":"no_client_apk","message":"no bundled client apk"}}`, http.StatusNotFound)
|
||||
writeErr(w, errNoClientAPK)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user