Skip to content

Commit

Permalink
added bad Request (#530)
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhishekism9450 authored Jan 13, 2023
1 parent 7081b2f commit f03e18b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
10 changes: 5 additions & 5 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,11 @@ func CheckResponse(r *http.Response) error {
// Nutanix returns non-json response with code 401 when
// invalid credentials are used
if c == http.StatusUnauthorized {
return fmt.Errorf("invalid Nutanix Credentials")
return fmt.Errorf("invalid auth Credentials")
}

if c == http.StatusBadRequest {
return fmt.Errorf("bad Request")
}

buf, err := ioutil.ReadAll(r.Body)
Expand All @@ -574,7 +578,6 @@ func CheckResponse(r *http.Response) error {
if err != nil {
return fmt.Errorf("unmarshalling error response %s for response body %s", err, string(buf))
}
log.Print("[DEBUG] after json.Unmarshal")

errRes := &ErrorResponse{}
if status, ok := res["status"]; ok {
Expand All @@ -590,11 +593,9 @@ func CheckResponse(r *http.Response) error {
return nil
}

log.Print("[DEBUG] after bunch of switch cases")
if err != nil {
return err
}
log.Print("[DEBUG] first nil check")

// karbon error check
if messageInfo, ok := res["message_info"]; ok {
Expand All @@ -610,7 +611,6 @@ func CheckResponse(r *http.Response) error {
return nil
}

log.Print("[DEBUG] after errRes.State")
pretty, _ := json.MarshalIndent(errRes, "", " ")
return fmt.Errorf("error: %s", string(pretty))
}
Expand Down
12 changes: 6 additions & 6 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func TestGetResponse(t *testing.T) {
StatusCode: http.StatusBadRequest,
Body: ioutil.NopCloser(strings.NewReader(
`{"api_version": "3.1", "code": 400, "kind": "error", "message_list":
[{"message": "This field may not be blank."}], "state": "ERROR"}`)),
[{"message": "bad Request"}], "state": "ERROR"}`)),
}

err := CheckResponse(res)
Expand All @@ -292,8 +292,8 @@ func TestGetResponse(t *testing.T) {
t.Fatal("Expected error response.")
}

if !strings.Contains(fmt.Sprint(err), "This field may not be blank.") {
t.Errorf("error = %#v, expected %#v", err, "This field may not be blank.")
if !strings.Contains(fmt.Sprint(err), "bad Request") {
t.Errorf("error = %#v, expected %#v", err, "bad Request")
}
}

Expand All @@ -303,16 +303,16 @@ func TestCheckResponse(t *testing.T) {
StatusCode: http.StatusBadRequest,
Body: ioutil.NopCloser(strings.NewReader(
`{"api_version": "3.1", "code": 400, "kind": "error", "message_list":
[{"message": "This field may not be blank."}], "state": "ERROR"}`)),
[{"message": "bad Request"}], "state": "ERROR"}`)),
}
err := CheckResponse(res)

if err == nil {
t.Fatalf("Expected error response.")
}

if !strings.Contains(fmt.Sprint(err), "This field may not be blank.") {
t.Errorf("error = %#v, expected %#v", err, "This field may not be blank.")
if !strings.Contains(fmt.Sprint(err), "bad Request") {
t.Errorf("error = %#v, expected %#v", err, "bad Request")
}
}

Expand Down

0 comments on commit f03e18b

Please sign in to comment.