Browse Source

Merge pull request #1396 from unihorn/185

etcdhttp: trim StoreKeysPrefix from error in serveKeys
Yicheng Qin 11 years ago
parent
commit
cb59a46576
2 changed files with 27 additions and 0 deletions
  1. 8 0
      etcdserver/etcdhttp/http.go
  2. 19 0
      etcdserver/etcdhttp/http_test.go

+ 8 - 0
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
+}

+ 19 - 0
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)
+		}
 	}
 }