mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
Merge pull request #82914 from Random-Liu/fix-kubectl-panic
Fix kubectl panic when handling invalid error.
This commit is contained in:
commit
08ef34a2b0
@ -131,9 +131,13 @@ func checkErr(err error, handleErr func(string, int)) {
|
||||
handleErr("", DefaultErrorExitCode)
|
||||
case kerrors.IsInvalid(err):
|
||||
details := err.(*kerrors.StatusError).Status().Details
|
||||
s := fmt.Sprintf("The %s %q is invalid", details.Kind, details.Name)
|
||||
if len(details.Kind) == 0 && len(details.Name) == 0 {
|
||||
s = "The request is invalid"
|
||||
s := "The request is invalid"
|
||||
if details == nil {
|
||||
handleErr(s, DefaultErrorExitCode)
|
||||
return
|
||||
}
|
||||
if len(details.Kind) != 0 || len(details.Name) != 0 {
|
||||
s = fmt.Sprintf("The %s %q is invalid", details.Kind, details.Name)
|
||||
}
|
||||
if len(details.Causes) > 0 {
|
||||
errs := statusCausesToAggrError(details.Causes)
|
||||
|
@ -19,6 +19,7 @@ package util
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"syscall"
|
||||
@ -226,6 +227,16 @@ func TestCheckInvalidErr(t *testing.T) {
|
||||
"The Invalid4 \"invalidation\" is invalid: field4: Invalid value: \"multi4\": details\n",
|
||||
DefaultErrorExitCode,
|
||||
},
|
||||
{
|
||||
&errors.StatusError{metav1.Status{
|
||||
Status: metav1.StatusFailure,
|
||||
Code: http.StatusUnprocessableEntity,
|
||||
Reason: metav1.StatusReasonInvalid,
|
||||
// Details is nil.
|
||||
}},
|
||||
"The request is invalid",
|
||||
DefaultErrorExitCode,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user