kataras/iris · error

at least one context response handler function is required

Error message

at least one context response handler function is required

What it means

joinContextResponseFuncs composes a list of ContextResponseFunc handlers and panics when the list is empty or the first handler is nil, since composing zero response handlers is meaningless.

Solutions

  1. Provide at least one non-nil ContextResponseFunc to Intercept
  2. Sanitize the slice (drop nils) before calling Intercept
  3. Return a default/no-op handler when the list would be empty

Example fix

// before
resp := xerrors.Intercept(nilHandlers...)
// after
if len(handlers) > 0 && handlers[0] != nil {
    resp = xerrors.Intercept(handlers...)
}
Defensive patterns

Strategy: validation

Validate before calling

func canJoinResponseFuncs[T, R any](fs []ContextResponseFunc[T, R]) bool {
    return len(fs) > 0 && fs[0] != nil
}

Try / catch

defer func() { if r := recover(); r != nil { log.Printf("response chain panic: %v", r) } }()
h := joinContextResponseFuncs(handlers)

Prevention

When it happens

Trigger: Calling x/errors Intercept(...) (which calls joinContextResponseFuncs) with no response handlers or with a nil first element.

Common situations: Same as the request-side twin: dynamically built handler slices left empty, or nil entries from map lookups.

Related errors


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

Appendix: source

Thrown at x/errors/handlers.go:335

// Example Code:
//
//	app.Post("/", errors.Intercept(func(ctx iris.Context, req *CreateRequest, resp *CreateResponse) error{ ... }), errors.CreateHandler(service.Create))
func Intercept[T, R any](responseHandlers ...ContextResponseFunc[T, R]) context.Handler {
	if len(responseHandlers) == 0 {
		return nil
	}

	responseHandler := joinContextResponseFuncs(responseHandlers)

	return func(ctx *context.Context) {
		ctx.Values().Set(contextResponseHandlerFuncKey, responseHandler)
		ctx.Next()
	}
}

func joinContextResponseFuncs[T, R any](responseHandlerFuncs []ContextResponseFunc[T, R]) ContextResponseFunc[T, R] {
	if len(responseHandlerFuncs) == 0 || responseHandlerFuncs[0] == nil {
		panic("at least one context response handler function is required")
	}

	if len(responseHandlerFuncs) == 1 {
		return responseHandlerFuncs[0]
	}

	return func(ctx *context.Context, req T, resp *R) error {
		for _, handler := range responseHandlerFuncs {
			if handler == nil {
				continue
			}

			if err := handler(ctx, req, resp); err != nil {
				return err
			}
		}

		return nil

View on GitHub (pinned to 7bedaf55a0)