Skip to content

Commit

Permalink
feat: Improve error message handling and avoid memory leaks (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
tembleking authored May 4, 2021
1 parent e1a2da8 commit bcca50e
Show file tree
Hide file tree
Showing 15 changed files with 208 additions and 284 deletions.
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ require (
github.com/apparentlymart/go-cidr v1.1.0 // indirect
github.com/aws/aws-sdk-go v1.37.10 // indirect
github.com/fatih/color v1.10.0 // indirect
github.com/golang/protobuf v1.4.3 // indirect
github.com/google/go-cmp v0.5.4 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
Expand All @@ -20,8 +18,8 @@ require (
github.com/hashicorp/go-uuid v1.0.2 // indirect
github.com/hashicorp/hcl/v2 v2.8.2 // indirect
github.com/hashicorp/terraform-plugin-sdk/v2 v2.4.3
github.com/hashicorp/terraform-plugin-test/v2 v2.1.2 // indirect
github.com/hashicorp/yamux v0.0.0-20200609203250-aecfd211c9ce // indirect
github.com/jmespath/go-jmespath v0.4.0
github.com/klauspost/compress v1.11.7 // indirect
github.com/mitchellh/copystructure v1.1.1 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
Expand All @@ -35,6 +33,5 @@ require (
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect
golang.org/x/oauth2 v0.0.0-20210210192628-66670185b0cd // indirect
google.golang.org/api v0.40.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20210212180131-e7f2df4ecc2d // indirect
)
131 changes: 8 additions & 123 deletions go.sum

Large diffs are not rendered by default.

46 changes: 24 additions & 22 deletions sysdig/internal/client/monitor/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package monitor

import (
"context"
"errors"
"fmt"
"io/ioutil"
"net/http"
Expand All @@ -13,26 +12,31 @@ func (c *sysdigMonitorClient) CreateAlert(ctx context.Context, alert Alert) (cre
if err != nil {
return
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
defer response.Body.Close()

if response.StatusCode != http.StatusOK {
err = errorFromResponse(response)
return
}

if response.StatusCode != 200 {
err = errors.New(string(body))
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return
}

defer response.Body.Close()

return AlertFromJSON(body), nil
}

func (c *sysdigMonitorClient) DeleteAlert(ctx context.Context, alertID int) error {
response, err := c.doSysdigMonitorRequest(ctx, http.MethodDelete, c.alertURL(alertID), nil)

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

if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK {
return errorFromResponse(response)
}

return err
}

Expand All @@ -41,19 +45,17 @@ func (c *sysdigMonitorClient) UpdateAlert(ctx context.Context, alert Alert) (upd
if err != nil {
return
}
defer response.Body.Close()

body, err := ioutil.ReadAll(response.Body)
if err != nil {
if response.StatusCode != 200 {
err = errorFromResponse(response)
return
}

if response.StatusCode != 200 {
err = errors.New(string(body))
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return
}

defer response.Body.Close()

return AlertFromJSON(body), nil
}

Expand All @@ -62,17 +64,17 @@ func (c *sysdigMonitorClient) GetAlertById(ctx context.Context, alertID int) (al
if err != nil {
return
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return
}
defer response.Body.Close()

if response.StatusCode != 200 {
err = errors.New(string(body))
err = errorFromResponse(response)
return
}

defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return
}

return AlertFromJSON(body), nil
}
Expand Down
57 changes: 25 additions & 32 deletions sysdig/internal/client/monitor/dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,76 +10,69 @@ import (
)

func (client *sysdigMonitorClient) GetDashboardByID(ctx context.Context, ID int) (*model.Dashboard, error) {
res, err := client.doSysdigMonitorRequest(ctx, http.MethodGet, client.getDashboardUrl(ID), nil)
response, err := client.doSysdigMonitorRequest(ctx, http.MethodGet, client.getDashboardUrl(ID), nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
defer response.Body.Close()

body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, nil
if response.StatusCode != http.StatusOK {
return nil, errorFromResponse(response)
}

if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf(string(body))
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, nil
}

return model.DashboardFromJSON(body), nil
}

func (client *sysdigMonitorClient) CreateDashboard(ctx context.Context, dashboard *model.Dashboard) (*model.Dashboard, error) {
res, err := client.doSysdigMonitorRequest(ctx, http.MethodPost, client.getDashboardsUrl(), dashboard.ToJSON())
response, err := client.doSysdigMonitorRequest(ctx, http.MethodPost, client.getDashboardsUrl(), dashboard.ToJSON())
if err != nil {
return nil, err
}
defer res.Body.Close()
defer response.Body.Close()

body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated {
return nil, errorFromResponse(response)
}

if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusCreated {
return nil, fmt.Errorf(string(body))
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}

return model.DashboardFromJSON(body), nil
}

func (client *sysdigMonitorClient) UpdateDashboard(ctx context.Context, dashboard *model.Dashboard) (*model.Dashboard, error) {
res, err := client.doSysdigMonitorRequest(ctx, http.MethodPut, client.getDashboardUrl(dashboard.ID), dashboard.ToJSON())
response, err := client.doSysdigMonitorRequest(ctx, http.MethodPut, client.getDashboardUrl(dashboard.ID), dashboard.ToJSON())
if err != nil {
return nil, err
}
defer res.Body.Close()
defer response.Body.Close()

body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated {
return nil, errorFromResponse(response)
}

if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusCreated {
return nil, fmt.Errorf(string(body))
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}

return model.DashboardFromJSON(body), nil
}

func (client *sysdigMonitorClient) DeleteDashboard(ctx context.Context, ID int) error {
res, err := client.doSysdigMonitorRequest(ctx, http.MethodDelete, client.getDashboardUrl(ID), nil)
response, err := client.doSysdigMonitorRequest(ctx, http.MethodDelete, client.getDashboardUrl(ID), nil)
if err != nil {
return err
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusNoContent {
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
defer response.Body.Close()

return fmt.Errorf(string(body))
if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK {
return errorFromResponse(response)
}

return nil
Expand Down
30 changes: 30 additions & 0 deletions sysdig/internal/client/monitor/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package monitor

import (
"encoding/json"
"errors"
"net/http"
"strings"

"github.com/jmespath/go-jmespath"
"github.com/spf13/cast"
)

func errorFromResponse(response *http.Response) error {
var data interface{}
err := json.NewDecoder(response.Body).Decode(&data)
if err != nil {
return errors.New(response.Status)
}

search, err := jmespath.Search("[message, errors[].[reason, message]][][] | join(', ', @)", data)
if err != nil {
return errors.New(response.Status)
}

if searchArray, ok := search.([]interface{}); ok {
return errors.New(strings.Join(cast.ToStringSlice(searchArray), ", "))
}

return errors.New(cast.ToString(search))
}
20 changes: 8 additions & 12 deletions sysdig/internal/client/monitor/notification_channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package monitor

import (
"context"
"errors"
"fmt"
"io/ioutil"
"net/http"
Expand All @@ -15,13 +14,12 @@ func (client *sysdigMonitorClient) GetNotificationChannelById(ctx context.Contex
}
defer response.Body.Close()

body, _ := ioutil.ReadAll(response.Body)

if response.StatusCode != http.StatusOK {
err = errors.New(response.Status)
err = errorFromResponse(response)
return
}

body, _ := ioutil.ReadAll(response.Body)
nc = NotificationChannelFromJSON(body)

if nc.Version == 0 {
Expand All @@ -41,7 +39,7 @@ func (client *sysdigMonitorClient) GetNotificationChannelByName(ctx context.Cont
body, _ := ioutil.ReadAll(response.Body)

if response.StatusCode != http.StatusOK {
err = errors.New(response.Status)
err = errorFromResponse(response)
return
}

Expand All @@ -65,13 +63,12 @@ func (client *sysdigMonitorClient) CreateNotificationChannel(ctx context.Context
}
defer response.Body.Close()

body, _ := ioutil.ReadAll(response.Body)

if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated {
err = errors.New(response.Status)
err = errorFromResponse(response)
return
}

body, _ := ioutil.ReadAll(response.Body)
nc = NotificationChannelFromJSON(body)
return
}
Expand All @@ -83,13 +80,12 @@ func (client *sysdigMonitorClient) UpdateNotificationChannel(ctx context.Context
}
defer response.Body.Close()

body, _ := ioutil.ReadAll(response.Body)

if response.StatusCode != http.StatusOK {
err = errors.New(response.Status)
err = errorFromResponse(response)
return
}

body, _ := ioutil.ReadAll(response.Body)
nc = NotificationChannelFromJSON(body)
return
}
Expand All @@ -102,7 +98,7 @@ func (client *sysdigMonitorClient) DeleteNotificationChannel(ctx context.Context
defer response.Body.Close()

if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK {
return errors.New(response.Status)
return errorFromResponse(response)
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion sysdig/internal/client/monitor/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (client *sysdigMonitorClient) DeleteTeam(ctx context.Context, id int) error
defer response.Body.Close()

if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK {
return errors.New(response.Status)
return errorFromResponse(response)
}
return nil
}
Expand Down
30 changes: 30 additions & 0 deletions sysdig/internal/client/secure/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package secure

import (
"encoding/json"
"errors"
"net/http"
"strings"

"github.com/jmespath/go-jmespath"
"github.com/spf13/cast"
)

func errorFromResponse(response *http.Response) error {
var data interface{}
err := json.NewDecoder(response.Body).Decode(&data)
if err != nil {
return errors.New(response.Status)
}

search, err := jmespath.Search("[message, errors[].[reason, message]][][] | join(', ', @)", data)
if err != nil {
return errors.New(response.Status)
}

if searchArray, ok := search.([]interface{}); ok {
return errors.New(strings.Join(cast.ToStringSlice(searchArray), ", "))
}

return errors.New(cast.ToString(search))
}
Loading

0 comments on commit bcca50e

Please sign in to comment.