{"id":"21ffb9b881d57fd4","repo":"labstack/echo","slug":"echo-body-dump-middleware-requires-a-handler-funct","errorCode":null,"errorMessage":"echo body-dump middleware requires a handler function","messagePattern":"echo body-dump middleware requires a handler function","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"middleware/body_dump.go","lineNumber":75,"sourceCode":"// in production), explicitly set MaxRequestBytes and MaxResponseBytes to -1.\nfunc BodyDump(handler BodyDumpHandler) echo.MiddlewareFunc {\n\treturn BodyDumpWithConfig(BodyDumpConfig{Handler: handler})\n}\n\n// BodyDumpWithConfig returns a BodyDump middleware with config.\n// See: `BodyDump()`.\n//\n// SECURITY: If MaxRequestBytes and MaxResponseBytes are not set (zero values), they default\n// to 5MB each to prevent DoS attacks via large payloads. Set them explicitly to -1 to disable\n// limits if needed for your use case.\nfunc BodyDumpWithConfig(config BodyDumpConfig) echo.MiddlewareFunc {\n\treturn toMiddlewareOrPanic(config)\n}\n\n// ToMiddleware converts BodyDumpConfig to middleware or returns an error for invalid configuration\nfunc (config BodyDumpConfig) ToMiddleware() (echo.MiddlewareFunc, error) {\n\tif config.Handler == nil {\n\t\treturn nil, errors.New(\"echo body-dump middleware requires a handler function\")\n\t}\n\tif config.Skipper == nil {\n\t\tconfig.Skipper = DefaultSkipper\n\t}\n\tif config.MaxRequestBytes == 0 {\n\t\tconfig.MaxRequestBytes = 5 * MB\n\t}\n\tif config.MaxResponseBytes == 0 {\n\t\tconfig.MaxResponseBytes = 5 * MB\n\t}\n\n\treturn func(next echo.HandlerFunc) echo.HandlerFunc {\n\t\treturn func(c *echo.Context) error {\n\t\t\tif config.Skipper(c) {\n\t\t\t\treturn next(c)\n\t\t\t}\n\n\t\t\treqBuf := bodyDumpBufferPool.Get().(*bytes.Buffer)","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/labstack/echo/blob/05489dc1730161df26b72d1ae2a3ba6fb8178fc7/middleware/body_dump.go#L57-L93","documentation":"Returned by BodyDumpConfig.ToMiddleware when config.Handler is nil, then converted to a panic by toMiddlewareOrPanic. BodyDump middleware captures request and response payloads and must have a callback (BodyDumpHandler) to deliver them to; without a Handler there is nothing to do with the captured data. This is a startup/configuration error.","triggerScenarios":"Calling middleware.BodyDumpWithConfig(middleware.BodyDumpConfig{}) without setting Handler, or calling middleware.BodyDump(nil). The panic happens at middleware chain construction time.","commonSituations":"Enabling BodyDump for logging/debugging but forgetting to wire the callback. Refactoring that removes the Handler assignment. Copy-paste error from docs.","solutions":["Provide a Handler function: middleware.BodyDump(func(c echo.Context, req, res []byte, err error) { log.Printf(...) })","If using BodyDumpWithConfig, set config.Handler to a BodyDumpHandler function","Use config.ToMiddleware() to get an error instead of a panic if you prefer graceful handling"],"exampleFix":"// before\nmw := middleware.BodyDumpWithConfig(middleware.BodyDumpConfig{}) // panic\n\n// after\nmw := middleware.BodyDump(func(c echo.Context, reqBody, resBody []byte, err error) {\n    log.Printf(\"req=%s res=%s\", reqBody, resBody)\n})","handlingStrategy":"validation","validationCode":"// Use ToMiddleware (returns error) instead of WithConfig (panics)\nif _, err := (middleware.BodyDumpConfig{}).ToMiddleware(); err != nil {\n    log.Fatal(\"BodyDump requires Handler:\", err)\n}","typeGuard":null,"tryCatchPattern":"defer func() {\n    if r := recover(); r != nil {\n        log.Fatal(\"middleware setup failed:\", r)\n    }\n}()\nmw := middleware.BodyDumpWithConfig(cfg)","preventionTips":["Always pass a Handler callback to BodyDump / BodyDumpConfig","Use config.ToMiddleware() to handle config errors gracefully","Build middleware chains in an init or setup function with tests"],"tags":["middleware","body-dump","configuration","panic","startup"],"analyzedSha":"05489dc1730161df26b72d1ae2a3ba6fb8178fc7","analyzedAt":"2026-08-04T21:32:47.783Z","schemaVersion":2}