{"id":"57621590492a80e5","repo":"gofiber/fiber","slug":"fiber-config-regexhandler-must-have-signature-fun","errorCode":null,"errorMessage":"fiber: Config.RegexHandler must have signature func(string) T","messagePattern":"fiber: Config\\.RegexHandler must have signature func\\(string\\) T","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"constraint.go","lineNumber":61,"sourceCode":"\t\tpanic(\"fiber: Config.RegexHandler return type must support MatchString(string) bool\")\n\t}\n\tif isNilRegexMatcher(matcher) {\n\t\tpanic(\"fiber: Config.RegexHandler must not return nil\")\n\t}\n\treturn matcher\n}\n\nfunc validateRegexHandler(handler any) any {\n\tif handler == nil {\n\t\treturn regexp.MustCompile\n\t}\n\thandlerValue := reflect.ValueOf(handler)\n\thandlerType := handlerValue.Type()\n\tif handlerType.Kind() != reflect.Func || handlerValue.IsNil() {\n\t\tpanic(\"fiber: Config.RegexHandler must be a non-nil function\")\n\t}\n\tif handlerType.NumIn() != 1 || handlerType.In(0) != stringType || handlerType.NumOut() != 1 {\n\t\tpanic(\"fiber: Config.RegexHandler must have signature func(string) T\")\n\t}\n\tif !handlerType.Out(0).Implements(regexMatcherType) {\n\t\tpanic(\"fiber: Config.RegexHandler return type must support MatchString(string) bool\")\n\t}\n\treturn handler\n}\n\n// ConstraintHandler is the interface that all constraints must implement.\n// Built-in and custom constraints are treated uniformly through this interface.\ntype ConstraintHandler interface {\n\t// Name returns the constraint identifier used in route patterns (e.g. \"int\", \"minLen\", \"regex\").\n\tName() string\n\n\t// Execute validates a request parameter value against the constraint.\n\t// param is the request parameter value to check.\n\t// data contains the pre-typed constraint data produced by Analyze() at registration time.\n\tExecute(param string, data []any) bool\n}","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/constraint.go#L43-L79","documentation":"Panics from constraint.go:61 inside validateRegexHandler when RegexHandler is a function but its signature is not exactly func(string) T (one string input, one output). The checks are NumIn()!=1, In(0)!=string, or NumOut()!=1. Validation happens at construction and route parsing.","triggerScenarios":"A handler taking the wrong input type: func(int) *regexp.Regexp; a handler with no return: func(string); a handler with extra inputs or outputs; or a named input type that is not string (func(regexPattern) *regexp.Regexp).","commonSituations":"Wrapping MustCompile to inject options/loggin and changing the arity; passing regexp.Compile (which returns (*Regexp, error) - two outputs) instead of regexp.MustCompile; using a named string type as the parameter.","solutions":["Keep the signature func(string) T; prefer assigning regexp.MustCompile directly.","If you need regexp.Compile's error, wrap it to panic or log+fallback so the signature stays single-return.","Use a typed adapter that preserves the func(string) *regexp.Regexp shape."],"exampleFix":"// before\napp := fiber.New(fiber.Config{\n    RegexHandler: regexp.Compile, // returns (*Regexp, error) -> 2 outputs\n})\n\n// after\napp := fiber.New(fiber.Config{\n    RegexHandler: func(p string) *regexp.Regexp {\n        re, err := regexp.Compile(p)\n        if err != nil {\n            panic(err)\n        }\n        return re\n    },\n})","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"func hasRegexHandlerSignature(h any) bool {\n    ht := reflect.ValueOf(h).Type()\n    return ht.NumIn() == 1 && ht.In(0) == reflect.TypeFor[string]() && ht.NumOut() == 1\n}","tryCatchPattern":null,"preventionTips":["Keep RegexHandler as func(string) T; do not use regexp.Compile (two returns).","Prefer assigning regexp.MustCompile directly.","Wrap multi-return compilers in an adapter that preserves single return."],"tags":["routing","config","regex","constraint","signature","panic"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}