{"record":{"id":"285cc59a1dbf4251","repo":"ahmetb/kubectx","slug":"failed-to-check-context-w-285cc5","errorCode":null,"errorMessage":"failed to check context: %w","messagePattern":"failed to check context: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/kubectx/switch.go","lineNumber":70,"sourceCode":"func switchContext(name string) (string, error) {\n\tprevCtxFile, err := kubectxPrevCtxFile()\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to determine state file: %w\", err)\n\t}\n\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\tprev, err := kc.GetCurrentContext()\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to get current context: %w\", err)\n\t}\n\texists, err := kc.ContextExists(name)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to check context: %w\", err)\n\t}\n\tif !exists {\n\t\treturn \"\", fmt.Errorf(\"no context exists with the name: \\\"%s\\\"\", name)\n\t}\n\tif err := kc.ModifyCurrentContext(name); err != nil {\n\t\treturn \"\", err\n\t}\n\tif err := kc.Save(); err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to save kubeconfig: %w\", err)\n\t}\n\n\tif prev != name {\n\t\tif err := writeLastContext(prevCtxFile, prev); err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"failed to save previous context name: %w\", err)\n\t\t}\n\t}\n\treturn name, nil\n}","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/ahmetb/kubectx/blob/12ad6fb22e8c546ee2b54e7de38aa51c906832f7/cmd/kubectx/switch.go#L52-L88","documentation":"ContextExists(name) errored while validating the requested target context. The contexts-list lookup itself failed (kyaml traversal error), not a clean not-found; the input at fault is the contexts field of a kubeconfig file.","triggerScenarios":"Thrown at cmd/kubectx/switch.go:70 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Validate the contexts list YAML in each KUBECONFIG file","Identify the broken file with kubectl config get-contexts","Fix or remove the malformed file and retry"],"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"}