Skip to content

Commit

Permalink
fix(client): correctly handle nonstandard json errors (#511)
Browse files Browse the repository at this point in the history
fix(client): correctly handle nonstandard json errors
  • Loading branch information
dbonf authored May 9, 2024
1 parent f681575 commit 5f3febf
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestAccSecureNotificationChannelTeamEmailDataSource(t *testing.T) {
func secureNotificationChannelTeamEmail(name string) string {
return fmt.Sprintf(`
resource "sysdig_secure_team" "sample_data" {
name = "secure-sample-data"
name = "secure-sample-data-%s"
all_zones = "true"
}
resource "sysdig_secure_notification_channel_team_email" "nc_team_email" {
Expand All @@ -50,5 +50,5 @@ resource "sysdig_secure_notification_channel_team_email" "nc_team_email" {
data "sysdig_secure_notification_channel_team_email" "nc_team_email" {
name = sysdig_secure_notification_channel_team_email.nc_team_email.name
}
`, name)
`, name, name)
}
9 changes: 7 additions & 2 deletions sysdig/internal/client/v2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (client *Client) ErrorFromResponse(response *http.Response) error {
return errors.New(response.Status)
}

search, err := jmespath.Search("[message, errors[].[reason, message]][][] | join(', ', @)", data)
search, err := jmespath.Search("[message, error, errors[].[reason, message]][][] | join(', ', @)", data)
if err != nil {
return errors.New(response.Status)
}
Expand All @@ -84,7 +84,12 @@ func (client *Client) ErrorFromResponse(response *http.Response) error {
return errors.New(strings.Join(cast.ToStringSlice(searchArray), ", "))
}

return errors.New(cast.ToString(search))
searchString := cast.ToString(search)
if searchString != "" {
return errors.New(searchString)
}

return errors.New(response.Status)
}

func Unmarshal[T any](data io.ReadCloser) (T, error) {
Expand Down
80 changes: 79 additions & 1 deletion sysdig/internal/client/v2/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,23 @@ func TestUnmarshal(t *testing.T) {
}
}

func TestClient_ErrorFromResponse(t *testing.T) {
func TestClient_ErrorFromResponse_non_json(t *testing.T) {

givenPayload := "non json body"
expected := "401 Unauthorized"
c := Client{}

resp := &http.Response{
Status: "401 Unauthorized",
Body: io.NopCloser(strings.NewReader(givenPayload)),
}
err := c.ErrorFromResponse(resp)
if err.Error() != expected {
t.Errorf("expected err %v, got %v", expected, err)
}
}

func TestClient_ErrorFromResponse_standard_error_format(t *testing.T) {
type Error struct {
Reason string `json:"reason"`
Message string `json:"message"`
Expand Down Expand Up @@ -94,6 +110,68 @@ func TestClient_ErrorFromResponse(t *testing.T) {
}
}

func TestClient_ErrorFromResponse_standard_error_format_2(t *testing.T) {

givenPayload := `
{
"timestamp" : 1715255725613,
"status" : 401,
"error" : "Unauthorized",
"path" : "/api/v2/alerts/46667521"
}
`
expected := "Unauthorized"
c := Client{}

resp := &http.Response{
Status: "401 Unauthorized",
Body: io.NopCloser(strings.NewReader(givenPayload)),
}
err := c.ErrorFromResponse(resp)
if err.Error() != expected {
t.Errorf("expected err %v, got %v", expected, err)
}
}

func TestClient_ErrorFromResponse_json_nonStandard_error_format(t *testing.T) {
type Error struct {
Reason string `json:"nonStandardFieldNameReason"`
Message string `json:"nonStandardFieldNameMessage"`
}

type Errors struct {
Errors []Error `json:"errors"`
}

given := Errors{
Errors: []Error{
{
Reason: "error1",
Message: "message1",
},
{
Reason: "error2",
Message: "message2",
},
},
}
expected := "401 Unauthorized"
c := Client{}
payload, err := json.Marshal(given)
if err != nil {
t.Errorf("failed to marshal errors, %v", err)
}

resp := &http.Response{
Status: "401 Unauthorized",
Body: io.NopCloser(strings.NewReader(string(payload))),
}
err = c.ErrorFromResponse(resp)
if err.Error() != expected {
t.Errorf("expected err %v, got %v", expected, err)
}
}

func TestRequest(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
agent := r.Header.Get(UserAgentHeader)
Expand Down

0 comments on commit 5f3febf

Please sign in to comment.