Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame^] | 1 | /* |
| 2 | Copyright 2016 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 negotiation |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "net/http" |
| 22 | "strings" |
| 23 | |
| 24 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | ) |
| 26 | |
| 27 | // errNotAcceptable indicates Accept negotiation has failed |
| 28 | type errNotAcceptable struct { |
| 29 | accepted []string |
| 30 | } |
| 31 | |
| 32 | func NewNotAcceptableError(accepted []string) error { |
| 33 | return errNotAcceptable{accepted} |
| 34 | } |
| 35 | |
| 36 | func (e errNotAcceptable) Error() string { |
| 37 | return fmt.Sprintf("only the following media types are accepted: %v", strings.Join(e.accepted, ", ")) |
| 38 | } |
| 39 | |
| 40 | func (e errNotAcceptable) Status() metav1.Status { |
| 41 | return metav1.Status{ |
| 42 | Status: metav1.StatusFailure, |
| 43 | Code: http.StatusNotAcceptable, |
| 44 | Reason: metav1.StatusReasonNotAcceptable, |
| 45 | Message: e.Error(), |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // errUnsupportedMediaType indicates Content-Type is not recognized |
| 50 | type errUnsupportedMediaType struct { |
| 51 | accepted []string |
| 52 | } |
| 53 | |
| 54 | func NewUnsupportedMediaTypeError(accepted []string) error { |
| 55 | return errUnsupportedMediaType{accepted} |
| 56 | } |
| 57 | |
| 58 | func (e errUnsupportedMediaType) Error() string { |
| 59 | return fmt.Sprintf("the body of the request was in an unknown format - accepted media types include: %v", strings.Join(e.accepted, ", ")) |
| 60 | } |
| 61 | |
| 62 | func (e errUnsupportedMediaType) Status() metav1.Status { |
| 63 | return metav1.Status{ |
| 64 | Status: metav1.StatusFailure, |
| 65 | Code: http.StatusUnsupportedMediaType, |
| 66 | Reason: metav1.StatusReasonUnsupportedMediaType, |
| 67 | Message: e.Error(), |
| 68 | } |
| 69 | } |