mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-26 21:17:23 +00:00
Use errors.As to detect wrapping in StatusCause
Other functions in this package handle wrapped errors since 3244350046
.
This commit is contained in:
parent
fbb5717279
commit
d404991eff
@ -87,21 +87,21 @@ func (e *StatusError) DebugError() (string, []interface{}) {
|
|||||||
|
|
||||||
// HasStatusCause returns true if the provided error has a details cause
|
// HasStatusCause returns true if the provided error has a details cause
|
||||||
// with the provided type name.
|
// with the provided type name.
|
||||||
|
// It supports wrapped errors and returns false when the error is nil.
|
||||||
func HasStatusCause(err error, name metav1.CauseType) bool {
|
func HasStatusCause(err error, name metav1.CauseType) bool {
|
||||||
_, ok := StatusCause(err, name)
|
_, ok := StatusCause(err, name)
|
||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|
||||||
// StatusCause returns the named cause from the provided error if it exists and
|
// StatusCause returns the named cause from the provided error if it exists and
|
||||||
// the error is of the type APIStatus. Otherwise it returns false.
|
// the error unwraps to the type APIStatus. Otherwise it returns false.
|
||||||
func StatusCause(err error, name metav1.CauseType) (metav1.StatusCause, bool) {
|
func StatusCause(err error, name metav1.CauseType) (metav1.StatusCause, bool) {
|
||||||
apierr, ok := err.(APIStatus)
|
var status APIStatus
|
||||||
if !ok || apierr == nil || apierr.Status().Details == nil {
|
if errors.As(err, &status) && status.Status().Details != nil {
|
||||||
return metav1.StatusCause{}, false
|
for _, cause := range status.Status().Details.Causes {
|
||||||
}
|
if cause.Type == name {
|
||||||
for _, cause := range apierr.Status().Details.Causes {
|
return cause, true
|
||||||
if cause.Type == name {
|
}
|
||||||
return cause, true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return metav1.StatusCause{}, false
|
return metav1.StatusCause{}, false
|
||||||
|
@ -625,3 +625,35 @@ func TestIsErrorTypesByReasonAndCode(t *testing.T) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStatusCauseSupportsWrappedErrors(t *testing.T) {
|
||||||
|
err := &StatusError{ErrStatus: metav1.Status{
|
||||||
|
Details: &metav1.StatusDetails{
|
||||||
|
Causes: []metav1.StatusCause{{Type: "SomeCause"}},
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
|
||||||
|
if cause, ok := StatusCause(nil, "SomeCause"); ok {
|
||||||
|
t.Errorf("expected no cause for nil, got %v: %#v", ok, cause)
|
||||||
|
}
|
||||||
|
if cause, ok := StatusCause(errors.New("boom"), "SomeCause"); ok {
|
||||||
|
t.Errorf("expected no cause for wrong type, got %v: %#v", ok, cause)
|
||||||
|
}
|
||||||
|
|
||||||
|
if cause, ok := StatusCause(err, "Other"); ok {
|
||||||
|
t.Errorf("expected no cause for wrong name, got %v: %#v", ok, cause)
|
||||||
|
}
|
||||||
|
if cause, ok := StatusCause(err, "SomeCause"); !ok || cause != err.ErrStatus.Details.Causes[0] {
|
||||||
|
t.Errorf("expected cause, got %v: %#v", ok, cause)
|
||||||
|
}
|
||||||
|
|
||||||
|
wrapped := fmt.Errorf("once: %w", err)
|
||||||
|
if cause, ok := StatusCause(wrapped, "SomeCause"); !ok || cause != err.ErrStatus.Details.Causes[0] {
|
||||||
|
t.Errorf("expected cause when wrapped, got %v: %#v", ok, cause)
|
||||||
|
}
|
||||||
|
|
||||||
|
nested := fmt.Errorf("twice: %w", wrapped)
|
||||||
|
if cause, ok := StatusCause(nested, "SomeCause"); !ok || cause != err.ErrStatus.Details.Causes[0] {
|
||||||
|
t.Errorf("expected cause when nested, got %v: %#v", ok, cause)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user