Merge branch 'master' of gitssh.handmade.network:hmn/hmn
This commit is contained in:
commit
e8d1859d0a
|
@ -31,10 +31,9 @@ func NewWebsiteRoutes(conn *pgxpool.Pool, perfCollector *perf.PerfCollector) htt
|
||||||
logPerf := TrackRequestPerf(c, perfCollector)
|
logPerf := TrackRequestPerf(c, perfCollector)
|
||||||
defer logPerf()
|
defer logPerf()
|
||||||
|
|
||||||
res = h(c)
|
defer LogContextErrors(c, &res)
|
||||||
|
|
||||||
LogContextErrors(c, res)
|
return h(c)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -47,15 +46,14 @@ func NewWebsiteRoutes(conn *pgxpool.Pool, perfCollector *perf.PerfCollector) htt
|
||||||
logPerf := TrackRequestPerf(c, perfCollector)
|
logPerf := TrackRequestPerf(c, perfCollector)
|
||||||
defer logPerf()
|
defer logPerf()
|
||||||
|
|
||||||
|
defer LogContextErrors(c, &res)
|
||||||
|
|
||||||
ok, errRes := LoadCommonWebsiteData(c)
|
ok, errRes := LoadCommonWebsiteData(c)
|
||||||
if !ok {
|
if !ok {
|
||||||
return errRes
|
return errRes
|
||||||
}
|
}
|
||||||
|
|
||||||
res = h(c)
|
return h(c)
|
||||||
|
|
||||||
LogContextErrors(c, res)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,20 +65,18 @@ func NewWebsiteRoutes(conn *pgxpool.Pool, perfCollector *perf.PerfCollector) htt
|
||||||
logPerf := TrackRequestPerf(c, perfCollector)
|
logPerf := TrackRequestPerf(c, perfCollector)
|
||||||
defer logPerf()
|
defer logPerf()
|
||||||
|
|
||||||
|
defer LogContextErrors(c, &res)
|
||||||
|
|
||||||
ok, errRes := LoadCommonWebsiteData(c)
|
ok, errRes := LoadCommonWebsiteData(c)
|
||||||
if !ok {
|
if !ok {
|
||||||
return errRes
|
return errRes
|
||||||
}
|
}
|
||||||
|
|
||||||
if !c.CurrentProject.IsHMN() {
|
if !c.CurrentProject.IsHMN() {
|
||||||
res = c.Redirect(hmnurl.Url(c.URL().String(), nil), http.StatusMovedPermanently)
|
return c.Redirect(hmnurl.Url(c.URL().String(), nil), http.StatusMovedPermanently)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
res = h(c)
|
return h(c)
|
||||||
|
|
||||||
LogContextErrors(c, res)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -300,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 {
|
for _, err := range res.Errors {
|
||||||
c.Logger.Error().Err(err).Msg("error occurred during request")
|
c.Logger.Error().Err(err).Msg("error occurred during request")
|
||||||
}
|
}
|
||||||
|
|
|
@ -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())
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue