diff --git a/api/account/v2alpha1/account_sdk.go b/api/account/v2alpha1/account_sdk.go index 5d738c3db..19f1b8d4d 100644 --- a/api/account/v2alpha1/account_sdk.go +++ b/api/account/v2alpha1/account_sdk.go @@ -180,7 +180,7 @@ func (r *ListSSHKeysResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListSSHKeysResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListSSHKeysResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListSSHKeysResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) diff --git a/api/baremetal/v1alpha1/baremetal_sdk.go b/api/baremetal/v1alpha1/baremetal_sdk.go index b1a8f6b42..2c73ee1ca 100644 --- a/api/baremetal/v1alpha1/baremetal_sdk.go +++ b/api/baremetal/v1alpha1/baremetal_sdk.go @@ -634,7 +634,7 @@ func (r *ListServersResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListServersResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListServersResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListServersResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) @@ -1087,7 +1087,7 @@ func (r *ListServerEventsResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListServerEventsResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListServerEventsResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListServerEventsResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) @@ -1334,7 +1334,7 @@ func (r *ListOffersResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListOffersResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListOffersResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListOffersResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) @@ -1401,7 +1401,7 @@ func (r *ListOsResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListOsResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListOsResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListOsResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) diff --git a/api/baremetal/v1alpha1/server_utils.go b/api/baremetal/v1alpha1/server_utils.go index a57683683..e4b74e3e2 100644 --- a/api/baremetal/v1alpha1/server_utils.go +++ b/api/baremetal/v1alpha1/server_utils.go @@ -17,7 +17,7 @@ type WaitForServerRequest struct { // WaitForServer wait for the server to be in a "terminal state" before returning. // This function can be used to wait for a server to be created. -func (s *API) WaitForServer(req *WaitForServerRequest) (*Server, scw.SdkError) { +func (s *API) WaitForServer(req *WaitForServerRequest) (*Server, error) { terminalStatus := map[ServerStatus]struct{}{ ServerStatusReady: {}, ServerStatusStopped: {}, @@ -59,7 +59,7 @@ type WaitForServerInstallRequest struct { // WaitForServerInstall wait for the server install to be in a // "terminal state" before returning. // This function can be used to wait for a server to be installed. -func (s *API) WaitForServerInstall(req *WaitForServerInstallRequest) (*Server, scw.SdkError) { +func (s *API) WaitForServerInstall(req *WaitForServerInstallRequest) (*Server, error) { installTerminalStatus := map[ServerInstallStatus]struct{}{ ServerInstallStatusCompleted: {}, ServerInstallStatusError: {}, diff --git a/api/instance/v1/instance_sdk.go b/api/instance/v1/instance_sdk.go index 2acc9a002..e9505ef2d 100644 --- a/api/instance/v1/instance_sdk.go +++ b/api/instance/v1/instance_sdk.go @@ -1438,7 +1438,7 @@ func (r *ListServersResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListServersResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListServersResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListServersResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) @@ -2010,7 +2010,7 @@ func (r *ListImagesResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListImagesResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListImagesResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListImagesResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) @@ -2297,7 +2297,7 @@ func (r *ListSnapshotsResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListSnapshotsResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListSnapshotsResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListSnapshotsResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) @@ -2575,7 +2575,7 @@ func (r *ListVolumesResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListVolumesResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListVolumesResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListVolumesResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) @@ -2797,7 +2797,7 @@ func (r *ListSecurityGroupsResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListSecurityGroupsResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListSecurityGroupsResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListSecurityGroupsResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) @@ -3088,7 +3088,7 @@ func (r *ListSecurityGroupRulesResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListSecurityGroupRulesResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListSecurityGroupRulesResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListSecurityGroupRulesResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) @@ -3391,7 +3391,7 @@ func (r *ListPlacementGroupsResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListPlacementGroupsResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListPlacementGroupsResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListPlacementGroupsResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) @@ -3853,7 +3853,7 @@ func (r *ListIPsResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListIPsResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListIPsResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListIPsResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) @@ -4156,7 +4156,7 @@ func (r *ListBootscriptsResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListBootscriptsResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListBootscriptsResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListBootscriptsResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) diff --git a/api/instance/v1/instance_utils.go b/api/instance/v1/instance_utils.go index ceccf9f57..17060d016 100644 --- a/api/instance/v1/instance_utils.go +++ b/api/instance/v1/instance_utils.go @@ -298,7 +298,7 @@ func (r *ListServersTypesResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListServersTypesResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListServersTypesResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListServersTypesResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) diff --git a/api/k8s/v1beta3/k8s_sdk.go b/api/k8s/v1beta3/k8s_sdk.go index 30601868c..aeb77bf42 100644 --- a/api/k8s/v1beta3/k8s_sdk.go +++ b/api/k8s/v1beta3/k8s_sdk.go @@ -703,7 +703,7 @@ func (r *ListClustersResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListClustersResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListClustersResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListClustersResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) @@ -1154,7 +1154,7 @@ func (r *ListPoolsResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListPoolsResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListPoolsResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListPoolsResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) @@ -1490,7 +1490,7 @@ func (r *ListNodesResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListNodesResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListNodesResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListNodesResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) diff --git a/api/lb/v1/lb_sdk.go b/api/lb/v1/lb_sdk.go index d545090f6..c710df07e 100644 --- a/api/lb/v1/lb_sdk.go +++ b/api/lb/v1/lb_sdk.go @@ -1265,7 +1265,7 @@ func (r *ListLbsResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListLbsResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListLbsResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListLbsResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) @@ -1515,7 +1515,7 @@ func (r *ListIPsResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListIPsResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListIPsResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListIPsResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) @@ -1669,7 +1669,7 @@ func (r *ListBackendsResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListBackendsResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListBackendsResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListBackendsResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) @@ -2306,7 +2306,7 @@ func (r *ListFrontendsResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListFrontendsResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListFrontendsResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListFrontendsResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) @@ -2653,7 +2653,7 @@ func (r *ListBackendStatsResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListBackendStatsResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListBackendStatsResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListBackendStatsResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) @@ -2731,7 +2731,7 @@ func (r *ListACLResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListACLResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListACLResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListACLResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) @@ -3045,7 +3045,7 @@ func (r *ListCertificatesResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListCertificatesResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListCertificatesResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListCertificatesResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) @@ -3229,7 +3229,7 @@ func (r *ListLbTypesResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListLbTypesResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListLbTypesResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListLbTypesResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) diff --git a/api/marketplace/v1/marketplace_sdk.go b/api/marketplace/v1/marketplace_sdk.go index 4c15462e9..6cfa674e0 100644 --- a/api/marketplace/v1/marketplace_sdk.go +++ b/api/marketplace/v1/marketplace_sdk.go @@ -204,7 +204,7 @@ func (r *ListImagesResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListImagesResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListImagesResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListImagesResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) diff --git a/api/rdb/v1/rdb_sdk.go b/api/rdb/v1/rdb_sdk.go index b56d88e58..48e122370 100644 --- a/api/rdb/v1/rdb_sdk.go +++ b/api/rdb/v1/rdb_sdk.go @@ -1074,7 +1074,7 @@ func (r *ListDatabaseBackupsResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListDatabaseBackupsResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListDatabaseBackupsResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListDatabaseBackupsResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) @@ -1394,7 +1394,7 @@ func (r *ListDatabaseEnginesResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListDatabaseEnginesResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListDatabaseEnginesResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListDatabaseEnginesResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) @@ -1516,7 +1516,7 @@ func (r *ListInstancesResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListInstancesResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListInstancesResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListInstancesResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) @@ -2086,7 +2086,7 @@ func (r *ListInstanceACLRulesResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListInstanceACLRulesResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListInstanceACLRulesResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListInstanceACLRulesResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) @@ -2296,7 +2296,7 @@ func (r *ListUsersResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListUsersResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListUsersResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListUsersResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) @@ -2521,7 +2521,7 @@ func (r *ListDatabasesResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListDatabasesResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListDatabasesResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListDatabasesResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) @@ -2687,7 +2687,7 @@ func (r *ListPrivilegesResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListPrivilegesResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListPrivilegesResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListPrivilegesResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) @@ -2804,7 +2804,7 @@ func (r *ListNodeTypesResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListNodeTypesResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListNodeTypesResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListNodeTypesResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) diff --git a/api/registry/v1/registry_sdk.go b/api/registry/v1/registry_sdk.go index 9eb75bb58..b87cde02d 100644 --- a/api/registry/v1/registry_sdk.go +++ b/api/registry/v1/registry_sdk.go @@ -484,7 +484,7 @@ func (r *ListNamespacesResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListNamespacesResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListNamespacesResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListNamespacesResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) @@ -742,7 +742,7 @@ func (r *ListImagesResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListImagesResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListImagesResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListImagesResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) @@ -950,7 +950,7 @@ func (r *ListTagsResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListTagsResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListTagsResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListTagsResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) diff --git a/api/registry/v1/registry_utils.go b/api/registry/v1/registry_utils.go index 6cf2b3674..15cbed216 100644 --- a/api/registry/v1/registry_utils.go +++ b/api/registry/v1/registry_utils.go @@ -17,7 +17,7 @@ type WaitForNamespaceRequest struct { // WaitForNamespace wait for the namespace to be in a "terminal state" before returning. // This function can be used to wait for a namespace to be ready for example. -func (s *API) WaitForNamespace(req *WaitForNamespaceRequest) (*Namespace, scw.SdkError) { +func (s *API) WaitForNamespace(req *WaitForNamespaceRequest) (*Namespace, error) { terminalStatus := map[NamespaceStatus]struct{}{ NamespaceStatusReady: {}, NamespaceStatusLocked: {}, diff --git a/api/test/v1/test_sdk.go b/api/test/v1/test_sdk.go index 6781f697c..df7c67065 100644 --- a/api/test/v1/test_sdk.go +++ b/api/test/v1/test_sdk.go @@ -317,7 +317,7 @@ func (r *ListHumansResponse) UnsafeGetTotalCount() uint32 { // UnsafeAppend should not be used // Internal usage only -func (r *ListHumansResponse) UnsafeAppend(res interface{}) (uint32, scw.SdkError) { +func (r *ListHumansResponse) UnsafeAppend(res interface{}) (uint32, error) { results, ok := res.(*ListHumansResponse) if !ok { return 0, errors.New("%T type cannot be appended to type %T", res, r) diff --git a/internal/e2e/errors_test.go b/internal/e2e/errors_test.go index c2aa273ed..73e9ca42e 100644 --- a/internal/e2e/errors_test.go +++ b/internal/e2e/errors_test.go @@ -28,11 +28,7 @@ func TestStandardErrors(t *testing.T) { AltitudeInMeter: -7000000, }) testhelpers.Equals(t, &scw.InvalidArgumentsError{ - Details: []struct { - ArgumentName string `json:"argument_name"` - Reason string `json:"reason"` - HelpMessage string `json:"help_message"` - }{ + Details: []scw.InvalidArgumentsErrorDetail{ { ArgumentName: "altitude_in_meter", Reason: "constraint", diff --git a/scw/client.go b/scw/client.go index 3fdec3894..b62123a4c 100644 --- a/scw/client.go +++ b/scw/client.go @@ -150,7 +150,7 @@ func (c *Client) Do(req *ScalewayRequest, res interface{}, opts ...RequestOption var requestNumber uint32 // do performs a single HTTP request based on the ScalewayRequest object. -func (c *Client) do(req *ScalewayRequest, res interface{}) (sdkErr SdkError) { +func (c *Client) do(req *ScalewayRequest, res interface{}) (sdkErr error) { currentRequestNumber := atomic.AddUint32(&requestNumber, 1) @@ -270,7 +270,7 @@ func (c *Client) do(req *ScalewayRequest, res interface{}) (sdkErr SdkError) { type lister interface { UnsafeGetTotalCount() uint32 - UnsafeAppend(interface{}) (uint32, SdkError) + UnsafeAppend(interface{}) (uint32, error) } type legacyLister interface { @@ -280,7 +280,7 @@ type legacyLister interface { const maxPageCount uint32 = math.MaxUint32 // doListAll collects all pages of a List request and aggregate all results on a single response. -func (c *Client) doListAll(req *ScalewayRequest, res interface{}) (err SdkError) { +func (c *Client) doListAll(req *ScalewayRequest, res interface{}) (err error) { // check for lister interface if response, isLister := res.(lister); isLister { diff --git a/scw/errors.go b/scw/errors.go index 330f6f24b..3e0a61b7e 100644 --- a/scw/errors.go +++ b/scw/errors.go @@ -5,6 +5,7 @@ import ( "fmt" "io/ioutil" "net/http" + "sort" "strings" "github.com/scaleway/scaleway-sdk-go/internal/errors" @@ -63,7 +64,7 @@ func (e *ResponseError) GetRawBody() json.RawMessage { } // hasResponseError throws an error when the HTTP status is not OK -func hasResponseError(res *http.Response) SdkError { +func hasResponseError(res *http.Response) error { if res.StatusCode >= 200 && res.StatusCode <= 299 { return nil } @@ -88,15 +89,20 @@ func hasResponseError(res *http.Response) SdkError { return errors.Wrap(err, "could not parse error response body") } - stdErr := unmarshalStandardError(newErr.Type, body) - if stdErr != nil { - return stdErr + err = unmarshalStandardError(newErr.Type, body) + if err != nil { + return err + } + + err = unmarshalNonStandardError(newErr.Type, body) + if err != nil { + return err } return newErr } -func unmarshalStandardError(errorType string, body []byte) SdkError { +func unmarshalStandardError(errorType string, body []byte) error { var stdErr SdkError switch errorType { @@ -124,12 +130,30 @@ func unmarshalStandardError(errorType string, body []byte) SdkError { return stdErr } +func unmarshalNonStandardError(errorType string, body []byte) error { + var stdErr SdkError + switch errorType { + + case "invalid_request_error": + invalidRequestError := &InvalidRequestError{RawBody: body} + err := json.Unmarshal(body, invalidRequestError) + if err != nil { + return errors.Wrap(err, "could not parse error %s response body", errorType) + } + stdErr = invalidRequestError.ToInvalidArgumentsError() + } + + return stdErr +} + +type InvalidArgumentsErrorDetail struct { + ArgumentName string `json:"argument_name"` + Reason string `json:"reason"` + HelpMessage string `json:"help_message"` +} + type InvalidArgumentsError struct { - Details []struct { - ArgumentName string `json:"argument_name"` - Reason string `json:"reason"` - HelpMessage string `json:"help_message"` - } `json:"details"` + Details []InvalidArgumentsErrorDetail `json:"details"` RawBody json.RawMessage `json:"-"` } @@ -161,6 +185,36 @@ func (e *InvalidArgumentsError) GetRawBody() json.RawMessage { return e.RawBody } +// InvalidRequestError are only returned by the compute API +// Warning: this is not a standard error. +type InvalidRequestError struct { + Fields map[string][]string `json:"fields"` + + RawBody json.RawMessage `json:"-"` +} + +// ToInvalidArgumentsError converts it to the standard error InvalidArgumentsError +func (e *InvalidRequestError) ToInvalidArgumentsError() *InvalidArgumentsError { + invalidArguments := &InvalidArgumentsError{ + RawBody: e.RawBody, + } + fieldNames := []string(nil) + for fieldName := range e.Fields { + fieldNames = append(fieldNames, fieldName) + } + sort.Strings(fieldNames) + for _, fieldName := range fieldNames { + for _, message := range e.Fields[fieldName] { + invalidArguments.Details = append(invalidArguments.Details, InvalidArgumentsErrorDetail{ + ArgumentName: fieldName, + Reason: "constraint", + HelpMessage: message, + }) + } + } + return invalidArguments +} + type QuotasExceededError struct { Details []struct { Resource string `json:"resource"` diff --git a/scw/errors_test.go b/scw/errors_test.go index 650eed9d6..77bc68582 100644 --- a/scw/errors_test.go +++ b/scw/errors_test.go @@ -5,6 +5,7 @@ import ( "encoding/json" "io/ioutil" "net/http" + "strings" "testing" "github.com/scaleway/scaleway-sdk-go/internal/testhelpers" @@ -28,6 +29,34 @@ func TestHasResponseErrorWithoutBody(t *testing.T) { } +func TestNonStandardError(t *testing.T) { + // Create expected error response + testErrorReponse := &InvalidArgumentsError{ + Details: []InvalidArgumentsErrorDetail{ + { + ArgumentName: "volumes.5.id", + Reason: "constraint", + HelpMessage: "92 is not a valid UUID.", + }, + { + ArgumentName: "volumes.5.name", + Reason: "constraint", + HelpMessage: "required key not provided", + }, + }, + RawBody: []byte(`{"fields":{"volumes.5.id":["92 is not a valid UUID."],"volumes.5.name":["required key not provided"]},"message":"Validation Error","type":"invalid_request_error"}`), + } + + // Create response body with marshalled error response + body := `{"fields":{"volumes.5.id":["92 is not a valid UUID."],"volumes.5.name":["required key not provided"]},"message":"Validation Error","type":"invalid_request_error"}` + res := &http.Response{Status: "400 Bad Request", StatusCode: 400, Body: ioutil.NopCloser(strings.NewReader(body))} + + // Test hasResponseError convert the response into a InvalidArgumentsError error + newErr := hasResponseError(res) + testhelpers.Assert(t, newErr != nil, "Should have error") + testhelpers.Equals(t, testErrorReponse, newErr) +} + func TestHasResponseErrorWithValidError(t *testing.T) { var ( errorMessage = "some message" diff --git a/scw/request.go b/scw/request.go index d7a2fcc73..f3b14d039 100644 --- a/scw/request.go +++ b/scw/request.go @@ -50,7 +50,7 @@ func (req *ScalewayRequest) getAllHeaders(token auth.Auth, userAgent string, ano } // getURL constructs a URL based on the base url and the client. -func (req *ScalewayRequest) getURL(baseURL string) (*url.URL, SdkError) { +func (req *ScalewayRequest) getURL(baseURL string) (*url.URL, error) { url, err := url.Parse(baseURL + req.Path) if err != nil { return nil, errors.New("invalid url %s: %s", baseURL+req.Path, err) diff --git a/scw/request_option.go b/scw/request_option.go index 2d541cefe..5f5e05afa 100644 --- a/scw/request_option.go +++ b/scw/request_option.go @@ -37,7 +37,7 @@ func (s *ScalewayRequest) apply(opts []RequestOption) { } } -func (s *ScalewayRequest) validate() SdkError { +func (s *ScalewayRequest) validate() error { // nothing so far return nil }