@@ -22,32 +22,26 @@ func New(h fiber.Handler, timeout time.Duration, tErrs ...error) fiber.Handler {
2222 timeoutContext , cancel := context .WithTimeout (ctx .Context (), timeout )
2323 defer cancel ()
2424
25- // Attach the timeout-bound context to the current Fiber context.
25+ // Replace the default Fiber context with our timeout-bound context.
2626 ctx .SetContext (timeoutContext )
2727
28- // Execute the wrapped handler synchronously .
29- err := h (ctx )
28+ // Run the handler and check for relevant errors .
29+ err := runHandler (ctx , h , tErrs )
3030
31- // If the context has timed out, return a request timeout error.
32- if timeoutContext . Err () != nil && errors .Is (timeoutContext .Err (), context .DeadlineExceeded ) {
31+ // If the context actually timed out, return a timeout error.
32+ if errors .Is (timeoutContext .Err (), context .DeadlineExceeded ) {
3333 return fiber .ErrRequestTimeout
3434 }
35-
36- // If the handler returned an error, check whether it's a deadline exceeded
37- // error or any other listed timeout-triggering error.
38- if err != nil {
39- if errors .Is (err , context .DeadlineExceeded ) || isCustomError (err , tErrs ) {
40- return fiber .ErrRequestTimeout
41- }
42- }
4335 return err
4436 }
4537}
4638
4739// runHandler executes the handler and returns fiber.ErrRequestTimeout if it
4840// sees a deadline exceeded error or one of the custom "timeout-like" errors.
4941func runHandler (c fiber.Ctx , h fiber.Handler , tErrs []error ) error {
42+ // Execute the wrapped handler synchronously.
5043 err := h (c )
44+ // If the context has timed out, return a request timeout error.
5145 if err != nil && (errors .Is (err , context .DeadlineExceeded ) || isCustomError (err , tErrs )) {
5246 return fiber .ErrRequestTimeout
5347 }
0 commit comments