pkg/apiserver: use httpError in handlers.go

This commit is contained in:
Xiang Li 2015-04-21 23:04:32 -07:00
parent 0035781c50
commit 405ebf4b1e

View File

@ -104,8 +104,7 @@ func RateLimit(rl util.RateLimiter, handler http.Handler) http.Handler {
func tooManyRequests(w http.ResponseWriter) { func tooManyRequests(w http.ResponseWriter) {
// Return a 429 status indicating "Too Many Requests" // Return a 429 status indicating "Too Many Requests"
w.Header().Set("Retry-After", RetryAfter) w.Header().Set("Retry-After", RetryAfter)
w.WriteHeader(errors.StatusTooManyRequests) http.Error(w, "Too many requests, please try again later.", errors.StatusTooManyRequests)
fmt.Fprintf(w, "Too many requests, please try again later.")
} }
// RecoverPanics wraps an http Handler to recover and log panics. // RecoverPanics wraps an http Handler to recover and log panics.
@ -113,8 +112,7 @@ func RecoverPanics(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
defer func() { defer func() {
if x := recover(); x != nil { if x := recover(); x != nil {
w.WriteHeader(http.StatusInternalServerError) http.Error(w, "apis panic. Look in log for details.", http.StatusInternalServerError)
fmt.Fprint(w, "apis panic. Look in log for details.")
glog.Infof("APIServer panic'd on %v %v: %v\n%s\n", req.Method, req.RequestURI, x, debug.Stack()) glog.Infof("APIServer panic'd on %v %v: %v\n%s\n", req.Method, req.RequestURI, x, debug.Stack())
} }
}() }()