From 03fe35a62946a0459557883d1ba1baaf47dfc35b Mon Sep 17 00:00:00 2001 From: Arewa Olakunle Date: Fri, 14 Aug 2020 02:53:57 +0100 Subject: [PATCH] fix: error logs are being printed twice (#34) --- middleware/errors.go | 17 ++++++++-------- responses.go | 48 ++++++++++++++++++++++++++------------------ 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/middleware/errors.go b/middleware/errors.go index f9265e8..703e57a 100644 --- a/middleware/errors.go +++ b/middleware/errors.go @@ -33,17 +33,16 @@ func RecovererWithHandler(env string, catch Catch) func(http.Handler) http.Handl fn := func(w http.ResponseWriter, r *http.Request) { defer func() { if rvr := recover(); rvr != nil && rvr != http.ErrAbortHandler { - - log := zerolog.Ctx(r.Context()) - if log != nil { - err := rvr.(error) // kill yourself - log.Err(err).Msg("") - } else { - fmt.Fprintf(os.Stderr, "Panic: %+v\n", rvr) - } - // only do this if catch could not handle it. if !catch(w, r, rvr) { + log := zerolog.Ctx(r.Context()) + if log == nil { + err := rvr.(error) // kill yourself + log.Err(err).Msg("") + } else { + fmt.Fprintf(os.Stderr, "Panic: %+v\n", rvr) + } + if env == "dev" || env == "test" { debug.PrintStack() } diff --git a/responses.go b/responses.go index 586be04..9d2bc50 100644 --- a/responses.go +++ b/responses.go @@ -6,12 +6,36 @@ import ( "net/http" "github.com/rs/zerolog" + "github.com/rs/zerolog/log" ) -// SendJSON response writes a JSON encoded version of `v` to the writer, making -// sure what deserves to be logged gets logged -func SendJSON(r *http.Request, w http.ResponseWriter, code int, v interface{}) { +// SendSuccess sends a JSON success message with status code 200 +func SendSuccess(r *http.Request, w http.ResponseWriter, v interface{}) { + raw := getJSON(r, v) + + log.Info().Msg("") + + w.Header().Set("Content-Type", "application/json; charset=utf-8") + w.Header().Set("X-Content-Type-Options", "nosniff") + w.WriteHeader(http.StatusOK) + _, _ = w.Write(raw) +} + +// SendError sends a JSON error message +func SendError(r *http.Request, w http.ResponseWriter, err APIError) { + raw := getJSON(r, err) + + log.Err(err).Msg("") + + w.Header().Set("Content-Type", "application/json; charset=utf-8") + w.Header().Set("X-Content-Type-Options", "nosniff") + w.WriteHeader(http.StatusOK) + _, _ = w.Write(raw) +} + +func getJSON(r *http.Request, v interface{}) []byte { raw, _ := json.Marshal(v) + log := zerolog.Ctx(r.Context()) // log API responses @@ -27,21 +51,5 @@ func SendJSON(r *http.Request, w http.ResponseWriter, code int, v interface{}) { }) } - // Send the actual logs - log.Info().Msg("") - - w.Header().Set("Content-Type", "application/json; charset=utf-8") - w.Header().Set("X-Content-Type-Options", "nosniff") - w.WriteHeader(code) - _, _ = w.Write(raw) -} - -// SendSuccess sends a JSON success message with status code 200 -func SendSuccess(r *http.Request, w http.ResponseWriter, v interface{}) { - SendJSON(r, w, 200, v) -} - -// SendError sends a JSON error message -func SendError(r *http.Request, w http.ResponseWriter, err APIError) { - SendJSON(r, w, err.Code, err) + return raw }