kataras/iris · error

Passed handler cannot be converted directly: - http.H

Error message

			Passed handler cannot be converted directly:
			- http.Handler(http.Handler)
			---------------------------------------------------------------------
			Please use the Application.WrapRouter method instead, example code:
			app := iris.New()
			// ...
			app.WrapRouter(func(w http.ResponseWriter, r *http.Request, router http.HandlerFunc) {
			    httpThirdPartyHandler(router).ServeHTTP(w, r)
			})

What it means

FromStd panics when handed a func(http.Handler) http.Handler (middleware-style adapter) because it cannot be converted to an iris handler directly — it wraps the router rather than a handler. The library directs you to Application.WrapRouter instead.

Source

Thrown at core/handlerconv/from_std.go:36

	switch h := handler.(type) {
	case context.Handler:
		return h
	// case func(*context.Context):
	// 	return h
	case http.Handler:
		// handlerFunc.ServeHTTP(w,r)
		return func(ctx *context.Context) {
			h.ServeHTTP(ctx.ResponseWriter(), ctx.Request())
		}
	case func(http.ResponseWriter, *http.Request):
		// handlerFunc(w,r)
		return FromStd(http.HandlerFunc(h))
	case func(http.ResponseWriter, *http.Request, http.HandlerFunc):
		// handlerFunc(w,r, http.HandlerFunc)
		//
		return FromStdWithNext(h)
	case func(http.Handler) http.Handler:
		panic(fmt.Errorf(`
			Passed handler cannot be converted directly:
			- http.Handler(http.Handler)
			---------------------------------------------------------------------
			Please use the Application.WrapRouter method instead, example code:
			app := iris.New()
			// ...
			app.WrapRouter(func(w http.ResponseWriter, r *http.Request, router http.HandlerFunc) {
			    httpThirdPartyHandler(router).ServeHTTP(w, r)
			})`))
	default:
		// No valid handler passed
		panic(fmt.Errorf(`
			Passed argument is not a func(iris.Context) neither one of these types:
			- http.Handler
			- func(w http.ResponseWriter, r *http.Request)
			- func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)
			---------------------------------------------------------------------
			It seems to be a %T points to: %v`, handler, handler))

View on GitHub (pinned to 7bedaf55a0)

Solutions

  1. Register the middleware via app.WrapRouter, adapting it: app.WrapRouter(func(w, r, router){ myMiddleware(http.HandlerFunc(router)).ServeHTTP(w, r) }).
  2. If the middleware can be invoked with a final handler, wrap the iris route handler instead of the router.
  3. Convert to an iris-native handler using iris.FromStdWithNext-style semantics or rewrite the middleware for iris.

Example fix

// before
app.UseRouter(fromStd(middleware)) // func(http.Handler) http.Handler -> panic
// after
app.WrapRouter(func(w http.ResponseWriter, r *http.Request, router http.HandlerFunc) {
    middleware(http.HandlerFunc(router)).ServeHTTP(w, r)
})
Defensive patterns

Strategy: type-guard

Validate before calling

// detect middleware-style adapters
switch h.(type) {
case func(http.Handler) http.Handler:
    // must go through app.WrapRouter, not FromStd/Use
}

Type guard

func isHandlerMiddleware(v interface{}) bool {
    _, ok := v.(func(http.Handler) http.Handler)
    return ok
}

Try / catch

defer func() {
    if r := recover(); r != nil {
        if strings.Contains(fmt.Sprint(r), "cannot be converted directly") {
            log.Fatal("use app.WrapRouter for func(http.Handler) http.Handler middleware")
        }
        panic(r)
    }
}()

Prevention

When it happens

Trigger: Calling handlerconv.FromStd (or app.Use/Handle with) a third-party middleware of type func(http.Handler) http.Handler, e.g. alice-style or gorilla middleware, promhttp.InstrumentHandlerDuration-style adapters.

Common situations: Mounting go chi/negroni/alice middleware on an iris app; passing a net/http middleware chain as if it were a plain handler.

Related errors


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