| 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 |
// NewNotAcceptableError returns an error of NotAcceptable which contains specified string |
| 33 |
func NewNotAcceptableError(accepted []string) error { |
| 34 |
return errNotAcceptable{accepted} |
| 35 |
} |
| 36 |
|
| 37 |
func (e errNotAcceptable) Error() string { |
| 38 |
return fmt.Sprintf("only the following media types are accepted: %v", strings.Join(e.accepted, ", ")) |
| 39 |
} |
| 40 |
|
| 41 |
func (e errNotAcceptable) Status() metav1.Status { |
| 42 |
return metav1.Status{ |
| 43 |
Status: metav1.StatusFailure, |
| 44 |
Code: http.StatusNotAcceptable, |
| 45 |
Reason: metav1.StatusReasonNotAcceptable, |
| 46 |
Message: e.Error(), |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
// errNotAcceptableConversion indicates Accept negotiation has failed specifically |
| 51 |
// for a conversion to a known type. |
| 52 |
type errNotAcceptableConversion struct { |
| 53 |
target string |
| 54 |
accepted []string |
| 55 |
} |
| 56 |
|
| 57 |
// NewNotAcceptableConversionError returns an error indicating that the desired |
| 58 |
// API transformation to the target group version kind string is not accepted and |
| 59 |
// only the listed mime types are allowed. This is temporary while Table does not |
| 60 |
// yet support protobuf encoding. |
| 61 |
func NewNotAcceptableConversionError(target string, accepted []string) error { |
| 62 |
return errNotAcceptableConversion{target, accepted} |
| 63 |
} |
| 64 |
|
| 65 |
func (e errNotAcceptableConversion) Error() string { |
| 66 |
return fmt.Sprintf("only the following media types are accepted when converting to %s: %v", e.target, strings.Join(e.accepted, ", ")) |
| 67 |
} |
| 68 |
|
| 69 |
func (e errNotAcceptableConversion) Status() metav1.Status { |
| 70 |
return metav1.Status{ |
| 71 |
Status: metav1.StatusFailure, |
| 72 |
Code: http.StatusNotAcceptable, |
| 73 |
Reason: metav1.StatusReasonNotAcceptable, |
| 74 |
Message: e.Error(), |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
// errUnsupportedMediaType indicates Content-Type is not recognized |
| 79 |
type errUnsupportedMediaType struct { |
| 80 |
accepted []string |
| 81 |
} |
| 82 |
|
| 83 |
// NewUnsupportedMediaTypeError returns an error of UnsupportedMediaType which contains specified string |
| 84 |
func NewUnsupportedMediaTypeError(accepted []string) error { |
| 85 |
return errUnsupportedMediaType{accepted} |
| 86 |
} |
| 87 |
|
| 88 |
func (e errUnsupportedMediaType) Error() string { |
| 89 |
return fmt.Sprintf("the body of the request was in an unknown format - accepted media types include: %v", strings.Join(e.accepted, ", ")) |
| 90 |
} |
| 91 |
|
| 92 |
func (e errUnsupportedMediaType) Status() metav1.Status { |
| 93 |
return metav1.Status{ |
| 94 |
Status: metav1.StatusFailure, |
| 95 |
Code: http.StatusUnsupportedMediaType, |
| 96 |
Reason: metav1.StatusReasonUnsupportedMediaType, |
| 97 |
Message: e.Error(), |
| 98 |
} |
| 99 |
} |