Skip to content

Commit 1b58bfc

Browse files
Feature: Adding standardize response error (#225)
* Adding standardize request error To help simplify and address how errors from request are handled, this returns an error type that allows for deeper introspection from external packages. * Correcting AsResponseError method * Adding slices contains
1 parent ebdaa3e commit 1b58bfc

26 files changed

+600
-719
lines changed

alertmuting.go

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7-
"fmt"
87
"io"
98
"io/ioutil"
109
"net/http"
@@ -25,16 +24,13 @@ func (c *Client) CreateAlertMutingRule(ctx context.Context, muteRequest *alertmu
2524
}
2625

2726
resp, err := c.doRequest(ctx, "POST", AlertMutingRuleAPIURL, nil, bytes.NewReader(payload))
28-
if resp != nil {
29-
defer resp.Body.Close()
30-
}
3127
if err != nil {
3228
return nil, err
3329
}
30+
defer resp.Body.Close()
3431

35-
if resp.StatusCode != http.StatusCreated {
36-
message, _ := ioutil.ReadAll(resp.Body)
37-
return nil, fmt.Errorf("Bad status %d: %s", resp.StatusCode, message)
32+
if err = newResponseError(resp, http.StatusCreated); err != nil {
33+
return nil, err
3834
}
3935

4036
finalRule := &alertmuting.AlertMutingRule{}
@@ -48,16 +44,13 @@ func (c *Client) CreateAlertMutingRule(ctx context.Context, muteRequest *alertmu
4844
// DeleteAlertMutingRule deletes an alert muting rule.
4945
func (c *Client) DeleteAlertMutingRule(ctx context.Context, name string) error {
5046
resp, err := c.doRequest(ctx, "DELETE", AlertMutingRuleAPIURL+"/"+name, nil, nil)
51-
if resp != nil {
52-
defer resp.Body.Close()
53-
}
5447
if err != nil {
5548
return err
5649
}
50+
defer resp.Body.Close()
5751

58-
if resp.StatusCode != http.StatusNoContent {
59-
message, _ := ioutil.ReadAll(resp.Body)
60-
return fmt.Errorf("Unexpected status code: %d: %s", resp.StatusCode, message)
52+
if err = newResponseError(resp, http.StatusNoContent); err != nil {
53+
return err
6154
}
6255
_, _ = io.Copy(ioutil.Discard, resp.Body)
6356

@@ -67,16 +60,13 @@ func (c *Client) DeleteAlertMutingRule(ctx context.Context, name string) error {
6760
// GetAlertMutingRule gets an alert muting rule.
6861
func (c *Client) GetAlertMutingRule(ctx context.Context, id string) (*alertmuting.AlertMutingRule, error) {
6962
resp, err := c.doRequest(ctx, "GET", AlertMutingRuleAPIURL+"/"+id, nil, nil)
70-
if resp != nil {
71-
defer resp.Body.Close()
72-
}
7363
if err != nil {
7464
return nil, err
7565
}
66+
defer resp.Body.Close()
7667

77-
if resp.StatusCode != http.StatusOK {
78-
message, _ := ioutil.ReadAll(resp.Body)
79-
return nil, fmt.Errorf("Bad status %d: %s", resp.StatusCode, message)
68+
if err = newResponseError(resp, http.StatusOK); err != nil {
69+
return nil, err
8070
}
8171

8272
finalRule := &alertmuting.AlertMutingRule{}
@@ -95,16 +85,13 @@ func (c *Client) UpdateAlertMutingRule(ctx context.Context, id string, muteReque
9585
}
9686

9787
resp, err := c.doRequest(ctx, "PUT", AlertMutingRuleAPIURL+"/"+id, nil, bytes.NewReader(payload))
98-
if resp != nil {
99-
defer resp.Body.Close()
100-
}
10188
if err != nil {
10289
return nil, err
10390
}
91+
defer resp.Body.Close()
10492

105-
if resp.StatusCode != http.StatusOK {
106-
message, _ := ioutil.ReadAll(resp.Body)
107-
return nil, fmt.Errorf("Bad status %d: %s", resp.StatusCode, message)
93+
if err = newResponseError(resp, http.StatusOK); err != nil {
94+
return nil, err
10895
}
10996

11097
finalRule := &alertmuting.AlertMutingRule{}
@@ -124,12 +111,14 @@ func (c *Client) SearchAlertMutingRules(ctx context.Context, include string, lim
124111
params.Add("offset", strconv.Itoa(offset))
125112

126113
resp, err := c.doRequest(ctx, "GET", AlertMutingRuleAPIURL, params, nil)
127-
if resp != nil {
128-
defer resp.Body.Close()
129-
}
130114
if err != nil {
131115
return nil, err
132116
}
117+
defer resp.Body.Close()
118+
119+
if err = newResponseError(resp, http.StatusOK); err != nil {
120+
return nil, err
121+
}
133122

134123
finalRules := &alertmuting.SearchResult{}
135124

aws_cloudwatch_integration.go

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7-
"fmt"
87
"io"
98
"io/ioutil"
109
"net/http"
@@ -20,16 +19,13 @@ func (c *Client) CreateAWSCloudWatchIntegration(ctx context.Context, acwi *integ
2019
}
2120

2221
resp, err := c.doRequest(ctx, "POST", IntegrationAPIURL, nil, bytes.NewReader(payload))
23-
if resp != nil {
24-
defer resp.Body.Close()
25-
}
2622
if err != nil {
2723
return nil, err
2824
}
25+
defer resp.Body.Close()
2926

30-
if resp.StatusCode != http.StatusOK {
31-
message, _ := ioutil.ReadAll(resp.Body)
32-
return nil, fmt.Errorf("Unexpected status code: %d: %s", resp.StatusCode, message)
27+
if err = newResponseError(resp, http.StatusOK); err != nil {
28+
return nil, err
3329
}
3430

3531
finalIntegration := integration.AwsCloudWatchIntegration{}
@@ -43,16 +39,13 @@ func (c *Client) CreateAWSCloudWatchIntegration(ctx context.Context, acwi *integ
4339
// GetAWSCloudWatchIntegration retrieves an AWS CloudWatch integration.
4440
func (c *Client) GetAWSCloudWatchIntegration(ctx context.Context, id string) (*integration.AwsCloudWatchIntegration, error) {
4541
resp, err := c.doRequest(ctx, "GET", IntegrationAPIURL+"/"+id, nil, nil)
46-
if resp != nil {
47-
defer resp.Body.Close()
48-
}
4942
if err != nil {
5043
return nil, err
5144
}
45+
defer resp.Body.Close()
5246

53-
if resp.StatusCode != http.StatusOK {
54-
message, _ := ioutil.ReadAll(resp.Body)
55-
return nil, fmt.Errorf("Unexpected status code: %d: %s", resp.StatusCode, message)
47+
if err = newResponseError(resp, http.StatusOK); err != nil {
48+
return nil, err
5649
}
5750

5851
finalIntegration := integration.AwsCloudWatchIntegration{}
@@ -71,16 +64,13 @@ func (c *Client) UpdateAWSCloudWatchIntegration(ctx context.Context, id string,
7164
}
7265

7366
resp, err := c.doRequest(ctx, "PUT", IntegrationAPIURL+"/"+id, nil, bytes.NewReader(payload))
74-
if resp != nil {
75-
defer resp.Body.Close()
76-
}
7767
if err != nil {
7868
return nil, err
7969
}
70+
defer resp.Body.Close()
8071

81-
if resp.StatusCode != http.StatusOK {
82-
message, _ := ioutil.ReadAll(resp.Body)
83-
return nil, fmt.Errorf("Unexpected status code: %d: %s", resp.StatusCode, message)
72+
if err = newResponseError(resp, http.StatusOK); err != nil {
73+
return nil, err
8474
}
8575

8676
finalIntegration := integration.AwsCloudWatchIntegration{}
@@ -94,16 +84,13 @@ func (c *Client) UpdateAWSCloudWatchIntegration(ctx context.Context, id string,
9484
// DeleteAWSCloudWatchIntegration deletes an AWS CloudWatch integration.
9585
func (c *Client) DeleteAWSCloudWatchIntegration(ctx context.Context, id string) error {
9686
resp, err := c.doRequest(ctx, "DELETE", IntegrationAPIURL+"/"+id, nil, nil)
97-
if resp != nil {
98-
defer resp.Body.Close()
99-
}
10087
if err != nil {
10188
return err
10289
}
90+
defer resp.Body.Close()
10391

104-
if resp.StatusCode != http.StatusNoContent {
105-
message, _ := ioutil.ReadAll(resp.Body)
106-
return fmt.Errorf("Unexpected status code: %d: %s", resp.StatusCode, message)
92+
if err = newResponseError(resp, http.StatusNoContent); err != nil {
93+
return err
10794
}
10895
_, _ = io.Copy(ioutil.Discard, resp.Body)
10996

azure_integration.go

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7-
"fmt"
87
"io"
98
"io/ioutil"
109
"net/http"
@@ -20,16 +19,13 @@ func (c *Client) CreateAzureIntegration(ctx context.Context, acwi *integration.A
2019
}
2120

2221
resp, err := c.doRequest(ctx, "POST", IntegrationAPIURL, nil, bytes.NewReader(payload))
23-
if resp != nil {
24-
defer resp.Body.Close()
25-
}
2622
if err != nil {
2723
return nil, err
2824
}
25+
defer resp.Body.Close()
2926

30-
if resp.StatusCode != http.StatusOK {
31-
message, _ := ioutil.ReadAll(resp.Body)
32-
return nil, fmt.Errorf("Unexpected status code: %d: %s", resp.StatusCode, message)
27+
if err = newResponseError(resp, http.StatusOK); err != nil {
28+
return nil, err
3329
}
3430

3531
finalIntegration := integration.AzureIntegration{}
@@ -43,16 +39,13 @@ func (c *Client) CreateAzureIntegration(ctx context.Context, acwi *integration.A
4339
// GetAzureIntegration retrieves an Azure integration.
4440
func (c *Client) GetAzureIntegration(ctx context.Context, id string) (*integration.AzureIntegration, error) {
4541
resp, err := c.doRequest(ctx, "GET", IntegrationAPIURL+"/"+id, nil, nil)
46-
if resp != nil {
47-
defer resp.Body.Close()
48-
}
4942
if err != nil {
5043
return nil, err
5144
}
45+
defer resp.Body.Close()
5246

53-
if resp.StatusCode != http.StatusOK {
54-
message, _ := ioutil.ReadAll(resp.Body)
55-
return nil, fmt.Errorf("Unexpected status code: %d: %s", resp.StatusCode, message)
47+
if err = newResponseError(resp, http.StatusOK); err != nil {
48+
return nil, err
5649
}
5750

5851
finalIntegration := integration.AzureIntegration{}
@@ -71,16 +64,14 @@ func (c *Client) UpdateAzureIntegration(ctx context.Context, id string, acwi *in
7164
}
7265

7366
resp, err := c.doRequest(ctx, "PUT", IntegrationAPIURL+"/"+id, nil, bytes.NewReader(payload))
74-
if resp != nil {
75-
defer resp.Body.Close()
76-
}
67+
7768
if err != nil {
7869
return nil, err
7970
}
71+
defer resp.Body.Close()
8072

81-
if resp.StatusCode != http.StatusOK {
82-
message, _ := ioutil.ReadAll(resp.Body)
83-
return nil, fmt.Errorf("Unexpected status code: %d: %s", resp.StatusCode, message)
73+
if err = newResponseError(resp, http.StatusOK); err != nil {
74+
return nil, err
8475
}
8576

8677
finalIntegration := integration.AzureIntegration{}
@@ -94,16 +85,13 @@ func (c *Client) UpdateAzureIntegration(ctx context.Context, id string, acwi *in
9485
// DeleteAzureIntegration deletes an Azure integration.
9586
func (c *Client) DeleteAzureIntegration(ctx context.Context, id string) error {
9687
resp, err := c.doRequest(ctx, "DELETE", IntegrationAPIURL+"/"+id, nil, nil)
97-
if resp != nil {
98-
defer resp.Body.Close()
99-
}
10088
if err != nil {
10189
return err
10290
}
91+
defer resp.Body.Close()
10392

104-
if resp.StatusCode != http.StatusNoContent {
105-
message, _ := ioutil.ReadAll(resp.Body)
106-
return fmt.Errorf("Unexpected status code: %d: %s", resp.StatusCode, message)
93+
if err = newResponseError(resp, http.StatusNoContent); err != nil {
94+
return err
10795
}
10896
_, _ = io.Copy(ioutil.Discard, resp.Body)
10997

chart.go

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7-
"errors"
8-
"fmt"
97
"io"
108
"io/ioutil"
119
"net/http"
@@ -26,16 +24,13 @@ func (c *Client) CreateChart(ctx context.Context, chartRequest *chart.CreateUpda
2624
}
2725

2826
resp, err := c.doRequest(ctx, "POST", ChartAPIURL, nil, bytes.NewReader(payload))
29-
if resp != nil {
30-
defer resp.Body.Close()
31-
}
3227
if err != nil {
3328
return nil, err
3429
}
30+
defer resp.Body.Close()
3531

36-
if resp.StatusCode != http.StatusOK {
37-
message, _ := ioutil.ReadAll(resp.Body)
38-
return nil, fmt.Errorf("Unexpected status code: %d: %s", resp.StatusCode, message)
32+
if err = newResponseError(resp, http.StatusOK); err != nil {
33+
return nil, err
3934
}
4035

4136
finalChart := &chart.Chart{}
@@ -49,15 +44,13 @@ func (c *Client) CreateChart(ctx context.Context, chartRequest *chart.CreateUpda
4944
// DeleteChart deletes a chart.
5045
func (c *Client) DeleteChart(ctx context.Context, id string) error {
5146
resp, err := c.doRequest(ctx, "DELETE", ChartAPIURL+"/"+id, nil, nil)
52-
if resp != nil {
53-
defer resp.Body.Close()
54-
}
5547
if err != nil {
5648
return err
5749
}
50+
defer resp.Body.Close()
5851

59-
if resp.StatusCode != http.StatusOK {
60-
return errors.New("Unexpected status code: " + resp.Status)
52+
if err = newResponseError(resp, http.StatusOK); err != nil {
53+
return err
6154
}
6255
_, _ = io.Copy(ioutil.Discard, resp.Body)
6356

@@ -67,16 +60,13 @@ func (c *Client) DeleteChart(ctx context.Context, id string) error {
6760
// GetChart gets a chart.
6861
func (c *Client) GetChart(ctx context.Context, id string) (*chart.Chart, error) {
6962
resp, err := c.doRequest(ctx, "GET", ChartAPIURL+"/"+id, nil, nil)
70-
if resp != nil {
71-
defer resp.Body.Close()
72-
}
7363
if err != nil {
7464
return nil, err
7565
}
66+
defer resp.Body.Close()
7667

77-
if resp.StatusCode != http.StatusOK {
78-
message, _ := ioutil.ReadAll(resp.Body)
79-
return nil, fmt.Errorf("Unexpected status code: %d: %s", resp.StatusCode, message)
68+
if err = newResponseError(resp, http.StatusOK); err != nil {
69+
return nil, err
8070
}
8171

8272
finalChart := &chart.Chart{}
@@ -95,16 +85,13 @@ func (c *Client) UpdateChart(ctx context.Context, id string, chartRequest *chart
9585
}
9686

9787
resp, err := c.doRequest(ctx, "PUT", ChartAPIURL+"/"+id, nil, bytes.NewReader(payload))
98-
if resp != nil {
99-
defer resp.Body.Close()
100-
}
10188
if err != nil {
10289
return nil, err
10390
}
91+
defer resp.Body.Close()
10492

105-
if resp.StatusCode != http.StatusOK {
106-
message, _ := ioutil.ReadAll(resp.Body)
107-
return nil, fmt.Errorf("Unexpected status code: %d: %s", resp.StatusCode, message)
93+
if err = newResponseError(resp, http.StatusOK); err != nil {
94+
return nil, err
10895
}
10996

11097
finalChart := &chart.Chart{}
@@ -128,16 +115,13 @@ func (c *Client) SearchCharts(ctx context.Context, limit int, name string, offse
128115
}
129116

130117
resp, err := c.doRequest(ctx, "GET", ChartAPIURL, params, nil)
131-
if resp != nil {
132-
defer resp.Body.Close()
133-
}
134118
if err != nil {
135119
return nil, err
136120
}
121+
defer resp.Body.Close()
137122

138-
if resp.StatusCode != http.StatusOK {
139-
message, _ := ioutil.ReadAll(resp.Body)
140-
return nil, fmt.Errorf("Unexpected status code: %d: %s", resp.StatusCode, message)
123+
if err = newResponseError(resp, http.StatusOK); err != nil {
124+
return nil, err
141125
}
142126

143127
finalCharts := &chart.SearchResult{}

0 commit comments

Comments
 (0)