{"record":{"id":"41ff0f020933404f","repo":"kataras/iris","slug":"iris-switch-hosts-invalid-target-type-t","errorCode":null,"errorMessage":"iris: switch: hosts: invalid target type: %T","messagePattern":"iris: switch: hosts: invalid target type: %T","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"apps/switch_hosts.go","lineNumber":114,"sourceCode":"\t\t// using that app.\n\t\tif targetApp, ok := context.GetApplication(target); ok {\n\t\t\t// It's always iris.Application so we are totally safe here.\n\t\t\treturn targetApp.(*iris.Application)\n\t\t}\n\t\t// If it's a real host, warn the user of invalid input.\n\t\tu, err := url.Parse(target)\n\t\tif err == nil && u.IsAbs() {\n\t\t\t// remember, we redirect hosts, not full URLs here.\n\t\t\tpanic(fmt.Sprintf(`iris: switch: hosts: invalid target host: \"%s\"`, target))\n\t\t}\n\n\t\tif regex := regexp.MustCompile(host.Pattern); regex.MatchString(target) {\n\t\t\tpanic(fmt.Sprintf(`iris: switch: hosts: loop detected between expression: \"%s\" and target host: \"%s\"`, host.Pattern, host.Target))\n\t\t}\n\n\t\treturn newHostRedirectApp(target, HostsRedirectCode)\n\tdefault:\n\t\tpanic(fmt.Sprintf(\"iris: switch: hosts: invalid target type: %T\", target))\n\t}\n}\n\nfunc hostFilter(expr string) iris.Filter {\n\tregex := regexp.MustCompile(expr)\n\treturn func(ctx iris.Context) bool {\n\t\treturn regex.MatchString(ctx.Host())\n\t}\n}\n\n// HostsRedirectCode is the default status code is used\n// to redirect a matching host to a url.\nvar HostsRedirectCode = iris.StatusMovedPermanently\n\nfunc newHostRedirectApp(targetHost string, code int) *iris.Application {\n\tapp := iris.New()\n\tapp.Downgrade(func(w http.ResponseWriter, r *http.Request) {\n\t\tif targetHost == context.GetHost(r) {","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/kataras/iris/blob/7bedaf55a0b64bbb2248a5845a2c60d81a30996a/apps/switch_hosts.go#L96-L132","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Make the switch case target a string host expression (e.g. \"example.com\" or a regex-compatible host pattern).","If a redirect is intended, use the supported redirect target constructor (newHostRedirectApp / HostsRedirectCode path) rather than a raw custom type.","Inspect the %T output in the panic message to identify the actual Go type being passed and fix the value's origin."],"exampleFix":"// before\ncases := iris.SwitchCases{\n  {Case: \"old.example.com\", Target: 301}, // int target\n}\n// after\ncases := iris.SwitchCases{\n  {Case: \"old.example.com\", Target: \"https://new.example.com\"},\n}","handlingStrategy":"validation","validationCode":"// before registering switch cases\nfor _, c := range cases {\n  if _, ok := c.Target.(string); !ok {\n    panic(fmt.Sprintf(\"case %q target must be a string, got %T\", c.Case, c.Target))\n  }\n}","typeGuard":"func isValidHostTarget(t any) bool {\n  switch t.(type) {\n  case string:\n    return true\n  default:\n    return false\n  }\n}","tryCatchPattern":null,"preventionTips":["Keep switch case targets as plain string host expressions","Use defer/recover around dynamic case construction in tests","Log the %T of targets during development"],"tags":["go","panic","type-mismatch","routing"],"backgroundTag":"invalid-switch-target-type","analyzedSha":"7bedaf55a0b64bbb2248a5845a2c60d81a30996a","analyzedAt":"2026-08-30T20:38:16.250Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}