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