{"id":"c42266f78329c90c","repo":"gofiber/fiber","slug":"fiber-config-regexhandler-must-be-a-non-nil-funct","errorCode":null,"errorMessage":"fiber: Config.RegexHandler must be a non-nil function","messagePattern":"fiber: Config\\.RegexHandler must be a non-nil function","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"constraint.go","lineNumber":58,"sourceCode":"\tresult := reflect.ValueOf(handler).Call([]reflect.Value{reflect.ValueOf(pattern)})\n\tmatcher, ok := result[0].Interface().(regexMatcher)\n\tif !ok {\n\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.","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/constraint.go#L40-L76","documentation":"Panics from constraint.go:58 inside validateRegexHandler when RegexHandler is a non-nil value that is either not a function (Kind != Func) or is a nil function value (handlerValue.IsNil()). A nil interface is fine - it defaults to regexp.MustCompile; only a typed-but-nil or non-function value is rejected. Validation runs at app construction (app.go:791) and on every parseRoute call.","triggerScenarios":"New(Config{RegexHandler: \"invalid\"}) (a string), New(Config{RegexHandler: 42}) (an int), or passing a nil function variable: var f func(string) *regexp.Regexp; New(Config{RegexHandler: f}).","commonSituations":"Assigning the compiled regexp object instead of the compiler function (RegexHandler: regexp.MustCompile(`\\d+`) instead of regexp.MustCompile); typo where a field name is reused for a different type; DI injecting the wrong value.","solutions":["Pass the compiler function reference, not a value: RegexHandler: regexp.MustCompile (no parentheses, no argument).","Leave RegexHandler unset/nil to accept the regexp.MustCompile default.","If injecting from config, type-check at load time and fall back to the default when the value is not a function."],"exampleFix":"// before\nvar compile func(string) *regexp.Regexp // declared, never set\napp := fiber.New(fiber.Config{RegexHandler: compile})\n\n// after\napp := fiber.New() // defaults to regexp.MustCompile\n// or, explicitly:\napp := fiber.New(fiber.Config{RegexHandler: regexp.MustCompile})","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"func isValidRegexHandler(h any) bool {\n    if h == nil {\n        return true // defaults to regexp.MustCompile\n    }\n    hv := reflect.ValueOf(h)\n    if hv.Kind() != reflect.Func || hv.IsNil() {\n        return false\n    }\n    return true\n}","tryCatchPattern":null,"preventionTips":["Assign the function value regexp.MustCompile, not a call result or a non-function.","Leave RegexHandler unset to accept the default.","When loading from config, validate Kind==Func before assignment."],"tags":["routing","config","regex","constraint","panic","reflection"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}