Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
admpub committed Jun 27, 2024
1 parent 96ae254 commit 2552a6e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
20 changes: 19 additions & 1 deletion echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

func init() {
mw.DefaultLogWriter = log.Writer(log.LevelInfo)
log.Sync()
}

func request(method, path string, e *Echo, reqRewrite ...func(*http.Request)) (int, string) {
Expand Down Expand Up @@ -78,6 +79,7 @@ func TestEchoMiddleware(t *testing.T) {

func TestEchoMiddlewareError(t *testing.T) {
e := New()
e.SetDebug(true)
e.Use(mw.Log(), func(next HandlerFunc) HandlerFunc {
return func(c Context) error {
return errors.New("error")
Expand All @@ -87,8 +89,24 @@ func TestEchoMiddlewareError(t *testing.T) {

e.RebuildRouter()

c, _ := request(GET, "/", e)
c, r := request(GET, "/", e)
assert.Equal(t, http.StatusInternalServerError, c)
assert.Equal(t, `error`, r)
}

func TestEchoHandlerError(t *testing.T) {
e := New()
e.SetDebug(true)
e.Use(mw.Log())
e.Get("/", func(c Context) error {
return errors.New("error")
})

e.RebuildRouter()

c, r := request(GET, "/", e)
assert.Equal(t, http.StatusInternalServerError, c)
assert.Equal(t, `error`, r)
}

func TestEchoRoutePath(t *testing.T) {
Expand Down
6 changes: 4 additions & 2 deletions middleware/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,13 @@ func LogWithConfig(config LogConfig) echo.MiddlewareFunc {
}
info := AcquireVisitorInfo()
info.Time = time.Now()
err := h.Handle(c)
if err := h.Handle(c); err != nil {
c.Error(err)
}
info.SetFromContext(c)
config.Execute(info)
ReleaseVisitorInfo(info)
return err
return nil
})
}
}

0 comments on commit 2552a6e

Please sign in to comment.