Skip to content

Commit

Permalink
Add status code on response of error
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed May 14, 2020
1 parent 73e7cfb commit a84e9cb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
37 changes: 21 additions & 16 deletions pkg/rpaas/client/internal_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,19 @@ 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%%")
ErrInvalidMemoryUsage = fmt.Errorf("rpaasv2: memory usage can't be lower than 1%%")
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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -182,17 +187,17 @@ 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)
if err != nil {
return nil, nil, err
}

if response.StatusCode != http.StatusOK {
return nil, response, ErrUnexpectedStatusCode
}

return &infoPayload, response, nil
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
22 changes: 11 additions & 11 deletions pkg/rpaas/client/internal_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit a84e9cb

Please sign in to comment.