MCPcopy Index your code
hub / github.com/labstack/echo / ToMiddleware

Method ToMiddleware

middleware/context_timeout.go:38–71  ·  view source on GitHub ↗

ToMiddleware converts Config to middleware.

()

Source from the content-addressed store, hash-verified

36
37// ToMiddleware converts Config to middleware.
38func (config ContextTimeoutConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
39 if config.Timeout == 0 {
40 return nil, errors.New("timeout must be set")
41 }
42 if config.Skipper == nil {
43 config.Skipper = DefaultSkipper
44 }
45 if config.ErrorHandler == nil {
46 config.ErrorHandler = func(c *echo.Context, err error) error {
47 if err != nil && errors.Is(err, context.DeadlineExceeded) {
48 return echo.ErrServiceUnavailable.Wrap(err)
49 }
50 return err
51 }
52 }
53
54 return func(next echo.HandlerFunc) echo.HandlerFunc {
55 return func(c *echo.Context) error {
56 if config.Skipper(c) {
57 return next(c)
58 }
59
60 timeoutContext, cancel := context.WithTimeout(c.Request().Context(), config.Timeout)
61 defer cancel()
62
63 c.SetRequest(c.Request().WithContext(timeoutContext))
64
65 if err := next(c); err != nil {
66 return config.ErrorHandler(c, err)
67 }
68 return nil
69 }
70 }, nil
71}

Callers

nothing calls this directly

Calls 3

RequestMethod · 0.80
SetRequestMethod · 0.80
WrapMethod · 0.45

Tested by

no test coverage detected