mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +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)
|
handleErr("", DefaultErrorExitCode)
|
||||||
case kerrors.IsInvalid(err):
|
case kerrors.IsInvalid(err):
|
||||||
details := err.(*kerrors.StatusError).Status().Details
|
details := err.(*kerrors.StatusError).Status().Details
|
||||||
s := fmt.Sprintf("The %s %q is invalid", details.Kind, details.Name)
|
s := "The request is invalid"
|
||||||
if len(details.Kind) == 0 && len(details.Name) == 0 {
|
if details == nil {
|
||||||
s = "The request is invalid"
|
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 {
|
if len(details.Causes) > 0 {
|
||||||
errs := statusCausesToAggrError(details.Causes)
|
errs := statusCausesToAggrError(details.Causes)
|
||||||
|
@ -19,6 +19,7 @@ package util
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
@ -226,6 +227,16 @@ func TestCheckInvalidErr(t *testing.T) {
|
|||||||
"The Invalid4 \"invalidation\" is invalid: field4: Invalid value: \"multi4\": details\n",
|
"The Invalid4 \"invalidation\" is invalid: field4: Invalid value: \"multi4\": details\n",
|
||||||
DefaultErrorExitCode,
|
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