File tree 2 files changed +31
-0
lines changed
2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ package middleware
4
4
5
5
import (
6
6
"context"
7
+ "fmt"
7
8
"github.com/labstack/echo/v4"
8
9
"time"
9
10
)
@@ -62,6 +63,17 @@ func TimeoutWithConfig(config TimeoutConfig) echo.MiddlewareFunc {
62
63
63
64
done := make (chan error , 1 )
64
65
go func () {
66
+ defer func () {
67
+ if r := recover (); r != nil {
68
+ err , ok := r .(error )
69
+ if ! ok {
70
+ err = fmt .Errorf ("panic recovered in timeout middleware: %v" , r )
71
+ }
72
+ c .Logger ().Error (err )
73
+ done <- err
74
+ }
75
+ }()
76
+
65
77
// This goroutine will keep running even if this middleware times out and
66
78
// will be stopped when ctx.Done() is called down the next(c) call chain
67
79
done <- next (c )
Original file line number Diff line number Diff line change @@ -175,3 +175,22 @@ func TestTimeoutTestRequestClone(t *testing.T) {
175
175
assert .NoError (t , err )
176
176
177
177
}
178
+
179
+ func TestTimeoutRecoversPanic (t * testing.T ) {
180
+ t .Parallel ()
181
+ m := TimeoutWithConfig (TimeoutConfig {
182
+ Timeout : 25 * time .Millisecond ,
183
+ })
184
+
185
+ req := httptest .NewRequest (http .MethodGet , "/" , nil )
186
+ rec := httptest .NewRecorder ()
187
+
188
+ e := echo .New ()
189
+ c := e .NewContext (req , rec )
190
+
191
+ err := m (func (c echo.Context ) error {
192
+ panic ("panic in handler" )
193
+ })(c )
194
+
195
+ assert .Error (t , err , "panic recovered in timeout middleware: panic in handler" )
196
+ }
You can’t perform that action at this time.
0 commit comments