kataras/iris · error

iris: switch: hosts: invalid target type: %T

Error message

iris: switch: hosts: invalid target type: %T

What it means

In the hosts switch application (apps/switch_hosts.go), each case target must be a string host expression or a redirect-like target. When a matched case's target is neither a string nor a recognized host target type, hostApp panics with the Go type of the offending value so the developer can see what was wrongly passed.

Source

Thrown at apps/switch_hosts.go:114

		// using that app.
		if targetApp, ok := context.GetApplication(target); ok {
			// It's always iris.Application so we are totally safe here.
			return targetApp.(*iris.Application)
		}
		// If it's a real host, warn the user of invalid input.
		u, err := url.Parse(target)
		if err == nil && u.IsAbs() {
			// remember, we redirect hosts, not full URLs here.
			panic(fmt.Sprintf(`iris: switch: hosts: invalid target host: "%s"`, target))
		}

		if regex := regexp.MustCompile(host.Pattern); regex.MatchString(target) {
			panic(fmt.Sprintf(`iris: switch: hosts: loop detected between expression: "%s" and target host: "%s"`, host.Pattern, host.Target))
		}

		return newHostRedirectApp(target, HostsRedirectCode)
	default:
		panic(fmt.Sprintf("iris: switch: hosts: invalid target type: %T", target))
	}
}

func hostFilter(expr string) iris.Filter {
	regex := regexp.MustCompile(expr)
	return func(ctx iris.Context) bool {
		return regex.MatchString(ctx.Host())
	}
}

// HostsRedirectCode is the default status code is used
// to redirect a matching host to a url.
var HostsRedirectCode = iris.StatusMovedPermanently

func newHostRedirectApp(targetHost string, code int) *iris.Application {
	app := iris.New()
	app.Downgrade(func(w http.ResponseWriter, r *http.Request) {
		if targetHost == context.GetHost(r) {

View on GitHub (pinned to 7bedaf55a0)

Solutions

  1. Make the switch case target a string host expression (e.g. "example.com" or a regex-compatible host pattern).
  2. If a redirect is intended, use the supported redirect target constructor (newHostRedirectApp / HostsRedirectCode path) rather than a raw custom type.
  3. Inspect the %T output in the panic message to identify the actual Go type being passed and fix the value's origin.

Example fix

// before
cases := iris.SwitchCases{
  {Case: "old.example.com", Target: 301}, // int target
}
// after
cases := iris.SwitchCases{
  {Case: "old.example.com", Target: "https://new.example.com"},
}
Defensive patterns

Strategy: validation

Validate before calling

// before registering switch cases
for _, c := range cases {
  if _, ok := c.Target.(string); !ok {
    panic(fmt.Sprintf("case %q target must be a string, got %T", c.Case, c.Target))
  }
}

Type guard

func isValidHostTarget(t any) bool {
  switch t.(type) {
  case string:
    return true
  default:
    return false
  }
}

Prevention

When it happens

Trigger: Calling iris switch 'hosts' registration (via GetSwitchCases -> hostApp) with a case target that is not a string, e.g. passing an int, a struct, an iris handler, or a nil interface value as the target for a host pattern.

Common situations: Typo'd or dynamically computed switch cases where the target ends up as a non-string value; refactoring that replaced string host targets with custom objects; forgetting that only string host expressions and redirect targets are supported.

Related errors


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