{"record":{"id":"73d3858eaa86601f","repo":"kataras/iris","slug":"bad-value-second-output-should-be-an-error","errorCode":null,"errorMessage":"bad value: second output should be an error","messagePattern":"bad value: second output should be an error","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"hero/dependency.go","lineNumber":295,"sourceCode":"\tif numIn == 0 {\n\t\t// it's an empty function, that must return a structure.\n\t\tif numOut != 1 {\n\t\t\tfirstOutType := indirectType(typ.Out(0))\n\t\t\tif firstOutType.Kind() != reflect.Struct && firstOutType.Kind() != reflect.Interface {\n\t\t\t\tpanic(fmt.Sprintf(\"bad value: function has zero inputs: empty input function must output a single value but got: length=%v, type[0]=%s\", numOut, firstOutType.String()))\n\t\t\t}\n\t\t}\n\n\t\t// fallback to structure.\n\t\treturn fromStructValue(v.Call(nil)[0], dest)\n\t}\n\n\tif numOut == 0 {\n\t\tpanic(\"bad value: function has zero outputs\")\n\t}\n\n\tif numOut == 2 && !isError(typ.Out(1)) {\n\t\tpanic(\"bad value: second output should be an error\")\n\t}\n\n\tif numOut > 2 {\n\t\t// - at least one output value\n\t\t// - maximum of two output values\n\t\t// - second output value should be a type of error.\n\t\tpanic(fmt.Sprintf(\"bad value: function has invalid number of output arguments: %v\", numOut))\n\t}\n\n\tvar handler DependencyHandler\n\n\tfirstIsContext := isContext(typ.In(0))\n\tsecondIsInput := numIn == 2 && typ.In(1) == inputTyp\n\tonlyContext := (numIn == 1 && firstIsContext) || (numIn == 2 && firstIsContext && typ.IsVariadic())\n\n\tif onlyContext || (firstIsContext && secondIsInput) {\n\t\thandler = handlerFromFunc(v, typ)\n\t}","sourceCodeStart":277,"sourceCodeEnd":313,"githubUrl":"https://github.com/kataras/iris/blob/7bedaf55a0b64bbb2248a5845a2c60d81a30996a/hero/dependency.go#L277-L313","documentation":"When a dependency function returns exactly two values, the second must implement the error interface so the container can short-circuit on failure. Returning two values where the second is not an error is rejected.","triggerScenarios":"Registering a function like func() (*Svc, *Result) or func(db *DB) (A, B) where the second return type is not error, via c.Register/NewDependency.","commonSituations":"Multi-value factories where the author meant to return two dependencies (not supported); a custom error-like type that does not implement error; accidentally swapped return order (error first).","solutions":["Make the second return value of type error: func() (*Svc, error).","If you need two non-error outputs, return a struct combining them: type Pair struct{ A A; B B }.","Ensure the second type actually implements error (has an Error() string method), or use the standard error interface."],"exampleFix":"// before\nfunc newPair() (A, B) { ... }\nc.Register(newPair)\n// after\nfunc newPair() (*Pair, error) { p, err := build(); return p, err }\n// with type Pair struct { A A; B B }","handlingStrategy":"validation","validationCode":"func secondOutIsError(fn any) bool {\n\tt := reflect.TypeOf(fn)\n\treturn t.NumOut() != 2 || t.Out(1).Implements(errType)\n}\nvar errType = reflect.TypeOf((*error)(nil)).Elem()","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Two-output dependency funcs must be (T, error).","Use the standard error interface as the second return.","Return a struct if you need two data values."],"tags":["dependency-injection","function-signature","panic","error-return"],"backgroundTag":"invalid-dependency-signature","analyzedSha":"7bedaf55a0b64bbb2248a5845a2c60d81a30996a","analyzedAt":"2026-08-30T20:38:16.250Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}