فهرست منبع

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.
Yicheng Qin 11 سال پیش
والد
کامیت
34dcbb4679
2فایلهای تغییر یافته به همراه27 افزوده شده و 0 حذف شده
  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
 	}
@@ -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
+}

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