mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-08-08 23:37:11 +00:00
Don't panic in case of an unknown API error code
This commit is contained in:
@@ -107,8 +107,8 @@ func (e *Error) ErrorBody() string {
|
||||
s = fmt.Sprintf("%s: %s", e.Type, valstr)
|
||||
}
|
||||
default:
|
||||
// NOTE: This panics if we find a code that truly is not supported.
|
||||
s = e.Type.String()
|
||||
internal := InternalError(nil, fmt.Errorf("unhandled error code: %s: please report this", e.Type))
|
||||
s = internal.ErrorBody()
|
||||
}
|
||||
if len(e.Detail) != 0 {
|
||||
s += fmt.Sprintf(": %s", e.Detail)
|
||||
@@ -195,7 +195,7 @@ func (t ErrorType) String() string {
|
||||
case ErrorTypeTypeInvalid:
|
||||
return "Invalid value"
|
||||
default:
|
||||
panic(fmt.Sprintf("unrecognized validation error: %q", string(t)))
|
||||
return fmt.Sprintf("<unknown error %q>", string(t))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -630,6 +630,17 @@ func TestErrorFormatting(t *testing.T) {
|
||||
CoveredByDeclarative: true,
|
||||
},
|
||||
expect: `path.to.field: Invalid value: field.SelfMarshalerNonStringer{S:"visible"}: the details`,
|
||||
}, {
|
||||
name: "unknown error type",
|
||||
input: &Error{
|
||||
Type: "not real",
|
||||
Field: "path.to.field",
|
||||
BadValue: SelfMarshalerNonStringer{"visible"},
|
||||
Detail: "the details",
|
||||
Origin: "theOrigin",
|
||||
CoveredByDeclarative: true,
|
||||
},
|
||||
expect: `path.to.field: Internal error: unhandled error code: <unknown error "not real">: please report this: the details`,
|
||||
}}
|
||||
|
||||
for _, tc := range cases {
|
||||
|
||||
Reference in New Issue
Block a user