Skip to content

Commit

Permalink
feat: add support for out of stock error (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
jerome-quere authored Sep 26, 2019
1 parent 1a5dc47 commit 5ff34b0
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 55 deletions.
132 changes: 81 additions & 51 deletions internal/e2e/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,65 +12,95 @@ func TestStandardErrors(t *testing.T) {
client, _, err := newE2EClient(true)
testhelpers.AssertNoError(t, err)

_, err = client.GetHuman(&test.GetHumanRequest{
HumanID: "b3ba839a-dcf2-4b0a-ac81-fc32370052a0",
t.Run("not found", func(t *testing.T) {
_, err = client.GetHuman(&test.GetHumanRequest{
HumanID: "b3ba839a-dcf2-4b0a-ac81-fc32370052a0",
})
testhelpers.Equals(t, &scw.ResourceNotFoundError{
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)
})
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{
AltitudeInMeter: -7000000,
})
testhelpers.Equals(t, &scw.InvalidArgumentsError{
Details: []struct {
ArgumentName string `json:"argument_name"`
Reason string `json:"reason"`
HelpMessage string `json:"help_message"`
}{
{
ArgumentName: "altitude_in_meter",
Reason: "constraint",
HelpMessage: "lowest altitude on earth is -6371km",
t.Run("invalid argument", func(t *testing.T) {
_, err = client.CreateHuman(&test.CreateHumanRequest{
AltitudeInMeter: -7000000,
})
testhelpers.Equals(t, &scw.InvalidArgumentsError{
Details: []struct {
ArgumentName string `json:"argument_name"`
Reason string `json:"reason"`
HelpMessage string `json:"help_message"`
}{
{
ArgumentName: "altitude_in_meter",
Reason: "constraint",
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)
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
for i := 0; i < 10; i++ {
human, err = client.CreateHuman(&test.CreateHumanRequest{})
testhelpers.AssertNoError(t, err)
}
t.Run("quotas exceeded", func(t *testing.T) {
var humans []*test.Human

_, err = client.CreateHuman(&test.CreateHumanRequest{})
testhelpers.Equals(t, &scw.QuotasExceededError{
Details: []struct {
Resource string `json:"resource"`
Quota uint32 `json:"quota"`
Current uint32 `json:"current"`
}{
{
Resource: "human",
Quota: 10,
Current: 10,
for i := 0; i < 10; i++ {
human, err := client.CreateHuman(&test.CreateHumanRequest{})
testhelpers.AssertNoError(t, err)
humans = append(humans, human)
}

_, err = client.CreateHuman(&test.CreateHumanRequest{})
testhelpers.Equals(t, &scw.QuotasExceededError{
Details: []struct {
Resource string `json:"resource"`
Quota uint32 `json:"quota"`
Current uint32 `json:"current"`
}{
{
Resource: "human",
Quota: 10,
Current: 10,
},
},
},
RawBody: []byte(`{"details":[{"current":10,"quota":10,"resource":"human"}],"message":"quota(s) exceeded for this resource","type":"quotas_exceeded"}`),
}, err)
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)
for _, human := range humans {
_, err := client.DeleteHuman(&test.DeleteHumanRequest{HumanID: human.ID})
testhelpers.AssertNoError(t, err)
}
})

_, err = client.UpdateHuman(&test.UpdateHumanRequest{HumanID: human.ID})
t.Run("transient state", func(t *testing.T) {
human, err := client.CreateHuman(&test.CreateHumanRequest{})
testhelpers.AssertNoError(t, err)
defer func() {
_, _ = client.DeleteHuman(&test.DeleteHumanRequest{HumanID: human.ID})
}()

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

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)
_, 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)

})

t.Run("out of stock", func(t *testing.T) {
_, err = client.CreateHuman(&test.CreateHumanRequest{
ShoeSize: 60,
})
testhelpers.Equals(t, &scw.OutOfStockError{
Resource: "ShoeSize60",
RawBody: []byte(`{"message":"resource is out of stock","resource":"ShoeSize60","type":"out_of_stock"}`),
}, err)
})
}
22 changes: 18 additions & 4 deletions scw/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,11 @@ func unmarshalStandardError(errorType string, body []byte) SdkError {
case "transient_state":
stdErr = &TransientStateError{RawBody: body}
case "not_found":
stdErr = &ResourceNotFound{RawBody: body}
stdErr = &ResourceNotFoundError{RawBody: body}
case "permissions_denied":
stdErr = &PermissionsDeniedError{RawBody: body}
case "out_of_stock":
stdErr = &OutOfStockError{RawBody: body}
default:
return nil
}
Expand Down Expand Up @@ -209,15 +211,27 @@ func (e *TransientStateError) Error() string {
return fmt.Sprintf("scaleway-sdk-go: resource %s with ID %s is in a transient state: %s", e.Resource, e.ResourceID, e.CurrentState)
}

type ResourceNotFound struct {
type ResourceNotFoundError struct {
Resource string `json:"resource"`
ResourceID string `json:"resource_id"`

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

// IsScwSdkError implements the SdkError interface
func (e *ResourceNotFound) IsScwSdkError() {}
func (e *ResourceNotFound) Error() string {
func (e *ResourceNotFoundError) IsScwSdkError() {}
func (e *ResourceNotFoundError) Error() string {
return fmt.Sprintf("scaleway-sdk-go: resource %s with ID %s is not found", e.Resource, e.ResourceID)
}

type OutOfStockError struct {
Resource string `json:"resource"`

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

// IsScwSdkError implements the SdkError interface
func (e *OutOfStockError) IsScwSdkError() {}
func (e *OutOfStockError) Error() string {
return fmt.Sprintf("scaleway-sdk-go: resource %s is out of stock", e.Resource)
}

0 comments on commit 5ff34b0

Please sign in to comment.