From f2e69f68e5b674e0539eac656958f6d841a8fb18 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Wed, 5 May 2021 13:44:19 -0500 Subject: [PATCH] Fix logging of context errors --- src/website/routes.go | 8 +++--- src/website/routes_test.go | 54 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 src/website/routes_test.go diff --git a/src/website/routes.go b/src/website/routes.go index 09ad0d7d..5498914f 100644 --- a/src/website/routes.go +++ b/src/website/routes.go @@ -31,7 +31,7 @@ func NewWebsiteRoutes(conn *pgxpool.Pool, perfCollector *perf.PerfCollector) htt logPerf := TrackRequestPerf(c, perfCollector) defer logPerf() - defer LogContextErrors(c, res) + defer LogContextErrors(c, &res) return h(c) } @@ -46,7 +46,7 @@ func NewWebsiteRoutes(conn *pgxpool.Pool, perfCollector *perf.PerfCollector) htt logPerf := TrackRequestPerf(c, perfCollector) defer logPerf() - defer LogContextErrors(c, res) + defer LogContextErrors(c, &res) ok, errRes := LoadCommonWebsiteData(c) if !ok { @@ -65,7 +65,7 @@ func NewWebsiteRoutes(conn *pgxpool.Pool, perfCollector *perf.PerfCollector) htt logPerf := TrackRequestPerf(c, perfCollector) defer logPerf() - defer LogContextErrors(c, res) + defer LogContextErrors(c, &res) ok, errRes := LoadCommonWebsiteData(c) if !ok { @@ -296,7 +296,7 @@ func TrackRequestPerf(c *RequestContext, perfCollector *perf.PerfCollector) (aft } } -func LogContextErrors(c *RequestContext, res ResponseData) { +func LogContextErrors(c *RequestContext, res *ResponseData) { for _, err := range res.Errors { c.Logger.Error().Err(err).Msg("error occurred during request") } diff --git a/src/website/routes_test.go b/src/website/routes_test.go new file mode 100644 index 00000000..86ea6525 --- /dev/null +++ b/src/website/routes_test.go @@ -0,0 +1,54 @@ +package website + +import ( + "bytes" + "errors" + "net/http" + "net/http/httptest" + "testing" + + "github.com/rs/zerolog" + "github.com/stretchr/testify/assert" +) + +func TestLogContextErrors(t *testing.T) { + err1 := errors.New("test error 1") + err2 := errors.New("test error 2") + + var buf bytes.Buffer + logger := zerolog.New(&buf) + logger.Print("sanity check") + + assert.Contains(t, buf.String(), "sanity check") + + router := &Router{} + routes := RouteBuilder{ + Router: router, + Middleware: func(h Handler) Handler { + return func(c *RequestContext) (res ResponseData) { + c.Logger = &logger + defer LogContextErrors(c, &res) + return h(c) + } + }, + } + + routes.GET("^/test$", func(c *RequestContext) ResponseData { + return ErrorResponse(http.StatusInternalServerError, err1, err2) + }) + + srv := httptest.NewServer(router) + defer srv.Close() + + res, err := http.Get(srv.URL + "/test") + if assert.Nil(t, err) { + defer res.Body.Close() + + t.Logf("Log contents: %s", buf.String()) + + assert.Equal(t, http.StatusInternalServerError, res.StatusCode) + + assert.Contains(t, buf.String(), err1.Error()) + assert.Contains(t, buf.String(), err2.Error()) + } +}