blob: 93b17cfb097fc48940eee9949dabfee06df535e6 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2016 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package negotiation
18
19import (
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
28type errNotAcceptable struct {
29 accepted []string
30}
31
32func NewNotAcceptableError(accepted []string) error {
33 return errNotAcceptable{accepted}
34}
35
36func (e errNotAcceptable) Error() string {
37 return fmt.Sprintf("only the following media types are accepted: %v", strings.Join(e.accepted, ", "))
38}
39
40func (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
50type errUnsupportedMediaType struct {
51 accepted []string
52}
53
54func NewUnsupportedMediaTypeError(accepted []string) error {
55 return errUnsupportedMediaType{accepted}
56}
57
58func (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
62func (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}