kataras/iris · error

bad value: function has invalid number of output arguments:

Error message

bad value: function has invalid number of output arguments: %v

What it means

Dependency functions may return at most two values: the dependency (optionally plus an error). Any function returning three or more values cannot be mapped to a single dependency and panics.

Source

Thrown at hero/dependency.go:302

		}

		// fallback to structure.
		return fromStructValue(v.Call(nil)[0], dest)
	}

	if numOut == 0 {
		panic("bad value: function has zero outputs")
	}

	if numOut == 2 && !isError(typ.Out(1)) {
		panic("bad value: second output should be an error")
	}

	if numOut > 2 {
		// - at least one output value
		// - maximum of two output values
		// - second output value should be a type of error.
		panic(fmt.Sprintf("bad value: function has invalid number of output arguments: %v", numOut))
	}

	var handler DependencyHandler

	firstIsContext := isContext(typ.In(0))
	secondIsInput := numIn == 2 && typ.In(1) == inputTyp
	onlyContext := (numIn == 1 && firstIsContext) || (numIn == 2 && firstIsContext && typ.IsVariadic())

	if onlyContext || (firstIsContext && secondIsInput) {
		handler = handlerFromFunc(v, typ)
	}

	if handler == nil {
		return false
	}

	dest.DestType = typ.Out(0)
	dest.Handle = handler

View on GitHub (pinned to 7bedaf55a0)

Solutions

  1. Reduce outputs to (T) or (T, error).
  2. Bundle multiple outputs into one struct: type Deps struct{ A A; B B } and return (Deps, error).
  3. Register a thin wrapper closure that collapses the multi-return: c.Register(func() (A, error) { a, b, err := f(); return a, err }).

Example fix

// before
func newClients() (*ClientA, *ClientB, error) { ... }
c.Register(newClients)
// after
type Clients struct { A *ClientA; B *ClientB }
func newClients() (*Clients, error) { ... }
c.Register(newClients)
Defensive patterns

Strategy: validation

Validate before calling

func outCountOK(fn any) bool { n := reflect.TypeOf(fn).NumOut(); return n == 1 || n == 2 }

Prevention

When it happens

Trigger: Registering funcs like func() (A, B, error) or func(cfg) (A, B, C) via c.Register/NewDependency.

Common situations: Go idiom of multi-return factories used directly as a dependency; returning the dependency plus metrics/clients alongside error; gRPC-style (resp, err) wrapped further.

Related errors


AI-assisted analysis of kataras/iris@7bedaf55a0 (2026-08-30). Data as JSON: /api/errors/7c445ed64742f866. Report an issue: GitHub.