{"record":{"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":"critical","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/a105acad6c1e4576a77f01e02973f67e962bb58d/constraint.go#L28-L64","documentation":"After compileRegex invokes the RegexHandler, isNilRegexMatcher checks the returned matcher; if it is a nil interface or a nil pointer/slice/map/etc., the app panics. A nil matcher would crash on the first request, so registration fails fast instead.","triggerScenarios":"Config.RegexHandler returns nil, or returns a typed nil (e.g. (*regexp.Regexp)(nil)) that compiles a pattern that the handler then returns as nil on failure.","commonSituations":"Custom handler that swallows compile errors and returns nil; handler that returns a nil *regexp.Regexp when regexp.Compile fails instead of propagating the error; refactor returning zero value.","solutions":["Have your RegexHandler propagate compile errors (panic or return a non-nil fallback) instead of returning nil.","Default to regexp.MustCompile when in doubt: leave Config.RegexHandler unset.","In a custom handler, on Compile error, fall back to a never-match sentinel: regexp.MustCompile(`$^`)."],"exampleFix":"// before\nRegexHandler: func(p string) *regexp.Regexp {\n  re, err := regexp.Compile(p)\n  if err != nil { return nil } // triggers this panic\n  return re\n}\n\n// after\nRegexHandler: func(p string) *regexp.Regexp {\n  re, err := regexp.Compile(p)\n  if err != nil { return regexp.MustCompile(`\\A\\z`) } // matches nothing, but non-nil\n  return re\n}","handlingStrategy":"validation","validationCode":"// Smoke-test the handler against a sample pattern before registering\nfunc checkRegexHandlerNonNil(h func(string) any) error {\n    out := h(\"test\")\n    if out == nil { return errors.New(\"handler returned nil\") }\n    rv := reflect.ValueOf(out)\n    if (rv.Kind() == reflect.Ptr || rv.Kind() == reflect.Interface) && rv.IsNil() {\n        return errors.New(\"handler returned typed nil\")\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never swallow compile errors in your RegexHandler - propagate or use a sentinel.","Add a smoke test that calls the handler with several patterns.","Prefer leaving Config.RegexHandler unset."],"tags":["config","regex","constraints","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"}