From 6fa8f7763841460c1dd2d2b22b5dddd96ac00250 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Mon, 10 Nov 2014 15:56:42 -0800 Subject: [PATCH 1/2] proxy: return JSON errors --- proxy/reverse.go | 15 +++++++++++---- proxy/reverse_test.go | 4 ++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/proxy/reverse.go b/proxy/reverse.go index 2dfb313f0..e1f9328a1 100644 --- a/proxy/reverse.go +++ b/proxy/reverse.go @@ -17,12 +17,15 @@ package proxy import ( + "fmt" "io" "log" "net" "net/http" "net/url" "strings" + + "github.com/coreos/etcd/etcdserver/etcdhttp/httptypes" ) // Hop-by-hop headers. These are removed when sent to the backend. @@ -64,8 +67,10 @@ func (p *reverseProxy) ServeHTTP(rw http.ResponseWriter, clientreq *http.Request endpoints := p.director.endpoints() if len(endpoints) == 0 { - log.Printf("proxy: zero endpoints currently available") - rw.WriteHeader(http.StatusServiceUnavailable) + msg := "proxy: zero endpoints currently available" + log.Printf(msg) + e := httptypes.NewHTTPError(http.StatusServiceUnavailable, msg) + e.WriteTo(rw) return } @@ -86,8 +91,10 @@ func (p *reverseProxy) ServeHTTP(rw http.ResponseWriter, clientreq *http.Request } if res == nil { - log.Printf("proxy: unable to get response from %d endpoint(s)", len(endpoints)) - rw.WriteHeader(http.StatusBadGateway) + msg := fmt.Sprintf("proxy: unable to get response from %d endpoint(s)", len(endpoints)) + log.Printf(msg) + e := httptypes.NewHTTPError(http.StatusBadGateway, msg) + e.WriteTo(rw) return } diff --git a/proxy/reverse_test.go b/proxy/reverse_test.go index 0a74dd8a7..499b8d879 100644 --- a/proxy/reverse_test.go +++ b/proxy/reverse_test.go @@ -70,6 +70,7 @@ func TestReverseProxyServe(t *testing.T) { res: &http.Response{ StatusCode: http.StatusCreated, Body: ioutil.NopCloser(&bytes.Reader{}), + Header: map[string][]string{"Content-Type": []string{"application/json"}}, }, }, want: http.StatusCreated, @@ -89,6 +90,9 @@ func TestReverseProxyServe(t *testing.T) { if rr.Code != tt.want { t.Errorf("#%d: unexpected HTTP status code: want = %d, got = %d", i, tt.want, rr.Code) } + if gct := rr.Header().Get("Content-Type"); gct != "application/json" { + t.Errorf("#%d: Content-Type = %s, want %s", i, gct, "application/json") + } } } From 424377f859f6a28083bfe5b0c8eb3baec2b377b9 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Mon, 10 Nov 2014 16:37:15 -0800 Subject: [PATCH 2/2] proxy: add a todo for logging --- proxy/reverse.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/proxy/reverse.go b/proxy/reverse.go index e1f9328a1..c9addd2ed 100644 --- a/proxy/reverse.go +++ b/proxy/reverse.go @@ -68,6 +68,7 @@ func (p *reverseProxy) ServeHTTP(rw http.ResponseWriter, clientreq *http.Request endpoints := p.director.endpoints() if len(endpoints) == 0 { msg := "proxy: zero endpoints currently available" + // TODO: limit the rate of the error logging. log.Printf(msg) e := httptypes.NewHTTPError(http.StatusServiceUnavailable, msg) e.WriteTo(rw) @@ -91,6 +92,7 @@ func (p *reverseProxy) ServeHTTP(rw http.ResponseWriter, clientreq *http.Request } if res == nil { + // TODO: limit the rate of the error logging. msg := fmt.Sprintf("proxy: unable to get response from %d endpoint(s)", len(endpoints)) log.Printf(msg) e := httptypes.NewHTTPError(http.StatusBadGateway, msg)