| 1 |
/* |
| 2 |
Copyright 2014 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package errors |
| 18 |
|
| 19 |
import ( |
| 20 |
"k8s.io/apimachinery/pkg/api/errors" |
| 21 |
"k8s.io/apimachinery/pkg/runtime/schema" |
| 22 |
"k8s.io/apiserver/pkg/storage" |
| 23 |
) |
| 24 |
|
| 25 |
// InterpretListError converts a generic error on a retrieval |
| 26 |
// operation into the appropriate API error. |
| 27 |
func InterpretListError(err error, qualifiedResource schema.GroupResource) error { |
| 28 |
switch { |
| 29 |
case storage.IsNotFound(err): |
| 30 |
return errors.NewNotFound(qualifiedResource, "") |
| 31 |
case storage.IsUnreachable(err), storage.IsRequestTimeout(err): |
| 32 |
return errors.NewServerTimeout(qualifiedResource, "list", 2) // TODO: make configurable or handled at a higher level |
| 33 |
case storage.IsInternalError(err): |
| 34 |
return errors.NewInternalError(err) |
| 35 |
case storage.IsCorruptObject(err): |
| 36 |
return errors.NewInternalError(err) |
| 37 |
default: |
| 38 |
return err |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
// InterpretGetError converts a generic error on a retrieval |
| 43 |
// operation into the appropriate API error. |
| 44 |
func InterpretGetError(err error, qualifiedResource schema.GroupResource, name string) error { |
| 45 |
switch { |
| 46 |
case storage.IsNotFound(err): |
| 47 |
return errors.NewNotFound(qualifiedResource, name) |
| 48 |
case storage.IsUnreachable(err): |
| 49 |
return errors.NewServerTimeout(qualifiedResource, "get", 2) // TODO: make configurable or handled at a higher level |
| 50 |
case storage.IsInternalError(err): |
| 51 |
return errors.NewInternalError(err) |
| 52 |
case storage.IsCorruptObject(err): |
| 53 |
return errors.NewInternalError(err) |
| 54 |
default: |
| 55 |
return err |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
// InterpretCreateError converts a generic error on a create |
| 60 |
// operation into the appropriate API error. |
| 61 |
func InterpretCreateError(err error, qualifiedResource schema.GroupResource, name string) error { |
| 62 |
switch { |
| 63 |
case storage.IsExist(err): |
| 64 |
return errors.NewAlreadyExists(qualifiedResource, name) |
| 65 |
case storage.IsUnreachable(err): |
| 66 |
return errors.NewServerTimeout(qualifiedResource, "create", 2) // TODO: make configurable or handled at a higher level |
| 67 |
case storage.IsInternalError(err): |
| 68 |
return errors.NewInternalError(err) |
| 69 |
case storage.IsCorruptObject(err): |
| 70 |
return errors.NewInternalError(err) |
| 71 |
default: |
| 72 |
return err |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
// InterpretUpdateError converts a generic error on an update |
| 77 |
// operation into the appropriate API error. |
| 78 |
func InterpretUpdateError(err error, qualifiedResource schema.GroupResource, name string) error { |
| 79 |
switch { |
| 80 |
case storage.IsConflict(err), storage.IsExist(err), storage.IsInvalidObj(err): |
| 81 |
return errors.NewConflict(qualifiedResource, name, err) |
| 82 |
case storage.IsUnreachable(err): |
| 83 |
return errors.NewServerTimeout(qualifiedResource, "update", 2) // TODO: make configurable or handled at a higher level |
| 84 |
case storage.IsNotFound(err): |
| 85 |
return errors.NewNotFound(qualifiedResource, name) |
| 86 |
case storage.IsInternalError(err): |
| 87 |
return errors.NewInternalError(err) |
| 88 |
case storage.IsCorruptObject(err): |
| 89 |
return errors.NewInternalError(err) |
| 90 |
default: |
| 91 |
return err |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
// InterpretDeleteError converts a generic error on a delete |
| 96 |
// operation into the appropriate API error. |
| 97 |
func InterpretDeleteError(err error, qualifiedResource schema.GroupResource, name string) error { |
| 98 |
switch { |
| 99 |
case storage.IsNotFound(err): |
| 100 |
return errors.NewNotFound(qualifiedResource, name) |
| 101 |
case storage.IsUnreachable(err): |
| 102 |
return errors.NewServerTimeout(qualifiedResource, "delete", 2) // TODO: make configurable or handled at a higher level |
| 103 |
case storage.IsConflict(err), storage.IsExist(err), storage.IsInvalidObj(err): |
| 104 |
return errors.NewConflict(qualifiedResource, name, err) |
| 105 |
case storage.IsInternalError(err): |
| 106 |
return errors.NewInternalError(err) |
| 107 |
case storage.IsCorruptObject(err): |
| 108 |
return errors.NewInternalError(err) |
| 109 |
default: |
| 110 |
return err |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
// InterpretWatchError converts a generic error on a watch |
| 115 |
// operation into the appropriate API error. |
| 116 |
func InterpretWatchError(err error, resource schema.GroupResource, name string) error { |
| 117 |
switch { |
| 118 |
case storage.IsInvalidError(err): |
| 119 |
invalidError, _ := err.(storage.InvalidError) |
| 120 |
return errors.NewInvalid(schema.GroupKind{Group: resource.Group, Kind: resource.Resource}, name, invalidError.Errs) |
| 121 |
case storage.IsInternalError(err): |
| 122 |
return errors.NewInternalError(err) |
| 123 |
case storage.IsCorruptObject(err): |
| 124 |
return errors.NewInternalError(err) |
| 125 |
default: |
| 126 |
return err |
| 127 |
} |
| 128 |
} |