{"id":"876c0bd1646a118b","repo":"labstack/echo","slug":"panic-err-from-config-tomiddleware","errorCode":null,"errorMessage":"panic: err from config.ToMiddleware()","messagePattern":"panic: err from config\\.ToMiddleware\\(\\)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"middleware/middleware.go","lineNumber":94,"sourceCode":"\t\t\t\treturn err\n\t\t\t}\n\t\t\treq.URL = url\n\n\t\t\treturn nil // rewrite only once\n\t\t}\n\t}\n\treturn nil\n}\n\n// DefaultSkipper returns false which processes the middleware.\nfunc DefaultSkipper(c *echo.Context) bool {\n\treturn false\n}\n\nfunc toMiddlewareOrPanic(config echo.MiddlewareConfigurator) echo.MiddlewareFunc {\n\tmw, err := config.ToMiddleware()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn mw\n}\n","sourceCodeStart":76,"sourceCodeEnd":98,"githubUrl":"https://github.com/labstack/echo/blob/05489dc1730161df26b72d1ae2a3ba6fb8178fc7/middleware/middleware.go#L76-L98","documentation":"Panicked by toMiddlewareOrPanic (middleware.go:94) when config.ToMiddleware() returns a non-nil error. This helper is used by all *WithConfig constructors (KeyAuthWithConfig, StaticWithConfig, CORSConfig, etc.) in v4. The panic surfaces configuration errors that ToMiddleware detects (missing validator, bad template, invalid origins, etc.).","triggerScenarios":"Calling any middleware.XxxWithConfig(cfg) where cfg violates the middleware's invariants: KeyAuth without a Validator, CORS with malformed AllowOrigins, Static with a bad template or Root, etc. The panic propagates the specific ToMiddleware error.","commonSituations":"Forgetting a required config field (e.g. KeyAuth Validator); supplying invalid strings parsed at construction; loading config from external sources without validation; running newly added middleware with incomplete config.","solutions":["Call config.ToMiddleware() directly and handle the error instead of using *WithConfig when you want non-panic behavior.","Inspect the panicked error to identify which config field is invalid.","Validate all config fields (Validator set, templates parse, origins well-formed) before constructing the middleware.","Wrap middleware setup in a function returning error for testability."],"exampleFix":"// before: panics on bad config at startup\nmw := middleware.KeyAuthWithConfig(middleware.KeyAuthConfig{}) // missing Validator\n// after: construct explicitly and handle error\nmw, err := middleware.KeyAuthConfig{}.ToMiddleware()\nif err != nil {\n    log.Fatal(err)\n}\ne.Use(mw)","handlingStrategy":"validation","validationCode":"// Use ToMiddleware() directly to get an error instead of a panic.\nmw, err := cfg.ToMiddleware()\nif err != nil {\n    return fmt.Errorf(\"middleware config invalid: %w\", err)\n}\ne.Use(mw)","typeGuard":null,"tryCatchPattern":"// Wrap *WithConfig construction to capture panics during setup.\nfunc buildMiddleware(cfg echo.MiddlewareConfigurator) (mw echo.MiddlewareFunc, err error) {\n    defer func() {\n        if r := recover(); r != nil {\n            err = fmt.Errorf(\"ToMiddleware panic: %v\", r)\n        }\n    }()\n    mw = middleware.ToMiddlewareOrPanic(cfg) // or the package-level WithConfig\n    return mw, nil\n}","preventionTips":["Prefer ToMiddleware() + explicit error handling over *WithConfig in code that must not panic.","Validate required config fields (Validator, templates, origins) before construction.","Add startup tests that build every middleware config to fail fast in CI."],"tags":["middleware","config","panic","startup"],"analyzedSha":"05489dc1730161df26b72d1ae2a3ba6fb8178fc7","analyzedAt":"2026-08-04T21:32:47.783Z","schemaVersion":2}