Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Adding standardize response error #225

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 17 additions & 28 deletions alertmuting.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
Expand All @@ -25,16 +24,13 @@ func (c *Client) CreateAlertMutingRule(ctx context.Context, muteRequest *alertmu
}

resp, err := c.doRequest(ctx, "POST", AlertMutingRuleAPIURL, nil, bytes.NewReader(payload))
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusCreated {
message, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("Bad status %d: %s", resp.StatusCode, message)
if err = newResponseError(resp, http.StatusCreated); err != nil {
return nil, err
}

finalRule := &alertmuting.AlertMutingRule{}
Expand All @@ -48,16 +44,13 @@ func (c *Client) CreateAlertMutingRule(ctx context.Context, muteRequest *alertmu
// DeleteAlertMutingRule deletes an alert muting rule.
func (c *Client) DeleteAlertMutingRule(ctx context.Context, name string) error {
resp, err := c.doRequest(ctx, "DELETE", AlertMutingRuleAPIURL+"/"+name, nil, nil)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusNoContent {
message, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("Unexpected status code: %d: %s", resp.StatusCode, message)
if err = newResponseError(resp, http.StatusNoContent); err != nil {
return err
}
_, _ = io.Copy(ioutil.Discard, resp.Body)

Expand All @@ -67,16 +60,13 @@ func (c *Client) DeleteAlertMutingRule(ctx context.Context, name string) error {
// GetAlertMutingRule gets an alert muting rule.
func (c *Client) GetAlertMutingRule(ctx context.Context, id string) (*alertmuting.AlertMutingRule, error) {
resp, err := c.doRequest(ctx, "GET", AlertMutingRuleAPIURL+"/"+id, nil, nil)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
message, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("Bad status %d: %s", resp.StatusCode, message)
if err = newResponseError(resp, http.StatusOK); err != nil {
return nil, err
}

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

resp, err := c.doRequest(ctx, "PUT", AlertMutingRuleAPIURL+"/"+id, nil, bytes.NewReader(payload))
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
message, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("Bad status %d: %s", resp.StatusCode, message)
if err = newResponseError(resp, http.StatusOK); err != nil {
return nil, err
}

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

resp, err := c.doRequest(ctx, "GET", AlertMutingRuleAPIURL, params, nil)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
defer resp.Body.Close()

if err = newResponseError(resp, http.StatusOK); err != nil {
return nil, err
}

finalRules := &alertmuting.SearchResult{}

Expand Down
37 changes: 12 additions & 25 deletions aws_cloudwatch_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
Expand All @@ -20,16 +19,13 @@ func (c *Client) CreateAWSCloudWatchIntegration(ctx context.Context, acwi *integ
}

resp, err := c.doRequest(ctx, "POST", IntegrationAPIURL, nil, bytes.NewReader(payload))
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
message, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("Unexpected status code: %d: %s", resp.StatusCode, message)
if err = newResponseError(resp, http.StatusOK); err != nil {
return nil, err
}

finalIntegration := integration.AwsCloudWatchIntegration{}
Expand All @@ -43,16 +39,13 @@ func (c *Client) CreateAWSCloudWatchIntegration(ctx context.Context, acwi *integ
// GetAWSCloudWatchIntegration retrieves an AWS CloudWatch integration.
func (c *Client) GetAWSCloudWatchIntegration(ctx context.Context, id string) (*integration.AwsCloudWatchIntegration, error) {
resp, err := c.doRequest(ctx, "GET", IntegrationAPIURL+"/"+id, nil, nil)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
message, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("Unexpected status code: %d: %s", resp.StatusCode, message)
if err = newResponseError(resp, http.StatusOK); err != nil {
return nil, err
}

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

resp, err := c.doRequest(ctx, "PUT", IntegrationAPIURL+"/"+id, nil, bytes.NewReader(payload))
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
message, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("Unexpected status code: %d: %s", resp.StatusCode, message)
if err = newResponseError(resp, http.StatusOK); err != nil {
return nil, err
}

finalIntegration := integration.AwsCloudWatchIntegration{}
Expand All @@ -94,16 +84,13 @@ func (c *Client) UpdateAWSCloudWatchIntegration(ctx context.Context, id string,
// DeleteAWSCloudWatchIntegration deletes an AWS CloudWatch integration.
func (c *Client) DeleteAWSCloudWatchIntegration(ctx context.Context, id string) error {
resp, err := c.doRequest(ctx, "DELETE", IntegrationAPIURL+"/"+id, nil, nil)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusNoContent {
message, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("Unexpected status code: %d: %s", resp.StatusCode, message)
if err = newResponseError(resp, http.StatusNoContent); err != nil {
return err
}
_, _ = io.Copy(ioutil.Discard, resp.Body)

Expand Down
38 changes: 13 additions & 25 deletions azure_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
Expand All @@ -20,16 +19,13 @@ func (c *Client) CreateAzureIntegration(ctx context.Context, acwi *integration.A
}

resp, err := c.doRequest(ctx, "POST", IntegrationAPIURL, nil, bytes.NewReader(payload))
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
message, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("Unexpected status code: %d: %s", resp.StatusCode, message)
if err = newResponseError(resp, http.StatusOK); err != nil {
return nil, err
}

finalIntegration := integration.AzureIntegration{}
Expand All @@ -43,16 +39,13 @@ func (c *Client) CreateAzureIntegration(ctx context.Context, acwi *integration.A
// GetAzureIntegration retrieves an Azure integration.
func (c *Client) GetAzureIntegration(ctx context.Context, id string) (*integration.AzureIntegration, error) {
resp, err := c.doRequest(ctx, "GET", IntegrationAPIURL+"/"+id, nil, nil)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
message, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("Unexpected status code: %d: %s", resp.StatusCode, message)
if err = newResponseError(resp, http.StatusOK); err != nil {
return nil, err
}

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

resp, err := c.doRequest(ctx, "PUT", IntegrationAPIURL+"/"+id, nil, bytes.NewReader(payload))
if resp != nil {
defer resp.Body.Close()
}

if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
message, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("Unexpected status code: %d: %s", resp.StatusCode, message)
if err = newResponseError(resp, http.StatusOK); err != nil {
return nil, err
}

finalIntegration := integration.AzureIntegration{}
Expand All @@ -94,16 +85,13 @@ func (c *Client) UpdateAzureIntegration(ctx context.Context, id string, acwi *in
// DeleteAzureIntegration deletes an Azure integration.
func (c *Client) DeleteAzureIntegration(ctx context.Context, id string) error {
resp, err := c.doRequest(ctx, "DELETE", IntegrationAPIURL+"/"+id, nil, nil)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusNoContent {
message, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("Unexpected status code: %d: %s", resp.StatusCode, message)
if err = newResponseError(resp, http.StatusNoContent); err != nil {
return err
}
_, _ = io.Copy(ioutil.Discard, resp.Body)

Expand Down
46 changes: 15 additions & 31 deletions chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
Expand All @@ -26,16 +24,13 @@ func (c *Client) CreateChart(ctx context.Context, chartRequest *chart.CreateUpda
}

resp, err := c.doRequest(ctx, "POST", ChartAPIURL, nil, bytes.NewReader(payload))
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
message, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("Unexpected status code: %d: %s", resp.StatusCode, message)
if err = newResponseError(resp, http.StatusOK); err != nil {
return nil, err
}

finalChart := &chart.Chart{}
Expand All @@ -49,15 +44,13 @@ func (c *Client) CreateChart(ctx context.Context, chartRequest *chart.CreateUpda
// DeleteChart deletes a chart.
func (c *Client) DeleteChart(ctx context.Context, id string) error {
resp, err := c.doRequest(ctx, "DELETE", ChartAPIURL+"/"+id, nil, nil)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return err
}
defer resp.Body.Close()

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

Expand All @@ -67,16 +60,13 @@ func (c *Client) DeleteChart(ctx context.Context, id string) error {
// GetChart gets a chart.
func (c *Client) GetChart(ctx context.Context, id string) (*chart.Chart, error) {
resp, err := c.doRequest(ctx, "GET", ChartAPIURL+"/"+id, nil, nil)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
message, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("Unexpected status code: %d: %s", resp.StatusCode, message)
if err = newResponseError(resp, http.StatusOK); err != nil {
return nil, err
}

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

resp, err := c.doRequest(ctx, "PUT", ChartAPIURL+"/"+id, nil, bytes.NewReader(payload))
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
message, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("Unexpected status code: %d: %s", resp.StatusCode, message)
if err = newResponseError(resp, http.StatusOK); err != nil {
return nil, err
}

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

resp, err := c.doRequest(ctx, "GET", ChartAPIURL, params, nil)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
message, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("Unexpected status code: %d: %s", resp.StatusCode, message)
if err = newResponseError(resp, http.StatusOK); err != nil {
return nil, err
}

finalCharts := &chart.SearchResult{}
Expand Down
Loading
Loading