Skip to content

feat: add raw body to standard errors #191

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

Merged
merged 2 commits into from
Sep 23, 2019
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
5 changes: 5 additions & 0 deletions internal/e2e/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestStandardErrors(t *testing.T) {
testhelpers.Equals(t, &scw.ResourceNotFound{
Resource: "human",
ResourceID: "b3ba839a-dcf2-4b0a-ac81-fc32370052a0",
RawBody: []byte(`{"message":"resource is not found","resource":"human","resource_id":"b3ba839a-dcf2-4b0a-ac81-fc32370052a0","type":"not_found"}`),
}, err)

_, err = client.CreateHuman(&test.CreateHumanRequest{
Expand All @@ -35,6 +36,7 @@ func TestStandardErrors(t *testing.T) {
HelpMessage: "lowest altitude on earth is -6371km",
},
},
RawBody: []byte(`{"details":[{"argument_name":"altitude_in_meter","help_message":"lowest altitude on earth is -6371km","reason":"constraint"}],"message":"invalid argument(s)","type":"invalid_arguments"}`),
}, err)

var human *test.Human
Expand All @@ -56,16 +58,19 @@ func TestStandardErrors(t *testing.T) {
Current: 10,
},
},
RawBody: []byte(`{"details":[{"current":10,"quota":10,"resource":"human"}],"message":"quota(s) exceeded for this resource","type":"quotas_exceeded"}`),
}, err)

_, err = client.RunHuman(&test.RunHumanRequest{HumanID: human.ID})
testhelpers.AssertNoError(t, err)

_, err = client.UpdateHuman(&test.UpdateHumanRequest{HumanID: human.ID})

testhelpers.Equals(t, &scw.TransientStateError{
Resource: "human",
ResourceID: human.ID,
CurrentState: "running",
RawBody: []byte(`{"current_state":"running","message":"resource is in a transient state","resource":"human","resource_id":"` + human.ID + `","type":"transient_state"}`),
}, err)

}
24 changes: 19 additions & 5 deletions scw/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type ResponseError struct {

// Status is the HTTP status received
Status string `json:"-"`

RawBody json.RawMessage `json:"-"`
}

func (e *ResponseError) Error() string {
Expand Down Expand Up @@ -77,11 +79,13 @@ func hasResponseError(res *http.Response) SdkError {
if err != nil {
return errors.Wrap(err, "cannot read error response body")
}
newErr.RawBody = body

err = json.Unmarshal(body, newErr)
if err != nil {
return errors.Wrap(err, "could not parse error response body")
}

stdErr := unmarshalStandardError(newErr.Type, body)
if stdErr != nil {
return stdErr
Expand All @@ -95,15 +99,15 @@ func unmarshalStandardError(errorType string, body []byte) SdkError {

switch errorType {
case "invalid_arguments":
stdErr = &InvalidArgumentsError{}
stdErr = &InvalidArgumentsError{RawBody: body}
case "quotas_exceeded":
stdErr = &QuotasExceededError{}
stdErr = &QuotasExceededError{RawBody: body}
case "transient_state":
stdErr = &TransientStateError{}
stdErr = &TransientStateError{RawBody: body}
case "not_found":
stdErr = &ResourceNotFound{}
stdErr = &ResourceNotFound{RawBody: body}
case "permissions_denied":
stdErr = &PermissionsDeniedError{}
stdErr = &PermissionsDeniedError{RawBody: body}
default:
return nil
}
Expand All @@ -122,6 +126,8 @@ type InvalidArgumentsError struct {
Reason string `json:"reason"`
HelpMessage string `json:"help_message"`
} `json:"details"`

RawBody json.RawMessage `json:"-"`
}

// IsScwSdkError implements the SdkError interface
Expand Down Expand Up @@ -154,6 +160,8 @@ type QuotasExceededError struct {
Quota uint32 `json:"quota"`
Current uint32 `json:"current"`
} `json:"details"`

RawBody json.RawMessage `json:"-"`
}

// IsScwSdkError implements the SdkError interface
Expand All @@ -172,6 +180,8 @@ type PermissionsDeniedError struct {
Resource string `json:"resource"`
Action string `json:"action"`
} `json:"details"`

RawBody json.RawMessage `json:"-"`
}

// IsScwSdkError implements the SdkError interface
Expand All @@ -189,6 +199,8 @@ type TransientStateError struct {
Resource string `json:"resource"`
ResourceID string `json:"resource_id"`
CurrentState string `json:"current_state"`

RawBody json.RawMessage `json:"-"`
}

// IsScwSdkError implements the SdkError interface
Expand All @@ -200,6 +212,8 @@ func (e *TransientStateError) Error() string {
type ResourceNotFound struct {
Resource string `json:"resource"`
ResourceID string `json:"resource_id"`

RawBody json.RawMessage `json:"-"`
}

// IsScwSdkError implements the SdkError interface
Expand Down
2 changes: 1 addition & 1 deletion scw/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func TestHasResponseErrorWithoutBody(t *testing.T) {
}

func TestHasResponseErrorWithValidError(t *testing.T) {

var (
errorMessage = "some message"
errorType = "some type"
Expand All @@ -45,6 +44,7 @@ func TestHasResponseErrorWithValidError(t *testing.T) {
Fields: errorFields,
StatusCode: errorStatusCode,
Status: errorStatus,
RawBody: []byte(`{"message":"some message","type":"some type","fields":{"some_field":["some_value"]}}`),
}

// Create response body with marshalled error response
Expand Down