From a84e9cb805805e34e17650f42bb4afb6bf23ce5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wilson=20J=C3=BAnior?= Date: Thu, 14 May 2020 13:23:17 -0300 Subject: [PATCH] Add status code on response of error --- pkg/rpaas/client/internal_client.go | 37 ++++++++++++++---------- pkg/rpaas/client/internal_client_test.go | 22 +++++++------- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/pkg/rpaas/client/internal_client.go b/pkg/rpaas/client/internal_client.go index 5c09b60e2..f46700271 100644 --- a/pkg/rpaas/client/internal_client.go +++ b/pkg/rpaas/client/internal_client.go @@ -29,7 +29,6 @@ var ( ErrMissingInstance = fmt.Errorf("rpaasv2: instance cannot be empty") ErrMissingBlockName = fmt.Errorf("rpaasv2: block name cannot be empty") ErrMissingPath = fmt.Errorf("rpaasv2: path cannot be empty") - ErrUnexpectedStatusCode = fmt.Errorf("rpaasv2: unexpected status code") ErrInvalidMaxReplicasNumber = fmt.Errorf("rpaasv2: max replicas can't be lower than 1") ErrInvalidMinReplicasNumber = fmt.Errorf("rpaasv2: min replicas can't be lower than 1 and can't be higher than the maximum number of replicas") ErrInvalidCPUUsage = fmt.Errorf("rpaasv2: CPU usage can't be lower than 1%%") @@ -37,6 +36,12 @@ var ( ErrMissingValues = fmt.Errorf("rpaasv2: values can't be all empty") ) +type ErrUnexpectedStatusCode string + +func (statusCode ErrUnexpectedStatusCode) Error() string { + return fmt.Sprintf("rpaasv2: unexpected status code: %s", string(statusCode)) +} + type ClientOptions struct { Timeout time.Duration } @@ -153,7 +158,7 @@ func (c *client) Scale(ctx context.Context, args ScaleArgs) (*http.Response, err } if response.StatusCode != http.StatusOK { - return response, ErrUnexpectedStatusCode + return response, ErrUnexpectedStatusCode(response.Status) } return response, nil @@ -182,6 +187,10 @@ func (c *client) Info(ctx context.Context, args InfoArgs) (*types.InstanceInfo, return nil, nil, err } + if response.StatusCode != http.StatusOK { + return nil, response, ErrUnexpectedStatusCode(response.Status) + } + defer response.Body.Close() var infoPayload types.InstanceInfo err = json.NewDecoder(response.Body).Decode(&infoPayload) @@ -189,10 +198,6 @@ func (c *client) Info(ctx context.Context, args InfoArgs) (*types.InstanceInfo, return nil, nil, err } - if response.StatusCode != http.StatusOK { - return nil, response, ErrUnexpectedStatusCode - } - return &infoPayload, response, nil } @@ -228,7 +233,7 @@ func (c *client) UpdateCertificate(ctx context.Context, args UpdateCertificateAr } if response.StatusCode != http.StatusOK { - return response, ErrUnexpectedStatusCode + return response, ErrUnexpectedStatusCode(response.Status) } return response, nil @@ -266,7 +271,7 @@ func (c *client) UpdateBlock(ctx context.Context, args UpdateBlockArgs) (*http.R } if response.StatusCode != http.StatusOK { - return response, ErrUnexpectedStatusCode + return response, ErrUnexpectedStatusCode(response.Status) } return response, nil @@ -300,7 +305,7 @@ func (c *client) DeleteBlock(ctx context.Context, args DeleteBlockArgs) (*http.R } if response.StatusCode != http.StatusOK { - return response, ErrUnexpectedStatusCode + return response, ErrUnexpectedStatusCode(response.Status) } return response, nil @@ -330,7 +335,7 @@ func (c *client) ListBlocks(ctx context.Context, args ListBlocksArgs) ([]types.B } if response.StatusCode != http.StatusOK { - return nil, response, ErrUnexpectedStatusCode + return nil, response, ErrUnexpectedStatusCode(response.Status) } var blockList struct { @@ -371,7 +376,7 @@ func (c *client) DeleteRoute(ctx context.Context, args DeleteRouteArgs) (*http.R } if response.StatusCode != http.StatusOK { - return response, ErrUnexpectedStatusCode + return response, ErrUnexpectedStatusCode(response.Status) } return response, nil @@ -401,7 +406,7 @@ func (c *client) ListRoutes(ctx context.Context, args ListRoutesArgs) ([]types.R } if response.StatusCode != http.StatusOK { - return nil, response, ErrUnexpectedStatusCode + return nil, response, ErrUnexpectedStatusCode(response.Status) } var routes struct { @@ -442,7 +447,7 @@ func (c *client) UpdateRoute(ctx context.Context, args UpdateRouteArgs) (*http.R } if response.StatusCode != http.StatusCreated { - return response, ErrUnexpectedStatusCode + return response, ErrUnexpectedStatusCode(response.Status) } return response, nil @@ -472,7 +477,7 @@ func (c *client) GetAutoscale(ctx context.Context, args GetAutoscaleArgs) (*type } if resp.StatusCode != http.StatusOK { - return nil, resp, ErrUnexpectedStatusCode + return nil, resp, ErrUnexpectedStatusCode(resp.Status) } defer resp.Body.Close() @@ -535,7 +540,7 @@ func (c *client) UpdateAutoscale(ctx context.Context, args UpdateAutoscaleArgs) } if resp.StatusCode != http.StatusCreated { - return resp, ErrUnexpectedStatusCode + return resp, ErrUnexpectedStatusCode(resp.Status) } return resp, nil @@ -564,7 +569,7 @@ func (c *client) RemoveAutoscale(ctx context.Context, args RemoveAutoscaleArgs) } if resp.StatusCode != http.StatusOK { - return resp, ErrUnexpectedStatusCode + return resp, ErrUnexpectedStatusCode(resp.Status) } return resp, nil diff --git a/pkg/rpaas/client/internal_client_test.go b/pkg/rpaas/client/internal_client_test.go index 14dceb28e..ef5a77501 100644 --- a/pkg/rpaas/client/internal_client_test.go +++ b/pkg/rpaas/client/internal_client_test.go @@ -148,7 +148,7 @@ func TestClientThroughTsuru_Scale(t *testing.T) { Instance: "my-instance", Replicas: int32(777), }, - expectedError: ErrUnexpectedStatusCode.Error(), + expectedError: "rpaasv2: unexpected status code: 404 Not Found", handler: func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) fmt.Fprintf(w, "instance not found") @@ -240,7 +240,7 @@ func TestClientThroughTsuru_UpdateCertificate(t *testing.T) { Certificate: `my certificate`, Key: `my key`, }, - expectedError: ErrUnexpectedStatusCode.Error(), + expectedError: "rpaasv2: unexpected status code: 404 Not Found", handler: func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) fmt.Fprintf(w, "instance not found") @@ -312,7 +312,7 @@ func TestClientThroughTsuru_UpdateBlock(t *testing.T) { Name: "server", Content: "Some NGINX snippet", }, - expectedError: "rpaasv2: unexpected status code", + expectedError: "rpaasv2: unexpected status code: 404 Not Found", handler: func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) fmt.Fprintf(w, "instance not found") @@ -371,7 +371,7 @@ func TestClientThroughTsuru_DeleteBlock(t *testing.T) { Instance: "my-instance", Name: "server", }, - expectedError: "rpaasv2: unexpected status code", + expectedError: "rpaasv2: unexpected status code: 404 Not Found", handler: func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) fmt.Fprintf(w, "instance not found") @@ -410,7 +410,7 @@ func TestClientThroughTsuru_ListBlocks(t *testing.T) { args: ListBlocksArgs{ Instance: "my-instance", }, - expectedError: "rpaasv2: unexpected status code", + expectedError: "rpaasv2: unexpected status code: 404 Not Found", handler: func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) fmt.Fprintf(w, "instance not found") @@ -480,7 +480,7 @@ func TestClientThroughTsuru_DeleteRoute(t *testing.T) { Instance: "my-instance", Path: "/custom/path", }, - expectedError: "rpaasv2: unexpected status code", + expectedError: "rpaasv2: unexpected status code: 404 Not Found", handler: func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) fmt.Fprintf(w, "instance not found") @@ -534,7 +534,7 @@ func TestClientThroughTsuru_ListRoutes(t *testing.T) { args: ListRoutesArgs{ Instance: "my-instance", }, - expectedError: "rpaasv2: unexpected status code", + expectedError: "rpaasv2: unexpected status code: 404 Not Found", handler: func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) fmt.Fprintf(w, "instance not found") @@ -650,7 +650,7 @@ func TestClientThroughTsuru_UpdateRoute(t *testing.T) { Path: "/app", Destination: "app.tsuru.example.com", }, - expectedError: "rpaasv2: unexpected status code", + expectedError: "rpaasv2: unexpected status code: 404 Not Found", handler: func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) fmt.Fprintf(w, "instance not found") @@ -717,7 +717,7 @@ func TestClientThroughTsuru_UpdateAutoscale(t *testing.T) { CPU: int32(27), Memory: int32(33), }, - expectedError: "rpaasv2: unexpected status code", + expectedError: "rpaasv2: unexpected status code: 404 Not Found", handler: func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) fmt.Fprintf(w, "instance not found") @@ -829,7 +829,7 @@ func TestClientThroughTsuru_GetAutoscale(t *testing.T) { args: GetAutoscaleArgs{ Instance: "my-instance", }, - expectedError: "rpaasv2: unexpected status code", + expectedError: "rpaasv2: unexpected status code: 404 Not Found", handler: func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) fmt.Fprintf(w, "instance not found") @@ -880,7 +880,7 @@ func TestClientThroughTsuru_RemoveAutoscale(t *testing.T) { args: RemoveAutoscaleArgs{ Instance: "my-instance", }, - expectedError: "rpaasv2: unexpected status code", + expectedError: "rpaasv2: unexpected status code: 404 Not Found", handler: func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) fmt.Fprintf(w, "instance not found")