Skip to content

Commit 0b8a393

Browse files
authored
separate onError to OnNextError and OnExtractionError (#6)
1 parent bdd9b1e commit 0b8a393

3 files changed

Lines changed: 20 additions & 20 deletions

File tree

.github/workflows/checks.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ jobs:
4141
4242
- name: Run gosec
4343
run: |
44-
# 2026-02-16: gosec is broken for multi-module repos, so we use v2.23.0 instead of the latest version.
45-
# go install github.com/securego/gosec/v2/cmd/gosec@latest
46-
go install github.com/securego/gosec/v2/cmd/gosec@v2.23.0
44+
go install github.com/securego/gosec/v2/cmd/gosec@master
4745
gosec -exclude-dir=.cache ./...
4846
4947
- name: Run govulncheck

otel.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@ type Config struct {
4949
// Skipper defines a function to skip middleware.
5050
Skipper middleware.Skipper
5151

52-
// OnErrorFn is used to specify how errors are handled in the middleware.
53-
OnError OnErrorFunc
52+
// OnNextError is used to specify how errors returned from the next middleware / handler are handled.
53+
OnNextError OnErrorFunc
54+
55+
// OnExtractionError is used to specify how errors returned from request extraction are handled.
56+
OnExtractionError OnErrorFunc
5457

5558
// TracerProvider allows overriding the default tracer provider.
5659
TracerProvider oteltrace.TracerProvider
@@ -119,11 +122,6 @@ func (config Config) ToMiddleware() (echo.MiddlewareFunc, error) {
119122
if config.Skipper == nil {
120123
config.Skipper = middleware.DefaultSkipper
121124
}
122-
if config.OnError == nil {
123-
config.OnError = func(c *echo.Context, err error) {
124-
c.Logger().Error("otel middleware error", "error", err)
125-
}
126-
}
127125

128126
var serverHost string
129127
var serverPort int
@@ -175,7 +173,9 @@ func (config Config) ToMiddleware() (echo.MiddlewareFunc, error) {
175173
ClientAddress: c.RealIP(),
176174
}
177175
if err := ev.ExtractRequest(request); err != nil {
178-
config.OnError(c, err)
176+
if config.OnExtractionError != nil {
177+
config.OnExtractionError(c, err)
178+
}
179179
}
180180
spanAttributes := ev.SpanStartAttributes()
181181
if config.SpanStartAttributes != nil {
@@ -217,7 +217,9 @@ func (config Config) ToMiddleware() (echo.MiddlewareFunc, error) {
217217
if err != nil {
218218
span.SetAttributes(semconv.ErrorType(err))
219219
span.SetStatus(codes.Error, err.Error())
220-
config.OnError(c, err)
220+
if config.OnNextError != nil {
221+
config.OnNextError(c, err)
222+
}
221223
}
222224

223225
resp, status := echo.ResolveResponseStatus(c.Response(), err)

otel_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -446,21 +446,21 @@ func TestNewMiddlewareWithConfig_Metric(t *testing.T) {
446446
}, sm.Metrics[0], metricdatatest.IgnoreTimestamp(), metricdatatest.IgnoreValue(), metricdatatest.IgnoreExemplars())
447447
}
448448

449-
func TestWithOnError(t *testing.T) {
449+
func TestConfig_OnNextError(t *testing.T) {
450450
tests := []struct {
451451
name string
452-
GivenOnErrorFunc OnErrorFunc
452+
givenOnNextError OnErrorFunc
453453
wantHandlerCalled int
454454
}{
455455
{
456-
name: "without WithOnError option (default)",
457-
GivenOnErrorFunc: nil,
456+
name: "without OnNextError option (default)",
457+
givenOnNextError: nil,
458458
wantHandlerCalled: 1,
459459
},
460460
{
461-
name: "custom onError logging only",
462-
GivenOnErrorFunc: func(_ *echo.Context, err error) {
463-
t.Logf("Inside custom OnError: %v", err)
461+
name: "custom OnNextError logging only",
462+
givenOnNextError: func(_ *echo.Context, err error) {
463+
t.Logf("Inside custom OnNextError: %v", err)
464464
},
465465
wantHandlerCalled: 1,
466466
},
@@ -472,7 +472,7 @@ func TestWithOnError(t *testing.T) {
472472
w := httptest.NewRecorder()
473473

474474
router := echo.New()
475-
router.Use(NewMiddlewareWithConfig(Config{ServerName: "foobar", OnError: tt.GivenOnErrorFunc}))
475+
router.Use(NewMiddlewareWithConfig(Config{ServerName: "foobar", OnNextError: tt.givenOnNextError}))
476476

477477
router.GET("/ping", func(_ *echo.Context) error {
478478
return assert.AnError

0 commit comments

Comments
 (0)