refactor(server/api): migrate auth/me handlers to writeErr(err) (T3a)

This commit is contained in:
2026-05-07 20:25:26 -04:00
parent 58766dbebe
commit e382b0d718
9 changed files with 66 additions and 56 deletions
+8 -7
View File
@@ -10,6 +10,7 @@ import (
"github.com/jackc/pgerrcode"
"github.com/jackc/pgx/v5/pgconn"
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
"git.fabledsword.com/bvandeusen/minstrel/internal/auth"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
)
@@ -40,12 +41,12 @@ type meProfileResp struct {
func (h *handlers) handleUpdateMyProfile(w http.ResponseWriter, r *http.Request) {
user, ok := auth.UserFromContext(r.Context())
if !ok {
writeErr(w, http.StatusUnauthorized, "auth_required", "")
writeErr(w, apierror.Unauthorized("auth_required", ""))
return
}
var req updateProfileReq
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeErr(w, http.StatusBadRequest, "invalid_body", "")
writeErr(w, apierror.BadRequest("invalid_body", ""))
return
}
@@ -65,7 +66,7 @@ func (h *handlers) handleUpdateMyProfile(w http.ResponseWriter, r *http.Request)
email = nil
} else {
if !emailRe.MatchString(s) {
writeErr(w, http.StatusBadRequest, "email_invalid", "")
writeErr(w, apierror.BadRequest("email_invalid", ""))
return
}
email = &s
@@ -78,7 +79,7 @@ func (h *handlers) handleUpdateMyProfile(w http.ResponseWriter, r *http.Request)
current, err := q.GetUserByID(r.Context(), user.ID)
if err != nil {
h.logger.Error("update profile: lookup failed", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "")
writeErr(w, apierror.Internal(err))
return
}
writeJSON(w, http.StatusOK, profileViewFromUser(current))
@@ -90,7 +91,7 @@ func (h *handlers) handleUpdateMyProfile(w http.ResponseWriter, r *http.Request)
current, err := q.GetUserByID(r.Context(), user.ID)
if err != nil {
h.logger.Error("update profile: lookup failed", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "")
writeErr(w, apierror.Internal(err))
return
}
if req.DisplayName == nil {
@@ -108,11 +109,11 @@ func (h *handlers) handleUpdateMyProfile(w http.ResponseWriter, r *http.Request)
if err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) && pgErr.Code == pgerrcode.UniqueViolation {
writeErr(w, http.StatusConflict, "email_taken", "")
writeErr(w, apierror.Conflict("email_taken", ""))
return
}
h.logger.Error("update profile: update failed", "err", err)
writeErr(w, http.StatusInternalServerError, "server_error", "")
writeErr(w, apierror.Internal(err))
return
}