Merge pull request #65788 from tallclair/errors

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Cleanup apiserver errors

- delete unused methods (is this safe, or might they be used in another project outside kubernetes/kubernetes?)
- use the standard library `http.Error` function (functional change adds a newline)

```release-note
NONE
```

/sig api-machinery
/kind cleanup

Kubernetes-commit: 30e4f528ed30a70bdb0c14b5cfe49d00a78194c2
This commit is contained in:
Kubernetes Publisher 2018-08-21 09:18:47 -07:00
commit 3465165a50
2 changed files with 4 additions and 26 deletions

View File

@ -32,14 +32,6 @@ import (
// Avoid emitting errors that look like valid HTML. Quotes are okay. // Avoid emitting errors that look like valid HTML. Quotes are okay.
var sanitizer = strings.NewReplacer(`&`, "&amp;", `<`, "&lt;", `>`, "&gt;") var sanitizer = strings.NewReplacer(`&`, "&amp;", `<`, "&lt;", `>`, "&gt;")
// BadGatewayError renders a simple bad gateway error.
func BadGatewayError(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(http.StatusBadGateway)
fmt.Fprintf(w, "Bad Gateway: %q", sanitizer.Replace(req.RequestURI))
}
// Forbidden renders a simple forbidden error // Forbidden renders a simple forbidden error
func Forbidden(ctx context.Context, attributes authorizer.Attributes, w http.ResponseWriter, req *http.Request, reason string, s runtime.NegotiatedSerializer) { func Forbidden(ctx context.Context, attributes authorizer.Attributes, w http.ResponseWriter, req *http.Request, reason string, s runtime.NegotiatedSerializer) {
msg := sanitizer.Replace(forbiddenMessage(attributes)) msg := sanitizer.Replace(forbiddenMessage(attributes))
@ -80,15 +72,7 @@ func forbiddenMessage(attributes authorizer.Attributes) string {
// InternalError renders a simple internal error // InternalError renders a simple internal error
func InternalError(w http.ResponseWriter, req *http.Request, err error) { func InternalError(w http.ResponseWriter, req *http.Request, err error) {
w.Header().Set("Content-Type", "text/plain") http.Error(w, sanitizer.Replace(fmt.Sprintf("Internal Server Error: %q: %v", req.RequestURI, err)),
w.Header().Set("X-Content-Type-Options", "nosniff") http.StatusInternalServerError)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Internal Server Error: %q: %v", sanitizer.Replace(req.RequestURI), err)
utilruntime.HandleError(err) utilruntime.HandleError(err)
} }
// NotFound renders a simple not found error.
func NotFound(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "Not Found: %q", sanitizer.Replace(req.RequestURI))
}

View File

@ -32,8 +32,6 @@ import (
func TestErrors(t *testing.T) { func TestErrors(t *testing.T) {
internalError := errors.New("ARGH") internalError := errors.New("ARGH")
fns := map[string]func(http.ResponseWriter, *http.Request){ fns := map[string]func(http.ResponseWriter, *http.Request){
"BadGatewayError": BadGatewayError,
"NotFound": NotFound,
"InternalError": func(w http.ResponseWriter, req *http.Request) { "InternalError": func(w http.ResponseWriter, req *http.Request) {
InternalError(w, req, internalError) InternalError(w, req, internalError)
}, },
@ -43,12 +41,8 @@ func TestErrors(t *testing.T) {
uri string uri string
expected string expected string
}{ }{
{"BadGatewayError", "/get", `Bad Gateway: "/get"`}, {"InternalError", "/get", "Internal Server Error: \"/get\": ARGH\n"},
{"BadGatewayError", "/<script>", `Bad Gateway: "/&lt;script&gt;"`}, {"InternalError", "/<script>", "Internal Server Error: \"/&lt;script&gt;\": ARGH\n"},
{"NotFound", "/get", `Not Found: "/get"`},
{"NotFound", "/<script&>", `Not Found: "/&lt;script&amp;&gt;"`},
{"InternalError", "/get", `Internal Server Error: "/get": ARGH`},
{"InternalError", "/<script>", `Internal Server Error: "/&lt;script&gt;": ARGH`},
} }
for _, test := range cases { for _, test := range cases {
observer := httptest.NewRecorder() observer := httptest.NewRecorder()