Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame^] | 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 storage |
| 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): |
| 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 | default: |
| 36 | return err |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // InterpretGetError converts a generic error on a retrieval |
| 41 | // operation into the appropriate API error. |
| 42 | func InterpretGetError(err error, qualifiedResource schema.GroupResource, name string) error { |
| 43 | switch { |
| 44 | case storage.IsNotFound(err): |
| 45 | return errors.NewNotFound(qualifiedResource, name) |
| 46 | case storage.IsUnreachable(err): |
| 47 | return errors.NewServerTimeout(qualifiedResource, "get", 2) // TODO: make configurable or handled at a higher level |
| 48 | case storage.IsInternalError(err): |
| 49 | return errors.NewInternalError(err) |
| 50 | default: |
| 51 | return err |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // InterpretCreateError converts a generic error on a create |
| 56 | // operation into the appropriate API error. |
| 57 | func InterpretCreateError(err error, qualifiedResource schema.GroupResource, name string) error { |
| 58 | switch { |
| 59 | case storage.IsNodeExist(err): |
| 60 | return errors.NewAlreadyExists(qualifiedResource, name) |
| 61 | case storage.IsUnreachable(err): |
| 62 | return errors.NewServerTimeout(qualifiedResource, "create", 2) // TODO: make configurable or handled at a higher level |
| 63 | case storage.IsInternalError(err): |
| 64 | return errors.NewInternalError(err) |
| 65 | default: |
| 66 | return err |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // InterpretUpdateError converts a generic error on an update |
| 71 | // operation into the appropriate API error. |
| 72 | func InterpretUpdateError(err error, qualifiedResource schema.GroupResource, name string) error { |
| 73 | switch { |
| 74 | case storage.IsConflict(err), storage.IsNodeExist(err), storage.IsInvalidObj(err): |
| 75 | return errors.NewConflict(qualifiedResource, name, err) |
| 76 | case storage.IsUnreachable(err): |
| 77 | return errors.NewServerTimeout(qualifiedResource, "update", 2) // TODO: make configurable or handled at a higher level |
| 78 | case storage.IsNotFound(err): |
| 79 | return errors.NewNotFound(qualifiedResource, name) |
| 80 | case storage.IsInternalError(err): |
| 81 | return errors.NewInternalError(err) |
| 82 | default: |
| 83 | return err |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // InterpretDeleteError converts a generic error on a delete |
| 88 | // operation into the appropriate API error. |
| 89 | func InterpretDeleteError(err error, qualifiedResource schema.GroupResource, name string) error { |
| 90 | switch { |
| 91 | case storage.IsNotFound(err): |
| 92 | return errors.NewNotFound(qualifiedResource, name) |
| 93 | case storage.IsUnreachable(err): |
| 94 | return errors.NewServerTimeout(qualifiedResource, "delete", 2) // TODO: make configurable or handled at a higher level |
| 95 | case storage.IsConflict(err), storage.IsNodeExist(err), storage.IsInvalidObj(err): |
| 96 | return errors.NewConflict(qualifiedResource, name, err) |
| 97 | case storage.IsInternalError(err): |
| 98 | return errors.NewInternalError(err) |
| 99 | default: |
| 100 | return err |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // InterpretWatchError converts a generic error on a watch |
| 105 | // operation into the appropriate API error. |
| 106 | func InterpretWatchError(err error, resource schema.GroupResource, name string) error { |
| 107 | switch { |
| 108 | case storage.IsInvalidError(err): |
| 109 | invalidError, _ := err.(storage.InvalidError) |
| 110 | return errors.NewInvalid(schema.GroupKind{Group: resource.Group, Kind: resource.Resource}, name, invalidError.Errs) |
| 111 | case storage.IsInternalError(err): |
| 112 | return errors.NewInternalError(err) |
| 113 | default: |
| 114 | return err |
| 115 | } |
| 116 | } |