diff --git a/etcdserver/etcdhttp/http.go b/etcdserver/etcdhttp/http.go index ade20c484..ced2bfbe2 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 } @@ -593,3 +594,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 084c8d6c3..094b59349 100644 --- a/etcdserver/etcdhttp/http_test.go +++ b/etcdserver/etcdhttp/http_test.go @@ -1136,6 +1136,7 @@ func TestBadServeKeys(t *testing.T) { server etcdserver.Server wcode int + wbody string }{ { // bad method @@ -1145,6 +1146,7 @@ func TestBadServeKeys(t *testing.T) { &resServer{}, http.StatusMethodNotAllowed, + "Method Not Allowed", }, { // bad method @@ -1154,6 +1156,7 @@ func TestBadServeKeys(t *testing.T) { &resServer{}, http.StatusMethodNotAllowed, + "Method Not Allowed", }, { // parseRequest error @@ -1164,6 +1167,7 @@ func TestBadServeKeys(t *testing.T) { &resServer{}, http.StatusBadRequest, + `{"errorCode":210,"message":"Invalid POST form","cause":"missing form body","index":0}`, }, { // etcdserver.Server error @@ -1173,6 +1177,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 @@ -1182,6 +1197,7 @@ func TestBadServeKeys(t *testing.T) { }, http.StatusInternalServerError, + "Internal Server Error", }, } for i, tt := range testBadCases { @@ -1194,6 +1210,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) + } } }