Skip to content
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
30 changes: 30 additions & 0 deletions scw/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/scaleway/scaleway-sdk-go/internal/errors"
"github.com/scaleway/scaleway-sdk-go/validation"
)

// SdkError is a base interface for all Scaleway SDK errors.
Expand Down Expand Up @@ -160,6 +161,14 @@ func unmarshalNonStandardError(errorType string, body []byte) error {
switch errorType {
// Only in instance API.

case "unknown_resource":
unknownResourceError := &UnknownResource{RawBody: body}
err := json.Unmarshal(body, unknownResourceError)
if err != nil {
return errors.Wrap(err, "could not parse error %s response body", errorType)
}
return unknownResourceError.ToResourceNotFoundError()

case "invalid_request_error":
invalidRequestError := &InvalidRequestError{RawBody: body}
err := json.Unmarshal(body, invalidRequestError)
Expand Down Expand Up @@ -225,6 +234,27 @@ func (e *InvalidArgumentsError) GetRawBody() json.RawMessage {
return e.RawBody
}

// UnknownResource is only returned by the instance API.
// Warning: this is not a standard error.
type UnknownResource struct {
Message string `json:"message"`
RawBody json.RawMessage `json:"-"`
}

// ToSdkError returns a standard error InvalidArgumentsError or nil Fields is nil.
func (e *UnknownResource) ToResourceNotFoundError() SdkError {
resourceNotFound := &ResourceNotFoundError{
RawBody: e.RawBody,
}

resourceNotFound.ResourceID = strings.TrimSuffix(e.Message, `" not found`)
resourceNotFound.ResourceID = strings.TrimPrefix(resourceNotFound.ResourceID, `"`)
if !validation.IsUUID(resourceNotFound.ResourceID) {
return nil
}
return resourceNotFound
}

// InvalidRequestError is only returned by the instance API.
// Warning: this is not a standard error.
type InvalidRequestError struct {
Expand Down
11 changes: 11 additions & 0 deletions scw/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ func TestNonStandardError(t *testing.T) {
},
}))

t.Run("unknown_resource ", run(&testCase{
resStatus: "404 Not Found",
resStatusCode: http.StatusNotFound,
contentType: "application/json",
resBody: `{"type": "unknown_resource", "message": "\"11111111-1111-4111-8111-111111111111\" not found"}`,
expectedError: &ResourceNotFoundError{
ResourceID: "11111111-1111-4111-8111-111111111111",
RawBody: []byte(`{"type": "unknown_resource", "message": "\"11111111-1111-4111-8111-111111111111\" not found"}`),
},
}))

t.Run("conflict type", run(&testCase{
resStatus: "409 Conflict",
resStatusCode: http.StatusConflict,
Expand Down