{"record":{"id":"9508de9c18871eb4","repo":"kataras/iris","slug":"api-container-set-dependency-matcher-fn-cannot-b","errorCode":null,"errorMessage":"api container: set dependency matcher: fn cannot be nil","messagePattern":"api container: set dependency matcher: fn cannot be nil","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/router/api_container.go","lineNumber":112,"sourceCode":"\tapi.Container.MarkExportedFieldsAsRequired = strictMode\n\treturn api\n}\n\n// EnableStructDependents sets the container's EnableStructDependents to true.\n// It's used to automatically fill the dependencies of a struct's fields\n// based on the previous registered dependencies, just like function inputs.\nfunc (api *APIContainer) EnableStructDependents() *APIContainer {\n\tapi.Container.EnableStructDependents = true\n\treturn api\n}\n\n// SetDependencyMatcher replaces the function that compares equality between\n// a dependency and an input (struct field or function parameter).\n//\n// Defaults to hero.DefaultMatchDependencyFunc.\nfunc (api *APIContainer) SetDependencyMatcher(fn hero.DependencyMatcher) *APIContainer {\n\tif fn == nil {\n\t\tpanic(\"api container: set dependency matcher: fn cannot be nil\")\n\t}\n\n\tapi.Container.DependencyMatcher = fn\n\treturn api\n}\n\n// convertHandlerFuncs accepts Iris hero handlers and returns a slice of native Iris handlers.\nfunc (api *APIContainer) convertHandlerFuncs(relativePath string, handlersFn ...any) context.Handlers {\n\tfullpath := api.Self.GetRelPath() + relativePath\n\tparamsCount := macro.CountParams(fullpath, *api.Self.Macros())\n\n\thandlers := make(context.Handlers, 0, len(handlersFn))\n\tfor _, h := range handlersFn {\n\t\thandlers = append(handlers, api.Container.HandlerWithParams(h, paramsCount))\n\t}\n\n\t// Note: let end-developer to decide that through Party.SetExecutionRules.\n\t// On that type of handlers the end-developer does not have to include the Context in the handler,","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/kataras/iris/blob/7bedaf55a0b64bbb2248a5845a2c60d81a30996a/core/router/api_container.go#L94-L130","documentation":"APIContainer.SetDependencyMatcher replaces the function that decides whether a hero dependency matches an input (struct field or function parameter). A nil fn would break every subsequent dependency resolution, so the method panics when fn is nil. Defaults to hero.DefaultMatchDependencyFunc if never called.","triggerScenarios":"Calling APIContainer.SetDependencyMatcher(nil), typically with a variable of type hero.DependencyMatcher that was declared but never assigned, or a lookup of a custom matcher function that returned nil.","commonSituations":"Building custom DI matchers where the matcher is fetched from a registry/config and can be nil; typos where the result of the assignment was discarded; attempting to \"reset\" the matcher by passing nil instead of restoring hero.DefaultMatchDependencyFunc.","solutions":["Pass a non-nil hero.DependencyMatcher implementation to SetDependencyMatcher","To restore default behavior, set hero.DefaultMatchDependencyFunc explicitly instead of nil","Guard the value before calling: if fn != nil { api.SetDependencyMatcher(fn) }"],"exampleFix":"// before\nvar matcher hero.DependencyMatcher\napi.SetDependencyMatcher(matcher) // panics: fn cannot be nil\n// after\napi.SetDependencyMatcher(hero.DefaultMatchDependencyFunc) // or a custom non-nil matcher","handlingStrategy":"validation","validationCode":"func safeSetDependencyMatcher(api *iris.APIContainer, fn hero.DependencyMatcher) *iris.APIContainer {\n    if fn == nil {\n        fn = hero.DefaultMatchDependencyFunc\n    }\n    return api.SetDependencyMatcher(fn)\n}","typeGuard":"func isMatcherSet(fn hero.DependencyMatcher) bool {\n    return fn != nil\n}","tryCatchPattern":"func setMatcherSafe(api *iris.APIContainer, fn hero.DependencyMatcher) {\n    defer func() {\n        if r := recover(); r != nil {\n            log.Fatalf(\"SetDependencyMatcher panicked: %v\", r)\n        }\n    }()\n    api.SetDependencyMatcher(fn)\n}","preventionTips":["Never pass nil; fall back to hero.DefaultMatchDependencyFunc to reset behavior","Validate that registry/config lookups for custom matchers return non-nil","Set the matcher once during app bootstrap, before hero dependency resolution"],"tags":["panic","nil-argument","dependency-injection","di"],"backgroundTag":"nil-argument-panic","analyzedSha":"7bedaf55a0b64bbb2248a5845a2c60d81a30996a","analyzedAt":"2026-08-30T20:38:16.250Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}