{"record":{"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":"critical","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/a105acad6c1e4576a77f01e02973f67e962bb58d/constraint.go#L40-L76","documentation":"validateRegexHandler runs at app initialization when Config.RegexHandler is set. It rejects values that are not reflect.Func or that are nil functions. This catches misconfiguration before any route is registered.","triggerScenarios":"Setting Config.RegexHandler to nil (explicitly), to a non-function value, or to a func variable that was declared but never assigned.","commonSituations":"Loading the handler from config/plugin and getting nil; passing regexp.MustCompile (correct) vs. passing a *regexp.Regexp instance (wrong, not a function); refactor changing the field type.","solutions":["Leave Config.RegexHandler unset to use the default regexp.MustCompile.","Pass a function value: Config.RegexHandler: regexp.MustCompile.","If loading dynamically, type-check it is a non-nil func before assignment."],"exampleFix":"// before\napp := fiber.New(fiber.Config{RegexHandler: nil})\n// or\napp := fiber.New(fiber.Config{RegexHandler: regexp.MustCompile(`\\d+`)}) // a Regexp, not a func\n\n// after\napp := fiber.New() // use default\n// or\napp := fiber.New(fiber.Config{RegexHandler: regexp.MustCompile}) // the function itself","handlingStrategy":"type-guard","validationCode":"// Validate RegexHandler is a non-nil function before passing to Config\nfunc validateRegexHandlerTyped(h any) error {\n    if h == nil { return errors.New(\"nil handler\") }\n    hv := reflect.ValueOf(h)\n    if hv.Kind() != reflect.Func { return errors.New(\"not a function\") }\n    if hv.IsNil() { return errors.New(\"nil function\") }\n    return nil\n}","typeGuard":"func isNonNilFunc(v any) bool {\n    if v == nil { return false }\n    rv := reflect.ValueOf(v)\n    return rv.Kind() == reflect.Func && !rv.IsNil()\n}","tryCatchPattern":null,"preventionTips":["Leave Config.RegexHandler unset when in doubt.","Pass the function value (regexp.MustCompile), not an instance.","Add a startup test that constructs fiber.New with your config."],"tags":["config","regex","reflection","nil-guard","startup"],"backgroundTag":null,"analyzedSha":"a105acad6c1e4576a77f01e02973f67e962bb58d","analyzedAt":"2026-08-11T17:33:26.942Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}