From 34dcbb4679168b9daffa0ad75870b9a28c9cd0ce Mon Sep 17 00:00:00 2001 From: Yicheng Qin Date: Fri, 24 Oct 2014 13:55:24 -0700 Subject: [PATCH] etcdhttp: trim StoreKeysPrefix from error in serveKeys It returns error messaage like this now: '{"errorCode":100,"message":"Key not found","cause":"/1/pants","index":10}' The commit trims '/1' prefix from cause field if exists. This is a hack to make it display well. It is correct because all error causes that contain Path puts Path at the head of the string. --- etcdserver/etcdhttp/http.go | 8 ++++++++ etcdserver/etcdhttp/http_test.go | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/etcdserver/etcdhttp/http.go b/etcdserver/etcdhttp/http.go index ab69dc1c3..9dba49e71 100644 --- a/etcdserver/etcdhttp/http.go +++ b/etcdserver/etcdhttp/http.go @@ -124,6 +124,7 @@ func (h serverHandler) serveKeys(w http.ResponseWriter, r *http.Request) { resp, err := h.server.Do(ctx, rr) if err != nil { + err = trimErrorPrefix(err, etcdserver.StoreKeysPrefix) writeError(w, err) return } @@ -587,3 +588,10 @@ func trimNodeExternPrefix(n *store.NodeExtern, prefix string) *store.NodeExtern } return n } + +func trimErrorPrefix(err error, prefix string) error { + if e, ok := err.(*etcdErr.Error); ok { + e.Cause = strings.TrimPrefix(e.Cause, prefix) + } + return err +} diff --git a/etcdserver/etcdhttp/http_test.go b/etcdserver/etcdhttp/http_test.go index cbebc12a0..754f6ef33 100644 --- a/etcdserver/etcdhttp/http_test.go +++ b/etcdserver/etcdhttp/http_test.go @@ -1100,6 +1100,7 @@ func TestBadServeKeys(t *testing.T) { server etcdserver.Server wcode int + wbody string }{ { // bad method @@ -1109,6 +1110,7 @@ func TestBadServeKeys(t *testing.T) { &resServer{}, http.StatusMethodNotAllowed, + "Method Not Allowed", }, { // bad method @@ -1118,6 +1120,7 @@ func TestBadServeKeys(t *testing.T) { &resServer{}, http.StatusMethodNotAllowed, + "Method Not Allowed", }, { // parseRequest error @@ -1128,6 +1131,7 @@ func TestBadServeKeys(t *testing.T) { &resServer{}, http.StatusBadRequest, + `{"errorCode":210,"message":"Invalid POST form","cause":"missing form body","index":0}`, }, { // etcdserver.Server error @@ -1137,6 +1141,17 @@ func TestBadServeKeys(t *testing.T) { }, http.StatusInternalServerError, + "Internal Server Error", + }, + { + // etcdserver.Server etcd error + mustNewRequest(t, "foo"), + &errServer{ + etcdErr.NewError(etcdErr.EcodeKeyNotFound, "/1/pant", 0), + }, + + http.StatusNotFound, + `{"errorCode":100,"message":"Key not found","cause":"/pant","index":0}`, }, { // non-event/watcher response from etcdserver.Server @@ -1146,6 +1161,7 @@ func TestBadServeKeys(t *testing.T) { }, http.StatusInternalServerError, + "Internal Server Error", }, } for i, tt := range testBadCases { @@ -1158,6 +1174,9 @@ func TestBadServeKeys(t *testing.T) { if rw.Code != tt.wcode { t.Errorf("#%d: got code=%d, want %d", i, rw.Code, tt.wcode) } + if g := strings.TrimSuffix(rw.Body.String(), "\n"); g != tt.wbody { + t.Errorf("#%d: body = %s, want %s", i, g, tt.wbody) + } } }