From f03e18b746d720824e615a44ab553d2810c9e2bb Mon Sep 17 00:00:00 2001 From: Abhishekism9450 <32683845+Abhishekism9450@users.noreply.github.com> Date: Fri, 13 Jan 2023 12:28:13 +0530 Subject: [PATCH] added bad Request (#530) --- client/client.go | 10 +++++----- client/client_test.go | 12 ++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/client/client.go b/client/client.go index 088377798..2decd025a 100644 --- a/client/client.go +++ b/client/client.go @@ -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) @@ -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 { @@ -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 { @@ -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)) } diff --git a/client/client_test.go b/client/client_test.go index dc9ff73c7..6c4d45fd8 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -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) @@ -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") } } @@ -303,7 +303,7 @@ 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) @@ -311,8 +311,8 @@ func TestCheckResponse(t *testing.T) { 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") } }