client: consume json error and return ErrInvaildJSON

The default JSON error is not very readable. We let client
consume the error and return a more understandable error in
the context of etcd.

Fix #3120
release-2.2
Xiang Li 2015-07-15 07:35:57 +08:00 committed by Yicheng Qin
parent 6317abf7e4
commit d2dac0fe59
1 changed files with 7 additions and 2 deletions

View File

@ -16,6 +16,7 @@ package client
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
@ -60,6 +61,10 @@ func (e Error) Error() string {
return fmt.Sprintf("%v: %v (%v) [%v]", e.Code, e.Message, e.Cause, e.Index)
}
var (
ErrInvalidJSON = errors.New("client: response is invalid json. The endpoint is probably not valid etcd cluster endpoint.")
)
// PrevExistType is used to define an existence condition when setting
// or deleting Nodes.
type PrevExistType string
@ -589,7 +594,7 @@ func unmarshalSuccessfulKeysResponse(header http.Header, body []byte) (*Response
var res Response
err := json.Unmarshal(body, &res)
if err != nil {
return nil, err
return nil, ErrInvalidJSON
}
if header.Get("X-Etcd-Index") != "" {
res.Index, err = strconv.ParseUint(header.Get("X-Etcd-Index"), 10, 64)
@ -603,7 +608,7 @@ func unmarshalSuccessfulKeysResponse(header http.Header, body []byte) (*Response
func unmarshalFailedKeysResponse(body []byte) error {
var etcdErr Error
if err := json.Unmarshal(body, &etcdErr); err != nil {
return err
return ErrInvalidJSON
}
return etcdErr
}