{"record":{"id":"5053ee69531980a0","repo":"kataras/iris","slug":"bad-value-nil-t","errorCode":null,"errorMessage":"bad value: nil: %T","messagePattern":"bad value: nil: %T","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"hero/dependency.go","lineNumber":90,"sourceCode":"\treturn fmt.Sprintf(\"%s (%#+v)\", sourceLine, val)\n}\n\n// NewDependency converts a function or a function which accepts other dependencies or static struct value to a *Dependency.\n//\n// See `Container.Handler` for more.\nfunc NewDependency(dependency any, funcDependencies ...*Dependency) *Dependency { // used only on tests.\n\treturn newDependency(dependency, false, false, nil, funcDependencies...)\n}\n\nfunc newDependency(\n\tdependency any,\n\tdisablePayloadAutoBinding bool,\n\tenableStructDependents bool,\n\tmatchDependency DependencyMatcher,\n\tfuncDependencies ...*Dependency,\n) *Dependency {\n\tif dependency == nil {\n\t\tpanic(fmt.Sprintf(\"bad value: nil: %T\", dependency))\n\t}\n\n\tif d, ok := dependency.(*Dependency); ok {\n\t\t// already a *Dependency, do not continue (and most importatly do not call resolveDependency) .\n\t\treturn d\n\t}\n\n\tv := valueOf(dependency)\n\tif !goodVal(v) {\n\t\tpanic(fmt.Sprintf(\"bad value: %#+v\", dependency))\n\t}\n\n\tif matchDependency == nil {\n\t\tmatchDependency = DefaultDependencyMatcher\n\t}\n\n\tdest := &Dependency{\n\t\tSource:           newSource(v),","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/kataras/iris/blob/7bedaf55a0b64bbb2248a5845a2c60d81a30996a/hero/dependency.go#L72-L108","documentation":"newDependency normalizes any user-supplied dependency (static value, struct, or resolving func) into a *Dependency. If the supplied value is nil (nil interface or typed-nil that valueOf turns into an invalid value), hero cannot represent it and panics immediately with 'bad value: nil'. This is a registration-time guard so bad dependency declarations fail at startup.","triggerScenarios":"Calling Container.Register(nil), hero.NewDependency(nil), or NewDependency(nil) directly; registering a variable that is a nil *Dependency; a struct field whose value is a nil interface being fed into newDependency via getBindingsForStruct's nonZero field handling; passing a typed nil (e.g. (*Service)(nil)) that fails goodVal and surfaces the nil/bad-value path.","commonSituations":"A config/env loader returning nil before wiring dependencies; refactors where the dependency variable became a nil pointer; forgetting to initialize a service before container.Register(svc); map lookups returning a nil value passed straight to Register.","solutions":["Ensure the dependency value is initialized before registration: create the service instance (e.g. NewService()) rather than passing its nil pointer.","Add a nil check before calling Register: if svc == nil { panic/log } then register.","Return an error early from config loading instead of nil, so registration never sees nil.","If the dependency is genuinely optional, do not register it; let hero resolve other bindings.","Register a factory func (func() *Service) that builds the value lazily instead of a nil static value."],"exampleFix":"// before\nvar svc *MyService\ncontainer.Register(svc) // panics: bad value: nil\n// after\ncontainer.Register(func() *MyService { return NewMyService() })","handlingStrategy":"validation","validationCode":"func mustRegister(c *hero.Container, dep any) {\n    if dep == nil || reflect.ValueOf(dep).IsZero() {\n        panic(fmt.Sprintf(\"refusing to register nil dependency %T\", dep))\n    }\n    c.Register(dep)\n}","typeGuard":"func isNonNil(v any) bool {\n    if v == nil { return false }\n    rv := reflect.ValueOf(v)\n    switch rv.Kind() {\n    case reflect.Ptr, reflect.Interface, reflect.Map, reflect.Slice, reflect.Func, reflect.Chan:\n        return !rv.IsNil()\n    default:\n        return true\n    }\n}","tryCatchPattern":"func safeRegister(dep any) (ok bool) {\n    defer func() {\n        if r := recover(); r != nil {\n            if s, isStr := r.(string); isStr && strings.Contains(s, \"bad value\") {\n                log.Printf(\"dependency registration failed: %s\", s)\n            } else { panic(r) }\n        }\n    }()\n    container.Register(dep)\n    return true\n}","preventionTips":["Never register variables that may be nil; register factory funcs (func() *Service) instead.","Validate config/env loading completes and produces non-nil services before DI wiring.","Use constructor functions that panic early on unresolvable dependencies, keeping nils out of the graph.","Add a startup integration test that performs all container.Register calls so nils fail in CI."],"tags":["go","nil-value","dependency-injection","panic","registration"],"backgroundTag":"nil-dependency-value","analyzedSha":"7bedaf55a0b64bbb2248a5845a2c60d81a30996a","analyzedAt":"2026-08-30T20:38:16.250Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}