{"record":{"id":"0e445389a4102ef3","repo":"kataras/iris","slug":"iris-switch-empty-cases","errorCode":null,"errorMessage":"iris: switch: empty cases","messagePattern":"iris: switch: empty cases","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"apps/switch.go","lineNumber":63,"sourceCode":"//\tif err := myOtherApp.Build(); err != nil {\n//\t\tpanic(err)\n//\t}\n//\n//\tapp.Any(\"/api/identity/{p:path}\", func(ctx iris.Context) {\n//\t\tapiPath := \"/\" + ctx.Params().Get(\"p\")\n//\t\tr := ctx.Request()\n//\t\tr.URL.Path = apiPath\n//\t\tr.URL.RawPath = apiPath\n//\t\tctx.Params().Remove(\"p\")\n//\n//\t\tmyOtherApp.ServeHTTPC(ctx)\n//\t})\n//\n// app.Listen(\":80\")\nfunc Switch(provider SwitchProvider, options ...SwitchOption) *iris.Application {\n\tcases := provider.GetSwitchCases()\n\tif len(cases) == 0 {\n\t\tpanic(\"iris: switch: empty cases\")\n\t}\n\n\tvar friendlyAddrs []string\n\tif fp, ok := provider.(FriendlyNameProvider); ok {\n\t\tif friendlyName := fp.GetFriendlyName(); friendlyName != \"\" {\n\t\t\tfriendlyAddrs = append(friendlyAddrs, friendlyName)\n\t\t}\n\t}\n\n\topts := DefaultSwitchOptions()\n\tfor _, opt := range options {\n\t\tif opt == nil {\n\t\t\tcontinue\n\t\t}\n\n\t\topt.Apply(&opts)\n\t}\n","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/kataras/iris/blob/7bedaf55a0b64bbb2248a5845a2c60d81a30996a/apps/switch.go#L45-L81","documentation":"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.","triggerScenarios":"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.","commonSituations":"Building hosts from environment/config that loaded empty; a map literal with no entries; conditional code that skips registering any case.","solutions":["Populate the provider with at least one switch case before calling Switch","Guard: check len(hosts) > 0 before invoking iris.SwitchHosts/Switch","Fix config/env loading so host entries are actually read"],"exampleFix":"// before\napp := iris.Switch(iris.SwitchHosts(iris.Hosts{}))\n// after\nhosts := iris.Hosts{\"example.com\": app2}\nif len(hosts) == 0 { panic(\"no hosts configured\") }\napp := iris.Switch(iris.SwitchHosts(hosts))","handlingStrategy":"validation","validationCode":"cases := provider.GetSwitchCases()\nif len(cases) == 0 { return errors.New(\"switch: provider returned no cases\") }","typeGuard":null,"tryCatchPattern":"defer func() {\n\tif r := recover(); r != nil {\n\t\tif s, ok := r.(string); ok && strings.Contains(s, \"iris: switch: empty cases\") {\n\t\t\tlog.Fatal(\"no switch cases configured\")\n\t\t}\n\t\tpanic(r)\n\t}\n}()","preventionTips":["Check host/config map length before calling Switch","Fail fast at config load time on empty host sets","Log configured cases at startup"],"tags":["iris","panic","configuration"],"backgroundTag":"empty-switch-cases","analyzedSha":"7bedaf55a0b64bbb2248a5845a2c60d81a30996a","analyzedAt":"2026-08-30T20:38:16.250Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}