{"record":{"id":"71cad7dcedef877d","repo":"derailed/k9s","slug":"set-current-context-failed-w","errorCode":null,"errorMessage":"set current context failed. %w","messagePattern":"set current context failed\\. %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/config/config.go","lineNumber":157,"sourceCode":"\tif ns == \"\" {\n\t\tns = client.DefaultNamespace\n\t}\n\tif err := c.SetActiveNamespace(ns); err != nil {\n\t\treturn err\n\t}\n\n\treturn data.EnsureDirPath(c.K9s.AppScreenDumpDir(), data.DefaultDirMod)\n}\n\n// Reset resets the context to the new current context/cluster.\nfunc (c *Config) Reset() {\n\tc.K9s.Reset()\n}\n\nfunc (c *Config) ActivateContext(n string) (*data.Context, error) {\n\tct, err := c.K9s.ActivateContext(n)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"set current context failed. %w\", err)\n\t}\n\n\treturn ct, nil\n}\n\n// CurrentContext fetch the configuration active context.\nfunc (c *Config) CurrentContext() (*data.Context, error) {\n\treturn c.K9s.ActiveContext()\n}\n\n// ActiveNamespace returns the active namespace in the current context.\n// If none found return the empty ns.\nfunc (c *Config) ActiveNamespace() string {\n\tns, err := c.K9s.ActiveContextNamespace()\n\tif err != nil {\n\t\tslog.Error(\"Unable to assert active namespace. Using default\", slogs.Error, err)\n\t\tns = client.DefaultNamespace\n\t}","sourceCodeStart":139,"sourceCodeEnd":175,"githubUrl":"https://github.com/derailed/k9s/blob/2d3ccc6ba2ce98c3781bfc441bb3e884f072774f/internal/config/config.go#L139-L175","documentation":"Returned by Config.ActivateContext (internal/config/config.go:157), the top-level facade that delegates to K9s.ActivateContext. Any failure in the underlying activation — context lookup miss, per-context config load error, proxy connectivity failure — is re-wrapped as 'set current context failed. %w'. Expect it stacked: this message wraps error chains rooted in internal/config/k9s.go and internal/client/config.go.","triggerScenarios":"Any programmatic call to Config.ActivateContext(name) where name is absent from the kubeconfig, the context's k9s yaml is invalid, or the context's proxy is unreachable. In the k9s UI this path runs on :ctx switching.","commonSituations":"Programmatic embedders of k9s config switching contexts at runtime; UI context switch to a context whose config was never initialized or was corrupted; switching after the kubeconfig changed on disk.","solutions":["Unwrap the chain (fmt.Printf(\"%+v\", err) or errors.Unwrap loop) and fix the deepest cause — this wrapper itself carries no extra information","Confirm the context exists in the loaded kubeconfig (kubectl config get-contexts)","Validate or delete the per-context yaml under the k9s config dir","If the cause is proxy connectivity, fix the address or remove the proxy block"],"exampleFix":"// before\nct, err := appCfg.ActivateContext(\"prod\")\nif err != nil {\n\tpanic(err) // opaque 'set current context failed. ...'\n}\n\n// after: surface the root cause\nct, err := appCfg.ActivateContext(\"prod\")\nif err != nil {\n\tvar root error = err\n\tfor errors.Unwrap(root) != nil {\n\t\troot = errors.Unwrap(root)\n\t}\n\tlog.Printf(\"context switch failed, root cause: %v\", root)\n\treturn err\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"ct, err := appCfg.ActivateContext(name)\nif err != nil {\n\t// strip wrappers: 'set current context failed.' adds no information\n\tlog.Printf(\"activation failed: root=%v\", errors.Unwrap(errors.Unwrap(err)))\n\t// fall back to a known-good default context if the app must stay up\n\tct, err = appCfg.ActivateContext(fallbackContext)\n\tif err != nil {\n\t\treturn err\n\t}\n}","preventionTips":["Never branch on the wrapper text — unwrap to the root cause before deciding remediation","Maintain a fallback context name so context-switch failures degrade instead of crashing","Log the full chain (%+v) in bug reports; single-line messages lose the diagnosis"],"tags":["kubernetes","k9s","context","activation","wrapped-error"],"backgroundTag":null,"analyzedSha":"2d3ccc6ba2ce98c3781bfc441bb3e884f072774f","analyzedAt":"2026-08-15T16:09:14.432Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}