mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
client: add error handling for addmember
This commit is contained in:
parent
b53a98eb38
commit
66572561bf
@ -56,7 +56,7 @@ func (m *httpMembersAPI) List(ctx context.Context) ([]httptypes.Member, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := assertStatusCode(http.StatusOK, resp.StatusCode); err != nil {
|
if err := assertStatusCode(resp.StatusCode, http.StatusOK); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,10 +80,18 @@ func (m *httpMembersAPI) Add(ctx context.Context, peerURL string) (*httptypes.Me
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := assertStatusCode(http.StatusCreated, resp.StatusCode); err != nil {
|
if err := assertStatusCode(resp.StatusCode, http.StatusCreated, http.StatusConflict); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusCreated {
|
||||||
|
var httperr httptypes.HTTPError
|
||||||
|
if err := json.Unmarshal(body, &httperr); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return nil, httperr
|
||||||
|
}
|
||||||
|
|
||||||
var memb httptypes.Member
|
var memb httptypes.Member
|
||||||
if err := json.Unmarshal(body, &memb); err != nil {
|
if err := json.Unmarshal(body, &memb); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -99,7 +107,7 @@ func (m *httpMembersAPI) Remove(ctx context.Context, memberID string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return assertStatusCode(http.StatusNoContent, resp.StatusCode)
|
return assertStatusCode(resp.StatusCode, http.StatusNoContent)
|
||||||
}
|
}
|
||||||
|
|
||||||
type membersAPIActionList struct{}
|
type membersAPIActionList struct{}
|
||||||
@ -134,11 +142,13 @@ func (a *membersAPIActionAdd) HTTPRequest(ep url.URL) *http.Request {
|
|||||||
return req
|
return req
|
||||||
}
|
}
|
||||||
|
|
||||||
func assertStatusCode(want, got int) (err error) {
|
func assertStatusCode(got int, want ...int) (err error) {
|
||||||
if want != got {
|
for _, w := range want {
|
||||||
err = fmt.Errorf("unexpected status code %d", got)
|
if w == got {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return err
|
return fmt.Errorf("unexpected status code %d", got)
|
||||||
}
|
}
|
||||||
|
|
||||||
// v2MembersURL add the necessary path to the provided endpoint
|
// v2MembersURL add the necessary path to the provided endpoint
|
||||||
|
@ -86,12 +86,12 @@ func TestMembersAPIActionRemove(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestAssertStatusCode(t *testing.T) {
|
func TestAssertStatusCode(t *testing.T) {
|
||||||
if err := assertStatusCode(400, 404); err == nil {
|
if err := assertStatusCode(404, 400); err == nil {
|
||||||
t.Errorf("assertStatusCode failed to detect conflict in 400 vs 404")
|
t.Errorf("assertStatusCode failed to detect conflict in 400 vs 404")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := assertStatusCode(400, 400); err != nil {
|
if err := assertStatusCode(404, 400, 404); err != nil {
|
||||||
t.Errorf("assertStatusCode found conflict in 400 vs 400: %v", err)
|
t.Errorf("assertStatusCode found conflict in (404,400) vs 400: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user