Skip to content
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

feat: add UnknownServiceError #1692

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
28 changes: 28 additions & 0 deletions scw/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ func hasResponseError(res *http.Response) error {
return err
}

if newErr.Type == "" {
err = convertNonTypedError(newErr)
if err != nil {
return err
}
}

return newErr
}

Expand Down Expand Up @@ -200,6 +207,14 @@ func unmarshalNonStandardError(errorType string, body []byte) error {
}
}

func convertNonTypedError(err *ResponseError) error {
if err.StatusCode == http.StatusNotImplemented && err.Message == "unknown service" {
return &UnknownServiceError{RawBody: err.RawBody}
}

return nil
}

type InvalidArgumentsErrorDetail struct {
ArgumentName string `json:"argument_name"`
Reason string `json:"reason"`
Expand Down Expand Up @@ -550,3 +565,16 @@ func (r PreconditionFailedError) Error() string {
}

func (r PreconditionFailedError) IsScwSdkError() {}

// UnknownServiceError implements the SdkError interface
type UnknownServiceError struct {
RawBody json.RawMessage `json:"-"`
}

func (e *UnknownServiceError) IsScwSdkError() {}
func (e *UnknownServiceError) Error() string {
return "scaleway-sdk-go: service is unknown in used locality"
}
func (e *UnknownServiceError) GetRawBody() json.RawMessage {
return e.RawBody
}
38 changes: 38 additions & 0 deletions scw/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,41 @@ func TestHasResponseErrorWithValidError(t *testing.T) {
testhelpers.Assert(t, newErr != nil, "Should have error")
testhelpers.Equals(t, testErrorReponse, newErr)
}

func TestNonTypedError(t *testing.T) {
type testCase struct {
resStatus string
resStatusCode int
resBody string
contentType string
expectedError SdkError
}

run := func(c *testCase) func(t *testing.T) {
return func(t *testing.T) {
res := &http.Response{
Status: c.resStatus,
StatusCode: c.resStatusCode,
Body: ioutil.NopCloser(strings.NewReader(c.resBody)),
Header: http.Header{
"Content-Type": []string{c.contentType},
},
}

// Test that hasResponseError converts the response to the expected SdkError.
newErr := hasResponseError(res)
testhelpers.Assert(t, newErr != nil, "Should have error")
testhelpers.Equals(t, c.expectedError, newErr)
}
}

t.Run("unknown service", run(&testCase{
resStatus: "501 Not Implemented",
resStatusCode: http.StatusNotImplemented,
contentType: "application/json",
resBody: `{"message": "unknown service"}`,
expectedError: &UnknownServiceError{
RawBody: []byte(`{"message": "unknown service"}`),
},
}))
}