From ecd7406c9fc6d86f8daef0887e018fa0959435f7 Mon Sep 17 00:00:00 2001 From: Marc Sluiter Date: Mon, 29 May 2017 23:21:23 +0200 Subject: [PATCH] Updated error handling example based on PR feedback Kubernetes-commit: 601ab10dbd08debca4b8a835f8dea00a26666060 --- examples/in-cluster-client-configuration/main.go | 8 ++++++-- examples/out-of-cluster-client-configuration/main.go | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/examples/in-cluster-client-configuration/main.go b/examples/in-cluster-client-configuration/main.go index 1617b383..ee996a74 100644 --- a/examples/in-cluster-client-configuration/main.go +++ b/examples/in-cluster-client-configuration/main.go @@ -45,10 +45,14 @@ func main() { } fmt.Printf("There are %d pods in the cluster\n", len(pods.Items)) - // Example for handling status errors + // Examples for error handling: + // - Use helper functions like e.g. errors.IsNotFound() + // - And/or cast to StatusError and use its properties like e.g. ErrStatus.Message _, err = clientset.CoreV1().Pods("").Get("ExamplePodName", metav1.GetOptions{}) - if statusError, isStatus := err.(*errors.StatusError); isStatus && statusError.Status().Reason == metav1.StatusReasonNotFound { + if errors.IsNotFound(err) { fmt.Printf("Pod not found\n") + } else if statusError, isStatus := err.(*errors.StatusError); isStatus { + fmt.Printf("Error getting pod %v\n", statusError.ErrStatus.Message) } else if err != nil { panic(err.Error()) } else { diff --git a/examples/out-of-cluster-client-configuration/main.go b/examples/out-of-cluster-client-configuration/main.go index 484accfe..5d0f619e 100644 --- a/examples/out-of-cluster-client-configuration/main.go +++ b/examples/out-of-cluster-client-configuration/main.go @@ -59,10 +59,14 @@ func main() { } fmt.Printf("There are %d pods in the cluster\n", len(pods.Items)) - // Example for handling status errors + // Examples for error handling: + // - Use helper functions like e.g. errors.IsNotFound() + // - And/or cast to StatusError and use its properties like e.g. ErrStatus.Message _, err = clientset.CoreV1().Pods("").Get("ExamplePodName", metav1.GetOptions{}) - if statusError, isStatus := err.(*errors.StatusError); isStatus && statusError.Status().Reason == metav1.StatusReasonNotFound { + if errors.IsNotFound(err) { fmt.Printf("Pod not found\n") + } else if statusError, isStatus := err.(*errors.StatusError); isStatus { + fmt.Printf("Error getting pod %v\n", statusError.ErrStatus.Message) } else if err != nil { panic(err.Error()) } else {