{"id":"4450bfc4ecf5d682","repo":"gofiber/fiber","slug":"fiber-config-regexhandler-must-not-return-nil","errorCode":null,"errorMessage":"fiber: Config.RegexHandler must not return nil","messagePattern":"fiber: Config\\.RegexHandler must not return nil","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"constraint.go","lineNumber":46,"sourceCode":"\t\treturn true\n\t}\n\tmatcherValue := reflect.ValueOf(matcher)\n\tswitch matcherValue.Kind() {\n\tcase reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice:\n\t\treturn matcherValue.IsNil()\n\tdefault:\n\t\treturn false\n\t}\n}\n\nfunc compileRegex(handler any, pattern string) regexMatcher {\n\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\")","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/constraint.go#L28-L64","documentation":"Panics from constraint.go:46 inside compileRegex when RegexHandler returns a nil matcher for a given pattern (isNilRegexMatcher check). Unlike the static validateRegexHandler checks, this only triggers at route registration when a regex(...) constraint is compiled and the handler yields nil - the value check validateRegexHandler cannot perform statically.","triggerScenarios":"Config.RegexHandler returns nil for an input pattern, e.g. func(string) *regexp.Regexp { return nil }, combined with any route using regex(...) (constraint.go:549 path). Pattern-specific failures where the handler returns nil instead of an error also hit this.","commonSituations":"A stub/mock handler that returns nil; a custom compiler that swallows compile errors and returns nil; refactoring regexp.MustCompile into a wrapper that forgot to forward the result.","solutions":["Return a real compiled matcher from RegexHandler for every input (regexp.MustCompile never returns nil - it panics on bad patterns instead).","If your wrapper can fail, validate the pattern and panic/return a non-nil zero matcher rather than nil.","Use the default regexp.MustCompile which guarantees a non-nil *regexp.Regexp."],"exampleFix":"// before\napp := fiber.New(fiber.Config{\n    RegexHandler: func(p string) *regexp.Regexp { return nil },\n})\napp.Get(\"/:v<regex(\\\\d+)>\", h)\n\n// after\napp := fiber.New(fiber.Config{\n    RegexHandler: regexp.MustCompile, // never returns nil\n})","handlingStrategy":"validation","validationCode":"// Wrap so a nil result is impossible.\nsafeHandler := func(p string) *regexp.Regexp {\n    re := regexp.MustCompile(p) // panics on bad pattern instead of returning nil\n    return re\n}\n_ = fiber.New(fiber.Config{RegexHandler: safeHandler})","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use regexp.MustCompile, which never returns nil.","If wrapping, forward the compiled matcher or panic on failure - never return nil.","Unit-test the handler against the patterns your routes actually use."],"tags":["routing","config","regex","constraint","nil-check","panic"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}