{"record":{"id":"2ae61aa33492aaa8","repo":"ahmetb/kubectx","slug":"kubeconfig-error-w-2ae61a","errorCode":null,"errorMessage":"kubeconfig error: %w","messagePattern":"kubeconfig error: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/kubens/switch.go","lineNumber":40,"sourceCode":"\t\"os\"\n\n\terrors2 \"k8s.io/apimachinery/pkg/api/errors\"\n\tmetav1 \"k8s.io/apimachinery/pkg/apis/meta/v1\"\n\n\t\"github.com/ahmetb/kubectx/internal/kubeconfig\"\n\t\"github.com/ahmetb/kubectx/internal/printer\"\n)\n\ntype SwitchOp struct {\n\tTarget string // '-' for back and forth, or NAME\n\tForce  bool   // force switch even if the namespace doesn't exist\n}\n\nfunc (s SwitchOp) Run(_, stderr io.Writer) error {\n\tkc := new(kubeconfig.Kubeconfig).WithLoader(kubeconfig.DefaultLoader)\n\tdefer kc.Close()\n\tif err := kc.Parse(); err != nil {\n\t\treturn fmt.Errorf(\"kubeconfig error: %w\", err)\n\t}\n\n\ttoNS, err := switchNamespace(kc, s.Target, s.Force)\n\tif err != nil {\n\t\treturn err\n\t}\n\terr = printer.Success(stderr, \"Active namespace is \\\"%s\\\"\", printer.SuccessColor.Sprint(toNS))\n\treturn err\n}\n\nfunc switchNamespace(kc *kubeconfig.Kubeconfig, ns string, force bool) (string, error) {\n\tctx, err := kc.GetCurrentContext()\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to get current context: %w\", err)\n\t}\n\tif ctx == \"\" {\n\t\treturn \"\", errors.New(\"current-context is not set\")\n\t}","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/ahmetb/kubectx/blob/12ad6fb22e8c546ee2b54e7de38aa51c906832f7/cmd/kubens/switch.go#L22-L58","documentation":"Generic kubeconfig parse guard at the top of kubens SwitchOp.Run: loading or decoding the kubeconfig failed before the namespace switch could proceed. The wrapped error indicates whether files were unreadable or contained invalid YAML.","triggerScenarios":"Thrown at cmd/kubens/switch.go:40 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Verify KUBECONFIG paths are readable","Lint the YAML of each kubeconfig file","Repair or remove the broken file and retry the namespace switch"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"12ad6fb22e8c546ee2b54e7de38aa51c906832f7","analyzedAt":"2026-09-02T12:23:10.107Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T16:17:10.729Z"}