kataras/iris · error

invalid number of arguments

Error message

invalid number of arguments

What it means

bindResponse inspects the number of input arguments returned by a handler and only supports 0 or 1; anything else panics with 'invalid number of arguments'. It's an internal arity check on handlers used by OK/Create/NoContentOrNotModified helpers.

Source

Thrown at x/errors/handlers.go:370

		return nil
	}
}

func bindResponse[T, R any, F ResponseFunc[T, R]](ctx *context.Context, fn F, fnInput ...T) (R, bool) {
	var req T
	switch len(fnInput) {
	case 0:
		var ok bool
		req, ok = ReadPayload[T](ctx)
		if !ok {
			var resp R
			return resp, false
		}
	case 1:
		req = fnInput[0]
	default:
		panic("invalid number of arguments")
	}

	if !validateRequest(ctx, req) {
		var resp R
		return resp, false
	}

	resp, err := fn(ctx, req)
	if err == nil {
		if !validateResponse(ctx, req, &resp) {
			return resp, false
		}
	}

	return resp, !HandleError(ctx, err)
}

// OK handles a generic response and error from a service call and sends a JSON response to the client.

View on GitHub (pinned to 7bedaf55a0)

Solutions

  1. Change the handler to accept at most one request argument (plus the implicit context handling the framework provides)
  2. Bundle extra parameters into a single request struct
  3. Use the generic Handle/other APIs that support more arguments if more are needed

Example fix

// before
func(ctx, req Req, logger *log.Logger) Resp { ... }
// after
func(ctx, req Req) Resp { /* logger from ctx or package level */ }
Defensive patterns

Strategy: type-guard

Validate before calling

// keep handler arity <= 1 input argument
func validHandlerArity(fn any) bool {
    t := reflect.TypeOf(fn)
    return t != nil && t.NumIn() <= 1
}

Try / catch

defer func() { if r := recover(); r != nil { log.Printf("bindResponse panic: %v", r) } }()
xerrors.OK(handler)

Prevention

When it happens

Trigger: Registering a response handler function whose reflected invocation yields 2+ input arguments (e.g. func(ctx, req, extra)) through OK/Create/NoContentOrNotModified.

Common situations: Writing a handler with extra dependency parameters beyond the single request payload; copy-pasting handlers with additional arguments.

Related errors


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