Skip to content

Commit

Permalink
Added HandleError flag to the middleware config to enable/disable err…
Browse files Browse the repository at this point in the history
…or propagation (#22)
  • Loading branch information
ziflex authored Feb 17, 2023
1 parent 967237f commit 319f16c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,17 @@ e.Use(lecho.Middleware(lecho.Config{
// Output: {"level":"info","user_id":"123", ...}
```

### Errors
Since lecho v3.4.0, the middleware does not automatically propagate errors up the chain.
If you want to do that, you can set `HandleError` to ``true``.

```go
e.Use(lecho.Middleware(lecho.Config{
Logger: logger,
HandleError: true,
}))
```

## Helpers

### Level converters
Expand Down
9 changes: 8 additions & 1 deletion middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type (
RequestIDKey string
// NestKey is the key name to use for the nested logger in a log record.
NestKey string
// HandleError indicates whether to propagate errors up the middleware chain, so the global error handler can decide appropriate status code.
HandleError bool
}

// Enricher is a function that can be used to enrich the logger with additional information.
Expand Down Expand Up @@ -117,7 +119,12 @@ func Middleware(config Config) echo.MiddlewareFunc {
config.BeforeNext(c)
}

err = next(c)
if err = next(c); err != nil {
if config.HandleError {
c.Error(err)
}
}

stop := time.Now()

var mainEvt *zerolog.Event
Expand Down
30 changes: 29 additions & 1 deletion middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func TestMiddleware(t *testing.T) {
t.Run("should not trigger error handler", func(t *testing.T) {
t.Run("should not trigger error handler when HandleError is false", func(t *testing.T) {
var called bool
e := echo.New()
e.HTTPErrorHandler = func(err error, c echo.Context) {
Expand All @@ -40,6 +40,34 @@ func TestMiddleware(t *testing.T) {
assert.False(t, called, "should not call error handler")
})

t.Run("should trigger error handler when HandleError is true", func(t *testing.T) {
var called bool
e := echo.New()
e.HTTPErrorHandler = func(err error, c echo.Context) {
called = true

c.JSON(http.StatusInternalServerError, err.Error())
}
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)

m := lecho.Middleware(lecho.Config{
HandleError: true,
})

next := func(c echo.Context) error {
return errors.New("error")
}

handler := m(next)
err := handler(c)

assert.Error(t, err, "should return error")
assert.Truef(t, called, "should call error handler")
})

t.Run("should use enricher", func(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
Expand Down

0 comments on commit 319f16c

Please sign in to comment.