From d0a920b8d919c28aca43a33880b9d8ab78ddc3f5 Mon Sep 17 00:00:00 2001 From: Eli Ribble Date: Wed, 11 Mar 2026 23:52:44 +0000 Subject: [PATCH] Log errors on POST, send JSON bodies back --- api/handler.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/api/handler.go b/api/handler.go index fcbfbc5e..31544b3f 100644 --- a/api/handler.go +++ b/api/handler.go @@ -55,7 +55,7 @@ func authenticatedHandlerJSON[T any](f handlerFunctionGet[T]) http.Handler { w.Header().Set("Content-Type", "application/json") //log.Info().Str("template", template).Err(e).Msg("handler done") if e != nil { - log.Warn().Int("status", e.Status).Err(e).Str("user message", e.Message).Msg("Responding with an error from sync pages") + log.Warn().Int("status", e.Status).Err(e).Str("user message", e.Message).Msg("Responding with an error from api") body, err = json.Marshal(ErrorAPI{Message: e.Error()}) if err != nil { log.Error().Err(err).Msg("failed to marshal error") @@ -93,7 +93,14 @@ func authenticatedHandlerJSONPost[ReqType any, ResponseType any](f handlerFuncti ctx := r.Context() response, e := f(ctx, r, org, u, req) if e != nil { - http.Error(w, e.Error(), e.Status) + log.Warn().Int("status", e.Status).Err(e).Str("user message", e.Message).Msg("Responding with an error from api") + body, err = json.Marshal(ErrorAPI{Message: e.Error()}) + if err != nil { + log.Error().Err(err).Msg("failed to marshal error") + http.Error(w, "{\"message\": \"boom. I can't even tell you what went wrong\"}", http.StatusInternalServerError) + return + } + http.Error(w, string(body), e.Status) return } resp_body, err := json.Marshal(response)