mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-28 14:07:14 +00:00
Updated error handling example based on PR feedback
This commit is contained in:
parent
8ca1732023
commit
601ab10dbd
@ -45,10 +45,14 @@ func main() {
|
|||||||
}
|
}
|
||||||
fmt.Printf("There are %d pods in the cluster\n", len(pods.Items))
|
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{})
|
_, 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")
|
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 {
|
} else if err != nil {
|
||||||
panic(err.Error())
|
panic(err.Error())
|
||||||
} else {
|
} else {
|
||||||
|
@ -59,10 +59,14 @@ func main() {
|
|||||||
}
|
}
|
||||||
fmt.Printf("There are %d pods in the cluster\n", len(pods.Items))
|
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{})
|
_, 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")
|
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 {
|
} else if err != nil {
|
||||||
panic(err.Error())
|
panic(err.Error())
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user