{"id":"7287e5312314627c","repo":"labstack/echo","slug":"echo-key-auth-middleware-could-not-create-key-extr","errorCode":null,"errorMessage":"echo key-auth middleware could not create key extractor: %w","messagePattern":"echo key-auth middleware could not create key extractor: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"middleware/key_auth.go","lineNumber":153,"sourceCode":"}\n\n// ToMiddleware converts KeyAuthConfig to middleware or returns an error for invalid configuration\nfunc (config KeyAuthConfig) ToMiddleware() (echo.MiddlewareFunc, error) {\n\tif config.Skipper == nil {\n\t\tconfig.Skipper = DefaultKeyAuthConfig.Skipper\n\t}\n\tif config.KeyLookup == \"\" {\n\t\tconfig.KeyLookup = DefaultKeyAuthConfig.KeyLookup\n\t}\n\tif config.Validator == nil {\n\t\treturn nil, errors.New(\"echo key-auth middleware requires a validator function\")\n\t}\n\n\tlimit := cmp.Or(config.AllowedCheckLimit, 1)\n\n\textractors, cErr := createExtractors(config.KeyLookup, limit)\n\tif cErr != nil {\n\t\treturn nil, fmt.Errorf(\"echo key-auth middleware could not create key extractor: %w\", cErr)\n\t}\n\tif len(extractors) == 0 {\n\t\treturn nil, errors.New(\"echo key-auth middleware could not create extractors from KeyLookup string\")\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\tvar lastExtractorErr error\n\t\t\tvar lastValidatorErr error\n\t\t\tfor _, extractor := range extractors {\n\t\t\t\tkeys, source, extrErr := extractor(c)\n\t\t\t\tif extrErr != nil {\n\t\t\t\t\tlastExtractorErr = extrErr\n\t\t\t\t\tcontinue","sourceCodeStart":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/labstack/echo/blob/05489dc1730161df26b72d1ae2a3ba6fb8178fc7/middleware/key_auth.go#L135-L171","documentation":"Returned by KeyAuthConfig.ToMiddleware() when createExtractors() fails to parse the config.KeyLookup string. The lookup string must follow the 'source:name' format (e.g. 'header:Authorization', 'query:token', 'cookie:session', 'form:key', 'param:id') optionally comma-separated for multiple sources. If a segment cannot be split into at least two colon-delimited parts, or uses an unknown source prefix, the error wraps that failure.","triggerScenarios":"Calling middleware.KeyAuthWithConfig (or KeyAuth) with a KeyLookup value like 'Authorization' (missing source prefix), 'header' (missing name), 'foo:bar' (unknown source 'foo'), or an empty segment from a trailing comma. The error surfaces during middleware construction, not at request time.","commonSituations":"Typos in the KeyLookup string; copying a header name without the 'header:' prefix; leaving a dangling comma in a multi-source lookup; migrating from another framework that uses a different lookup syntax.","solutions":["Set KeyLookup to a valid 'source:name' pair, e.g. 'header:Authorization' or 'query:api-key'.","For multiple extraction sources, comma-separate valid pairs, e.g. 'header:X-API-Key,query:key'.","Leave KeyLookup empty to use the default 'header:Authorization' (with 'Bearer ' optional prefix).","Remove trailing commas or empty segments from the lookup string."],"exampleFix":"// before\nmiddleware.KeyAuthWithConfig(middleware.KeyAuthConfig{\n    Validator:    fn,\n    KeyLookup:    \"Authorization\", // missing source prefix\n})\n// after\nmiddleware.KeyAuthWithConfig(middleware.KeyAuthConfig{\n    Validator:    fn,\n    KeyLookup:    \"header:Authorization\",\n})","handlingStrategy":"validation","validationCode":"// Validate KeyLookup before building the middleware.\nfunc validKeyLookup(s string) error {\n    if s == \"\" {\n        return nil // default applies\n    }\n    for _, src := range strings.Split(s, \",\") {\n        parts := strings.Split(src, \":\")\n        if len(parts) < 2 {\n            return fmt.Errorf(\"invalid KeyLookup segment %q: need 'source:name'\", src)\n        }\n        switch parts[0] {\n        case \"query\", \"param\", \"cookie\", \"form\", \"header\":\n        default:\n            return fmt.Errorf(\"invalid KeyLookup source %q in %q\", parts[0], src)\n        }\n    }\n    return nil\n}\n\n// usage\nif err := validKeyLookup(cfg.KeyLookup); err != nil { return err }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer middleware.KeyAuth(fn) which uses the default 'header:Authorization' lookup.","Construct via KeyAuthConfig{}.ToMiddleware() and handle the returned error instead of panicking.","Add a unit test that builds your middleware config during TestMain to catch malformed lookups early."],"tags":["config","key-auth","middleware","startup"],"analyzedSha":"05489dc1730161df26b72d1ae2a3ba6fb8178fc7","analyzedAt":"2026-08-04T21:32:47.783Z","schemaVersion":2}