kataras/iris · error

iris: switch: empty cases

Error message

iris: switch: empty cases

What it means

iris.Switch builds a host/path switching application from a SwitchProvider's cases. If the provider returns zero cases there is nothing to switch between, so it panics immediately. This is a configuration/programming error surfaced as a panic rather than a returned error.

Source

Thrown at apps/switch.go:63

//	if err := myOtherApp.Build(); err != nil {
//		panic(err)
//	}
//
//	app.Any("/api/identity/{p:path}", func(ctx iris.Context) {
//		apiPath := "/" + ctx.Params().Get("p")
//		r := ctx.Request()
//		r.URL.Path = apiPath
//		r.URL.RawPath = apiPath
//		ctx.Params().Remove("p")
//
//		myOtherApp.ServeHTTPC(ctx)
//	})
//
// app.Listen(":80")
func Switch(provider SwitchProvider, options ...SwitchOption) *iris.Application {
	cases := provider.GetSwitchCases()
	if len(cases) == 0 {
		panic("iris: switch: empty cases")
	}

	var friendlyAddrs []string
	if fp, ok := provider.(FriendlyNameProvider); ok {
		if friendlyName := fp.GetFriendlyName(); friendlyName != "" {
			friendlyAddrs = append(friendlyAddrs, friendlyName)
		}
	}

	opts := DefaultSwitchOptions()
	for _, opt := range options {
		if opt == nil {
			continue
		}

		opt.Apply(&opts)
	}

View on GitHub (pinned to 7bedaf55a0)

Solutions

  1. Populate the provider with at least one switch case before calling Switch
  2. Guard: check len(hosts) > 0 before invoking iris.SwitchHosts/Switch
  3. Fix config/env loading so host entries are actually read

Example fix

// before
app := iris.Switch(iris.SwitchHosts(iris.Hosts{}))
// after
hosts := iris.Hosts{"example.com": app2}
if len(hosts) == 0 { panic("no hosts configured") }
app := iris.Switch(iris.SwitchHosts(hosts))
Defensive patterns

Strategy: validation

Validate before calling

cases := provider.GetSwitchCases()
if len(cases) == 0 { return errors.New("switch: provider returned no cases") }

Try / catch

defer func() {
	if r := recover(); r != nil {
		if s, ok := r.(string); ok && strings.Contains(s, "iris: switch: empty cases") {
			log.Fatal("no switch cases configured")
		}
		panic(r)
	}
}()

Prevention

When it happens

Trigger: Calling Switch with a provider whose GetSwitchCases() returns an empty slice — e.g. an empty Hosts map passed to iris.SwitchHosts, or cases filtered out at runtime.

Common situations: Building hosts from environment/config that loaded empty; a map literal with no entries; conditional code that skips registering any case.

Related errors


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