Skip to content

Commit

Permalink
fix: error logs are being printed twice (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
noxecane authored Aug 14, 2020
1 parent 60bcaff commit 03fe35a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
17 changes: 8 additions & 9 deletions middleware/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
48 changes: 28 additions & 20 deletions responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

0 comments on commit 03fe35a

Please sign in to comment.