resource.Builder should not alter error type from server

The current helpful message loses the error type, which means
resource.Builder consumers can't filter errors or have downstream logic.

If the error is a known type, only mutate the message, not the message
type.
This commit is contained in:
Clayton Coleman 2016-06-11 19:15:25 -04:00
parent 55dbcee4dc
commit b2f72e6b1b
No known key found for this signature in database
GPG Key ID: 3D16906B4F1C5CB3

View File

@ -50,6 +50,15 @@ func (r *Selector) Visit(fn VisitorFunc) error {
list, err := NewHelper(r.Client, r.Mapping).List(r.Namespace, r.ResourceMapping().GroupVersionKind.GroupVersion().String(), r.Selector, r.Export)
if err != nil {
if errors.IsBadRequest(err) || errors.IsNotFound(err) {
if se, ok := err.(*errors.StatusError); ok {
// modify the message without hiding this is an API error
if r.Selector.Empty() {
se.ErrStatus.Message = fmt.Sprintf("Unable to list %q: %v", r.Mapping.Resource, se.ErrStatus.Message)
} else {
se.ErrStatus.Message = fmt.Sprintf("Unable to find %q that match the selector %q: %v", r.Mapping.Resource, r.Selector, se.ErrStatus.Message)
}
return se
}
if r.Selector.Empty() {
return fmt.Errorf("Unable to list %q: %v", r.Mapping.Resource, err)
} else {