diff --git a/services/argus/api_default.go b/services/argus/api_default.go index 3d56f211..9776df14 100644 --- a/services/argus/api_default.go +++ b/services/argus/api_default.go @@ -18,6 +18,8 @@ import ( "net/http" "net/url" "strings" + + oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" ) // DefaultApiService DefaultApi service @@ -40,7 +42,7 @@ func (r ApiCreateCredentialRequest) Execute() (*ApiUserProjectCreated, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.CreateCredential") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/credentials" @@ -86,30 +88,30 @@ func (r ApiCreateCredentialRequest) Execute() (*ApiUserProjectCreated, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 403 { var v PermissionDenied err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -168,7 +170,7 @@ func (r ApiCreateInstanceRequest) Execute() (*CreateInstanceResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.CreateInstance") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances" @@ -218,41 +220,41 @@ func (r ApiCreateInstanceRequest) Execute() (*CreateInstanceResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 403 { var v PermissionDenied err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -309,7 +311,7 @@ func (r ApiCreateScrapeConfigRequest) Execute() (*ScrapeConfigsResponse, error) a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.CreateScrapeConfig") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/scrapeconfigs" @@ -360,41 +362,41 @@ func (r ApiCreateScrapeConfigRequest) Execute() (*ScrapeConfigsResponse, error) } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 403 { var v PermissionDenied err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -449,7 +451,7 @@ func (r ApiDeleteCredentialRequest) Execute() (*Message, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.DeleteCredential") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/credentials/{username}" @@ -496,52 +498,52 @@ func (r ApiDeleteCredentialRequest) Execute() (*Message, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 403 { var v PermissionDenied err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 500 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -598,7 +600,7 @@ func (r ApiDeleteInstanceRequest) Execute() (*ProjectInstancesUpdateResponse, er a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.DeleteInstance") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}" @@ -644,30 +646,30 @@ func (r ApiDeleteInstanceRequest) Execute() (*ProjectInstancesUpdateResponse, er } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 403 { var v PermissionDenied err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -722,7 +724,7 @@ func (r ApiDeleteScrapeConfigRequest) Execute() (*ScrapeConfigsResponse, error) a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.DeleteScrapeConfig") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/scrapeconfigs/{jobName}" @@ -769,41 +771,41 @@ func (r ApiDeleteScrapeConfigRequest) Execute() (*ScrapeConfigsResponse, error) } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 403 { var v PermissionDenied err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -861,7 +863,7 @@ func (r ApiGetCredentialRequest) Execute() (*ServiceKeysResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetCredential") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/credentials/{username}" @@ -908,41 +910,41 @@ func (r ApiGetCredentialRequest) Execute() (*ServiceKeysResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 403 { var v PermissionDenied err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -999,7 +1001,7 @@ func (r ApiGetCredentialsRequest) Execute() (*CredentialsListResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetCredentials") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/credentials" @@ -1045,30 +1047,30 @@ func (r ApiGetCredentialsRequest) Execute() (*CredentialsListResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 403 { var v PermissionDenied err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1122,7 +1124,7 @@ func (r ApiGetInstanceRequest) Execute() (*InstanceResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetInstance") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}" @@ -1168,30 +1170,30 @@ func (r ApiGetInstanceRequest) Execute() (*InstanceResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 403 { var v PermissionDenied err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1244,7 +1246,7 @@ func (r ApiGetInstancesRequest) Execute() (*ProjectInstanceFullMany, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetInstances") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances" @@ -1289,30 +1291,30 @@ func (r ApiGetInstancesRequest) Execute() (*ProjectInstanceFullMany, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 403 { var v PermissionDenied err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1362,7 +1364,7 @@ func (r ApiGetPlansRequest) Execute() (*PlansResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetPlans") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/plans" @@ -1407,30 +1409,30 @@ func (r ApiGetPlansRequest) Execute() (*PlansResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 403 { var v PermissionDenied err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1482,7 +1484,7 @@ func (r ApiGetScrapeConfigRequest) Execute() (*ScrapeConfigResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetScrapeConfig") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/scrapeconfigs/{jobName}" @@ -1529,30 +1531,30 @@ func (r ApiGetScrapeConfigRequest) Execute() (*ScrapeConfigResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 403 { var v PermissionDenied err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1609,7 +1611,7 @@ func (r ApiGetScrapeConfigsRequest) Execute() (*ScrapeConfigsResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetScrapeConfigs") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/scrapeconfigs" @@ -1655,30 +1657,30 @@ func (r ApiGetScrapeConfigsRequest) Execute() (*ScrapeConfigsResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 403 { var v PermissionDenied err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1738,7 +1740,7 @@ func (r ApiUpdateInstanceRequest) Execute() (*ProjectInstancesUpdateResponse, er a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.UpdateInstance") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}" @@ -1789,41 +1791,41 @@ func (r ApiUpdateInstanceRequest) Execute() (*ProjectInstancesUpdateResponse, er } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 403 { var v PermissionDenied err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1884,7 +1886,7 @@ func (r ApiUpdateScrapeConfigRequest) Execute() (*ScrapeConfigsResponse, error) a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.UpdateScrapeConfig") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/scrapeconfigs/{jobName}" @@ -1936,52 +1938,52 @@ func (r ApiUpdateScrapeConfigRequest) Execute() (*ScrapeConfigsResponse, error) } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 403 { var v PermissionDenied err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } diff --git a/services/argus/client.go b/services/argus/client.go index 161e99ad..f3f3b083 100644 --- a/services/argus/client.go +++ b/services/argus/client.go @@ -15,7 +15,6 @@ import ( "context" "encoding/json" "encoding/xml" - "errors" "fmt" "io" "log" @@ -36,11 +35,6 @@ import ( "github.com/stackitcloud/stackit-sdk-go/core/config" ) -var ( - // When a response has a bad status, this limits the number of characters that are shown from the response body - ApiErrorMaxCharacterLimit = 500 -) - var ( jsonCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:vnd\.[^;]+\+)?json)`) xmlCheck = regexp.MustCompile(`(?i:(?:application|text)/xml)`) @@ -325,7 +319,7 @@ func (c *APIClient) prepareRequest( // add form parameters and file if available. if strings.HasPrefix(headerParams["Content-Type"], "multipart/form-data") && len(formParams) > 0 || (len(formFiles) > 0) { if body != nil { - return nil, errors.New("Cannot specify postBody and multipart form at the same time.") + return nil, fmt.Errorf("cannot specify postBody and multipart form at the same time.") } body = &bytes.Buffer{} w := multipart.NewWriter(body) @@ -366,7 +360,7 @@ func (c *APIClient) prepareRequest( if strings.HasPrefix(headerParams["Content-Type"], "application/x-www-form-urlencoded") && len(formParams) > 0 { if body != nil { - return nil, errors.New("Cannot specify postBody and x-www-form-urlencoded form at the same time.") + return nil, fmt.Errorf("cannot specify postBody and x-www-form-urlencoded form at the same time.") } body = &bytes.Buffer{} body.WriteString(formParams.Encode()) @@ -486,14 +480,14 @@ func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err err return err } } else { - return errors.New("Unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined") + return fmt.Errorf("unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined") } } else if err = json.Unmarshal(b, v); err != nil { // simple model return err } return nil } - return errors.New("undefined response type") + return fmt.Errorf("undefined response type") } // Add a file to the multipart request @@ -550,7 +544,7 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e } if bodyBuf.Len() == 0 { - err = fmt.Errorf("invalid body type %s\n", contentType) + err = fmt.Errorf("invalid body type %s", contentType) return nil, err } return bodyBuf, nil @@ -630,73 +624,3 @@ func CacheExpires(r *http.Response) time.Time { func strlen(s string) int { return utf8.RuneCountInString(s) } - -// GenericOpenAPIError Provides access to the body, error and model on returned errors. -type GenericOpenAPIError struct { - statusCode int - body []byte - error string - model interface{} -} - -// Error returns non-empty string if there was an error. -func (e GenericOpenAPIError) Error() string { - // Prevent panic in case of negative value - if ApiErrorMaxCharacterLimit < 0 { - ApiErrorMaxCharacterLimit = 500 - } - - if len(e.body) <= ApiErrorMaxCharacterLimit { - return fmt.Sprintf("%s, status code %d, body: %s\n", e.error, e.statusCode, string(e.body)) - } else { - indexStart := ApiErrorMaxCharacterLimit / 2 - indexEnd := len(e.body) - ApiErrorMaxCharacterLimit/2 - numberTruncatedCharacters := indexEnd - indexStart - return fmt.Sprintf( - "%s, status code %d, body: %s [...truncated %d characters...] %s", - e.error, - e.statusCode, - string(e.body[:indexStart]), - numberTruncatedCharacters, - string(e.body[indexEnd:]), - ) - } -} - -// StatusCode returns the status code of the response -func (e GenericOpenAPIError) StatusCode() int { - return e.statusCode -} - -// Body returns the raw bytes of the response -func (e GenericOpenAPIError) Body() []byte { - return e.body -} - -// Model returns the unpacked model of the error -func (e GenericOpenAPIError) Model() interface{} { - return e.model -} - -// format error message using title and detail when model implements rfc7807 -func formatErrorMessage(status string, v interface{}) string { - str := "" - metaValue := reflect.ValueOf(v).Elem() - switch metaValue.Kind() { - case reflect.Map: - return status - case reflect.Struct: - field := metaValue.FieldByName("Title") - if field != (reflect.Value{}) { - str = fmt.Sprintf("%s", field.Interface()) - } - field = metaValue.FieldByName("Detail") - if field != (reflect.Value{}) { - str = fmt.Sprintf("%s (%s)", str, field.Interface()) - } - - // status title (detail) - return strings.TrimSpace(fmt.Sprintf("%s %s", status, str)) - } - return status -} diff --git a/services/dns/api_default.go b/services/dns/api_default.go index 1a88ce41..19b4ece0 100644 --- a/services/dns/api_default.go +++ b/services/dns/api_default.go @@ -18,6 +18,8 @@ import ( "net/http" "net/url" "strings" + + oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" ) // DefaultApiService DefaultApi service @@ -48,7 +50,7 @@ func (r ApiCreateRecordSetRequest) Execute() (*RecordSetResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.CreateRecordSet") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/zones/{zoneId}/rrsets" @@ -99,63 +101,63 @@ func (r ApiCreateRecordSetRequest) Execute() (*RecordSetResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 409 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 502 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -216,7 +218,7 @@ func (r ApiCreateZoneRequest) Execute() (*ZoneResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.CreateZone") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/zones" @@ -266,63 +268,63 @@ func (r ApiCreateZoneRequest) Execute() (*ZoneResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 409 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 502 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -374,7 +376,7 @@ func (r ApiDeleteRecordSetRequest) Execute() (*Message, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.DeleteRecordSet") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/zones/{zoneId}/rrsets/{rrSetId}" @@ -421,52 +423,52 @@ func (r ApiDeleteRecordSetRequest) Execute() (*Message, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 502 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -523,7 +525,7 @@ func (r ApiDeleteZoneRequest) Execute() (*Message, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.DeleteZone") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/zones/{zoneId}" @@ -569,52 +571,52 @@ func (r ApiDeleteZoneRequest) Execute() (*Message, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 502 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -669,7 +671,7 @@ func (r ApiGetRecordSetRequest) Execute() (*RecordSetResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetRecordSet") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/zones/{zoneId}/rrsets/{rrSetId}" @@ -716,41 +718,41 @@ func (r ApiGetRecordSetRequest) Execute() (*RecordSetResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 502 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1063,7 +1065,7 @@ func (r ApiGetRecordSetsRequest) Execute() (*RecordSetsResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetRecordSets") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/zones/{zoneId}/rrsets" @@ -1205,41 +1207,41 @@ func (r ApiGetRecordSetsRequest) Execute() (*RecordSetsResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 502 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1293,7 +1295,7 @@ func (r ApiGetZoneRequest) Execute() (*ZoneResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetZone") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/zones/{zoneId}" @@ -1339,41 +1341,41 @@ func (r ApiGetZoneRequest) Execute() (*ZoneResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 502 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1762,7 +1764,7 @@ func (r ApiGetZonesRequest) Execute() (*ZonesResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetZones") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/zones" @@ -1933,41 +1935,41 @@ func (r ApiGetZonesRequest) Execute() (*ZonesResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 502 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -2027,7 +2029,7 @@ func (r ApiUpdateRecordRequest) Execute() (*Message, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.UpdateRecord") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/zones/{zoneId}/rrsets/{rrSetId}/records" @@ -2079,52 +2081,52 @@ func (r ApiUpdateRecordRequest) Execute() (*Message, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 502 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -2190,7 +2192,7 @@ func (r ApiUpdateRecordSetRequest) Execute() (*Message, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.UpdateRecordSet") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/zones/{zoneId}/rrsets/{rrSetId}" @@ -2242,52 +2244,52 @@ func (r ApiUpdateRecordSetRequest) Execute() (*Message, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 502 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -2352,7 +2354,7 @@ func (r ApiUpdateZoneRequest) Execute() (*ZoneResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.UpdateZone") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/zones/{zoneId}" @@ -2403,52 +2405,52 @@ func (r ApiUpdateZoneRequest) Execute() (*ZoneResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 502 { var v Message err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } diff --git a/services/dns/client.go b/services/dns/client.go index 85b32788..ab8e9028 100644 --- a/services/dns/client.go +++ b/services/dns/client.go @@ -15,7 +15,6 @@ import ( "context" "encoding/json" "encoding/xml" - "errors" "fmt" "io" "log" @@ -36,11 +35,6 @@ import ( "github.com/stackitcloud/stackit-sdk-go/core/config" ) -var ( - // When a response has a bad status, this limits the number of characters that are shown from the response body - ApiErrorMaxCharacterLimit = 500 -) - var ( jsonCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:vnd\.[^;]+\+)?json)`) xmlCheck = regexp.MustCompile(`(?i:(?:application|text)/xml)`) @@ -325,7 +319,7 @@ func (c *APIClient) prepareRequest( // add form parameters and file if available. if strings.HasPrefix(headerParams["Content-Type"], "multipart/form-data") && len(formParams) > 0 || (len(formFiles) > 0) { if body != nil { - return nil, errors.New("Cannot specify postBody and multipart form at the same time.") + return nil, fmt.Errorf("cannot specify postBody and multipart form at the same time.") } body = &bytes.Buffer{} w := multipart.NewWriter(body) @@ -366,7 +360,7 @@ func (c *APIClient) prepareRequest( if strings.HasPrefix(headerParams["Content-Type"], "application/x-www-form-urlencoded") && len(formParams) > 0 { if body != nil { - return nil, errors.New("Cannot specify postBody and x-www-form-urlencoded form at the same time.") + return nil, fmt.Errorf("cannot specify postBody and x-www-form-urlencoded form at the same time.") } body = &bytes.Buffer{} body.WriteString(formParams.Encode()) @@ -486,14 +480,14 @@ func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err err return err } } else { - return errors.New("Unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined") + return fmt.Errorf("unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined") } } else if err = json.Unmarshal(b, v); err != nil { // simple model return err } return nil } - return errors.New("undefined response type") + return fmt.Errorf("undefined response type") } // Add a file to the multipart request @@ -550,7 +544,7 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e } if bodyBuf.Len() == 0 { - err = fmt.Errorf("invalid body type %s\n", contentType) + err = fmt.Errorf("invalid body type %s", contentType) return nil, err } return bodyBuf, nil @@ -630,73 +624,3 @@ func CacheExpires(r *http.Response) time.Time { func strlen(s string) int { return utf8.RuneCountInString(s) } - -// GenericOpenAPIError Provides access to the body, error and model on returned errors. -type GenericOpenAPIError struct { - statusCode int - body []byte - error string - model interface{} -} - -// Error returns non-empty string if there was an error. -func (e GenericOpenAPIError) Error() string { - // Prevent panic in case of negative value - if ApiErrorMaxCharacterLimit < 0 { - ApiErrorMaxCharacterLimit = 500 - } - - if len(e.body) <= ApiErrorMaxCharacterLimit { - return fmt.Sprintf("%s, status code %d, body: %s\n", e.error, e.statusCode, string(e.body)) - } else { - indexStart := ApiErrorMaxCharacterLimit / 2 - indexEnd := len(e.body) - ApiErrorMaxCharacterLimit/2 - numberTruncatedCharacters := indexEnd - indexStart - return fmt.Sprintf( - "%s, status code %d, body: %s [...truncated %d characters...] %s", - e.error, - e.statusCode, - string(e.body[:indexStart]), - numberTruncatedCharacters, - string(e.body[indexEnd:]), - ) - } -} - -// StatusCode returns the status code of the response -func (e GenericOpenAPIError) StatusCode() int { - return e.statusCode -} - -// Body returns the raw bytes of the response -func (e GenericOpenAPIError) Body() []byte { - return e.body -} - -// Model returns the unpacked model of the error -func (e GenericOpenAPIError) Model() interface{} { - return e.model -} - -// format error message using title and detail when model implements rfc7807 -func formatErrorMessage(status string, v interface{}) string { - str := "" - metaValue := reflect.ValueOf(v).Elem() - switch metaValue.Kind() { - case reflect.Map: - return status - case reflect.Struct: - field := metaValue.FieldByName("Title") - if field != (reflect.Value{}) { - str = fmt.Sprintf("%s", field.Interface()) - } - field = metaValue.FieldByName("Detail") - if field != (reflect.Value{}) { - str = fmt.Sprintf("%s (%s)", str, field.Interface()) - } - - // status title (detail) - return strings.TrimSpace(fmt.Sprintf("%s %s", status, str)) - } - return status -} diff --git a/services/loadbalancer/api_default.go b/services/loadbalancer/api_default.go index f6b20fac..760563c0 100644 --- a/services/loadbalancer/api_default.go +++ b/services/loadbalancer/api_default.go @@ -18,6 +18,8 @@ import ( "net/http" "net/url" "strings" + + oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" ) // DefaultApiService DefaultApi service @@ -51,7 +53,7 @@ func (r ApiCreateLoadBalancerRequest) Execute() (*LoadBalancer, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.CreateLoadBalancer") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1beta/projects/{projectId}/load-balancers" @@ -105,20 +107,20 @@ func (r ApiCreateLoadBalancerRequest) Execute() (*LoadBalancer, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -169,7 +171,7 @@ func (r ApiDeleteLoadBalancerRequest) Execute() (map[string]interface{}, error) a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.DeleteLoadBalancer") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1beta/projects/{projectId}/load-balancers/{name}" @@ -215,20 +217,20 @@ func (r ApiDeleteLoadBalancerRequest) Execute() (map[string]interface{}, error) } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -281,7 +283,7 @@ func (r ApiDisableLoadBalancingRequest) Execute() (map[string]interface{}, error a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.DisableLoadBalancing") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1beta/projects/{projectId}" @@ -326,20 +328,20 @@ func (r ApiDisableLoadBalancingRequest) Execute() (map[string]interface{}, error } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -395,7 +397,7 @@ func (r ApiEnableLoadBalancingRequest) Execute() (map[string]interface{}, error) a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.EnableLoadBalancing") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1beta/projects/{projectId}" @@ -444,20 +446,20 @@ func (r ApiEnableLoadBalancingRequest) Execute() (map[string]interface{}, error) } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -508,7 +510,7 @@ func (r ApiGetLoadBalancerRequest) Execute() (*LoadBalancer, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetLoadBalancer") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1beta/projects/{projectId}/load-balancers/{name}" @@ -554,20 +556,20 @@ func (r ApiGetLoadBalancerRequest) Execute() (*LoadBalancer, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -620,7 +622,7 @@ func (r ApiGetStatusRequest) Execute() (*StatusResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetStatus") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1beta/projects/{projectId}" @@ -665,20 +667,20 @@ func (r ApiGetStatusRequest) Execute() (*StatusResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -728,7 +730,7 @@ func (r ApiListLoadBalancersRequest) Execute() (*ListLoadBalancersResponse, erro a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.ListLoadBalancers") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1beta/projects/{projectId}/load-balancers" @@ -773,20 +775,20 @@ func (r ApiListLoadBalancersRequest) Execute() (*ListLoadBalancersResponse, erro } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -844,7 +846,7 @@ func (r ApiUpdateTargetPoolRequest) Execute() (*TargetPool, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.UpdateTargetPool") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1beta/projects/{projectId}/load-balancers/{name}/target-pools/{targetPoolName}" @@ -896,20 +898,20 @@ func (r ApiUpdateTargetPoolRequest) Execute() (*TargetPool, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } diff --git a/services/loadbalancer/client.go b/services/loadbalancer/client.go index bf7ef7b8..6bd7be65 100644 --- a/services/loadbalancer/client.go +++ b/services/loadbalancer/client.go @@ -15,7 +15,6 @@ import ( "context" "encoding/json" "encoding/xml" - "errors" "fmt" "io" "log" @@ -36,11 +35,6 @@ import ( "github.com/stackitcloud/stackit-sdk-go/core/config" ) -var ( - // When a response has a bad status, this limits the number of characters that are shown from the response body - ApiErrorMaxCharacterLimit = 500 -) - var ( jsonCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:vnd\.[^;]+\+)?json)`) xmlCheck = regexp.MustCompile(`(?i:(?:application|text)/xml)`) @@ -325,7 +319,7 @@ func (c *APIClient) prepareRequest( // add form parameters and file if available. if strings.HasPrefix(headerParams["Content-Type"], "multipart/form-data") && len(formParams) > 0 || (len(formFiles) > 0) { if body != nil { - return nil, errors.New("Cannot specify postBody and multipart form at the same time.") + return nil, fmt.Errorf("cannot specify postBody and multipart form at the same time.") } body = &bytes.Buffer{} w := multipart.NewWriter(body) @@ -366,7 +360,7 @@ func (c *APIClient) prepareRequest( if strings.HasPrefix(headerParams["Content-Type"], "application/x-www-form-urlencoded") && len(formParams) > 0 { if body != nil { - return nil, errors.New("Cannot specify postBody and x-www-form-urlencoded form at the same time.") + return nil, fmt.Errorf("cannot specify postBody and x-www-form-urlencoded form at the same time.") } body = &bytes.Buffer{} body.WriteString(formParams.Encode()) @@ -486,14 +480,14 @@ func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err err return err } } else { - return errors.New("Unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined") + return fmt.Errorf("unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined") } } else if err = json.Unmarshal(b, v); err != nil { // simple model return err } return nil } - return errors.New("undefined response type") + return fmt.Errorf("undefined response type") } // Add a file to the multipart request @@ -550,7 +544,7 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e } if bodyBuf.Len() == 0 { - err = fmt.Errorf("invalid body type %s\n", contentType) + err = fmt.Errorf("invalid body type %s", contentType) return nil, err } return bodyBuf, nil @@ -630,73 +624,3 @@ func CacheExpires(r *http.Response) time.Time { func strlen(s string) int { return utf8.RuneCountInString(s) } - -// GenericOpenAPIError Provides access to the body, error and model on returned errors. -type GenericOpenAPIError struct { - statusCode int - body []byte - error string - model interface{} -} - -// Error returns non-empty string if there was an error. -func (e GenericOpenAPIError) Error() string { - // Prevent panic in case of negative value - if ApiErrorMaxCharacterLimit < 0 { - ApiErrorMaxCharacterLimit = 500 - } - - if len(e.body) <= ApiErrorMaxCharacterLimit { - return fmt.Sprintf("%s, status code %d, body: %s\n", e.error, e.statusCode, string(e.body)) - } else { - indexStart := ApiErrorMaxCharacterLimit / 2 - indexEnd := len(e.body) - ApiErrorMaxCharacterLimit/2 - numberTruncatedCharacters := indexEnd - indexStart - return fmt.Sprintf( - "%s, status code %d, body: %s [...truncated %d characters...] %s", - e.error, - e.statusCode, - string(e.body[:indexStart]), - numberTruncatedCharacters, - string(e.body[indexEnd:]), - ) - } -} - -// StatusCode returns the status code of the response -func (e GenericOpenAPIError) StatusCode() int { - return e.statusCode -} - -// Body returns the raw bytes of the response -func (e GenericOpenAPIError) Body() []byte { - return e.body -} - -// Model returns the unpacked model of the error -func (e GenericOpenAPIError) Model() interface{} { - return e.model -} - -// format error message using title and detail when model implements rfc7807 -func formatErrorMessage(status string, v interface{}) string { - str := "" - metaValue := reflect.ValueOf(v).Elem() - switch metaValue.Kind() { - case reflect.Map: - return status - case reflect.Struct: - field := metaValue.FieldByName("Title") - if field != (reflect.Value{}) { - str = fmt.Sprintf("%s", field.Interface()) - } - field = metaValue.FieldByName("Detail") - if field != (reflect.Value{}) { - str = fmt.Sprintf("%s (%s)", str, field.Interface()) - } - - // status title (detail) - return strings.TrimSpace(fmt.Sprintf("%s %s", status, str)) - } - return status -} diff --git a/services/logme/api_default.go b/services/logme/api_default.go index 16846fd1..e98b4053 100644 --- a/services/logme/api_default.go +++ b/services/logme/api_default.go @@ -18,6 +18,8 @@ import ( "net/http" "net/url" "strings" + + oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" ) // DefaultApiService DefaultApi service @@ -40,7 +42,7 @@ func (r ApiCreateCredentialsRequest) Execute() (*CredentialsResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.CreateCredentials") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/credentials" @@ -86,30 +88,30 @@ func (r ApiCreateCredentialsRequest) Execute() (*CredentialsResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -170,7 +172,7 @@ func (r ApiCreateInstanceRequest) Execute() (*InstanceId, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.CreateInstance") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances" @@ -220,41 +222,41 @@ func (r ApiCreateInstanceRequest) Execute() (*InstanceId, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 409 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -305,7 +307,7 @@ func (r ApiDeleteCredentialsRequest) Execute() error { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.DeleteCredentials") if err != nil { - return &GenericOpenAPIError{error: err.Error()} + return &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/credentials/{credentialsId}" @@ -352,20 +354,20 @@ func (r ApiDeleteCredentialsRequest) Execute() error { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return newErr } @@ -421,7 +423,7 @@ func (r ApiDeleteInstanceRequest) Execute() error { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.DeleteInstance") if err != nil { - return &GenericOpenAPIError{error: err.Error()} + return &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}" @@ -467,31 +469,31 @@ func (r ApiDeleteInstanceRequest) Execute() error { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return newErr } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return newErr } @@ -546,7 +548,7 @@ func (r ApiGetCredentialsRequest) Execute() (*CredentialsResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetCredentials") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/credentials/{credentialsId}" @@ -593,30 +595,30 @@ func (r ApiGetCredentialsRequest) Execute() (*CredentialsResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -673,7 +675,7 @@ func (r ApiGetCredentialsIdsRequest) Execute() (*CredentialsIdsResponse, error) a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetCredentialsIds") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/credentials" @@ -719,30 +721,30 @@ func (r ApiGetCredentialsIdsRequest) Execute() (*CredentialsIdsResponse, error) } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -796,7 +798,7 @@ func (r ApiGetInstanceRequest) Execute() (*Instance, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetInstance") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}" @@ -842,41 +844,41 @@ func (r ApiGetInstanceRequest) Execute() (*Instance, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 410 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -929,7 +931,7 @@ func (r ApiGetInstancesRequest) Execute() (*InstanceList, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetInstances") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances" @@ -974,30 +976,30 @@ func (r ApiGetInstancesRequest) Execute() (*InstanceList, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1047,7 +1049,7 @@ func (r ApiGetOfferingsRequest) Execute() (*OfferingList, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetOfferings") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/offerings" @@ -1092,20 +1094,20 @@ func (r ApiGetOfferingsRequest) Execute() (*OfferingList, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1163,7 +1165,7 @@ func (r ApiUpdateInstanceRequest) Execute() error { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.UpdateInstance") if err != nil { - return &GenericOpenAPIError{error: err.Error()} + return &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}" @@ -1214,31 +1216,31 @@ func (r ApiUpdateInstanceRequest) Execute() error { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return newErr } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return newErr } diff --git a/services/logme/client.go b/services/logme/client.go index 4286f3e2..91739963 100644 --- a/services/logme/client.go +++ b/services/logme/client.go @@ -15,7 +15,6 @@ import ( "context" "encoding/json" "encoding/xml" - "errors" "fmt" "io" "log" @@ -36,11 +35,6 @@ import ( "github.com/stackitcloud/stackit-sdk-go/core/config" ) -var ( - // When a response has a bad status, this limits the number of characters that are shown from the response body - ApiErrorMaxCharacterLimit = 500 -) - var ( jsonCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:vnd\.[^;]+\+)?json)`) xmlCheck = regexp.MustCompile(`(?i:(?:application|text)/xml)`) @@ -325,7 +319,7 @@ func (c *APIClient) prepareRequest( // add form parameters and file if available. if strings.HasPrefix(headerParams["Content-Type"], "multipart/form-data") && len(formParams) > 0 || (len(formFiles) > 0) { if body != nil { - return nil, errors.New("Cannot specify postBody and multipart form at the same time.") + return nil, fmt.Errorf("cannot specify postBody and multipart form at the same time.") } body = &bytes.Buffer{} w := multipart.NewWriter(body) @@ -366,7 +360,7 @@ func (c *APIClient) prepareRequest( if strings.HasPrefix(headerParams["Content-Type"], "application/x-www-form-urlencoded") && len(formParams) > 0 { if body != nil { - return nil, errors.New("Cannot specify postBody and x-www-form-urlencoded form at the same time.") + return nil, fmt.Errorf("cannot specify postBody and x-www-form-urlencoded form at the same time.") } body = &bytes.Buffer{} body.WriteString(formParams.Encode()) @@ -486,14 +480,14 @@ func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err err return err } } else { - return errors.New("Unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined") + return fmt.Errorf("unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined") } } else if err = json.Unmarshal(b, v); err != nil { // simple model return err } return nil } - return errors.New("undefined response type") + return fmt.Errorf("undefined response type") } // Add a file to the multipart request @@ -550,7 +544,7 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e } if bodyBuf.Len() == 0 { - err = fmt.Errorf("invalid body type %s\n", contentType) + err = fmt.Errorf("invalid body type %s", contentType) return nil, err } return bodyBuf, nil @@ -630,73 +624,3 @@ func CacheExpires(r *http.Response) time.Time { func strlen(s string) int { return utf8.RuneCountInString(s) } - -// GenericOpenAPIError Provides access to the body, error and model on returned errors. -type GenericOpenAPIError struct { - statusCode int - body []byte - error string - model interface{} -} - -// Error returns non-empty string if there was an error. -func (e GenericOpenAPIError) Error() string { - // Prevent panic in case of negative value - if ApiErrorMaxCharacterLimit < 0 { - ApiErrorMaxCharacterLimit = 500 - } - - if len(e.body) <= ApiErrorMaxCharacterLimit { - return fmt.Sprintf("%s, status code %d, body: %s\n", e.error, e.statusCode, string(e.body)) - } else { - indexStart := ApiErrorMaxCharacterLimit / 2 - indexEnd := len(e.body) - ApiErrorMaxCharacterLimit/2 - numberTruncatedCharacters := indexEnd - indexStart - return fmt.Sprintf( - "%s, status code %d, body: %s [...truncated %d characters...] %s", - e.error, - e.statusCode, - string(e.body[:indexStart]), - numberTruncatedCharacters, - string(e.body[indexEnd:]), - ) - } -} - -// StatusCode returns the status code of the response -func (e GenericOpenAPIError) StatusCode() int { - return e.statusCode -} - -// Body returns the raw bytes of the response -func (e GenericOpenAPIError) Body() []byte { - return e.body -} - -// Model returns the unpacked model of the error -func (e GenericOpenAPIError) Model() interface{} { - return e.model -} - -// format error message using title and detail when model implements rfc7807 -func formatErrorMessage(status string, v interface{}) string { - str := "" - metaValue := reflect.ValueOf(v).Elem() - switch metaValue.Kind() { - case reflect.Map: - return status - case reflect.Struct: - field := metaValue.FieldByName("Title") - if field != (reflect.Value{}) { - str = fmt.Sprintf("%s", field.Interface()) - } - field = metaValue.FieldByName("Detail") - if field != (reflect.Value{}) { - str = fmt.Sprintf("%s (%s)", str, field.Interface()) - } - - // status title (detail) - return strings.TrimSpace(fmt.Sprintf("%s %s", status, str)) - } - return status -} diff --git a/services/mariadb/api_default.go b/services/mariadb/api_default.go index a5173bd8..cc6a9b92 100644 --- a/services/mariadb/api_default.go +++ b/services/mariadb/api_default.go @@ -18,6 +18,8 @@ import ( "net/http" "net/url" "strings" + + oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" ) // DefaultApiService DefaultApi service @@ -40,7 +42,7 @@ func (r ApiCreateCredentialsRequest) Execute() (*CredentialsResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.CreateCredentials") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/credentials" @@ -86,30 +88,30 @@ func (r ApiCreateCredentialsRequest) Execute() (*CredentialsResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -170,7 +172,7 @@ func (r ApiCreateInstanceRequest) Execute() (*InstanceId, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.CreateInstance") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances" @@ -220,41 +222,41 @@ func (r ApiCreateInstanceRequest) Execute() (*InstanceId, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 409 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -305,7 +307,7 @@ func (r ApiDeleteCredentialsRequest) Execute() error { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.DeleteCredentials") if err != nil { - return &GenericOpenAPIError{error: err.Error()} + return &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/credentials/{credentialsId}" @@ -352,20 +354,20 @@ func (r ApiDeleteCredentialsRequest) Execute() error { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return newErr } @@ -421,7 +423,7 @@ func (r ApiDeleteInstanceRequest) Execute() error { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.DeleteInstance") if err != nil { - return &GenericOpenAPIError{error: err.Error()} + return &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}" @@ -467,31 +469,31 @@ func (r ApiDeleteInstanceRequest) Execute() error { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return newErr } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return newErr } @@ -546,7 +548,7 @@ func (r ApiGetCredentialsRequest) Execute() (*CredentialsResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetCredentials") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/credentials/{credentialsId}" @@ -593,30 +595,30 @@ func (r ApiGetCredentialsRequest) Execute() (*CredentialsResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -673,7 +675,7 @@ func (r ApiGetCredentialsIdsRequest) Execute() (*CredentialsIdsResponse, error) a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetCredentialsIds") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/credentials" @@ -719,30 +721,30 @@ func (r ApiGetCredentialsIdsRequest) Execute() (*CredentialsIdsResponse, error) } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -796,7 +798,7 @@ func (r ApiGetInstanceRequest) Execute() (*Instance, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetInstance") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}" @@ -842,41 +844,41 @@ func (r ApiGetInstanceRequest) Execute() (*Instance, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 410 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -929,7 +931,7 @@ func (r ApiGetInstancesRequest) Execute() (*InstanceList, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetInstances") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances" @@ -974,30 +976,30 @@ func (r ApiGetInstancesRequest) Execute() (*InstanceList, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1047,7 +1049,7 @@ func (r ApiGetOfferingsRequest) Execute() (*OfferingList, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetOfferings") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/offerings" @@ -1092,20 +1094,20 @@ func (r ApiGetOfferingsRequest) Execute() (*OfferingList, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1163,7 +1165,7 @@ func (r ApiUpdateInstanceRequest) Execute() error { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.UpdateInstance") if err != nil { - return &GenericOpenAPIError{error: err.Error()} + return &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}" @@ -1214,31 +1216,31 @@ func (r ApiUpdateInstanceRequest) Execute() error { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return newErr } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return newErr } diff --git a/services/mariadb/client.go b/services/mariadb/client.go index 7aec64d7..4fc8ecca 100644 --- a/services/mariadb/client.go +++ b/services/mariadb/client.go @@ -15,7 +15,6 @@ import ( "context" "encoding/json" "encoding/xml" - "errors" "fmt" "io" "log" @@ -36,11 +35,6 @@ import ( "github.com/stackitcloud/stackit-sdk-go/core/config" ) -var ( - // When a response has a bad status, this limits the number of characters that are shown from the response body - ApiErrorMaxCharacterLimit = 500 -) - var ( jsonCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:vnd\.[^;]+\+)?json)`) xmlCheck = regexp.MustCompile(`(?i:(?:application|text)/xml)`) @@ -325,7 +319,7 @@ func (c *APIClient) prepareRequest( // add form parameters and file if available. if strings.HasPrefix(headerParams["Content-Type"], "multipart/form-data") && len(formParams) > 0 || (len(formFiles) > 0) { if body != nil { - return nil, errors.New("Cannot specify postBody and multipart form at the same time.") + return nil, fmt.Errorf("cannot specify postBody and multipart form at the same time.") } body = &bytes.Buffer{} w := multipart.NewWriter(body) @@ -366,7 +360,7 @@ func (c *APIClient) prepareRequest( if strings.HasPrefix(headerParams["Content-Type"], "application/x-www-form-urlencoded") && len(formParams) > 0 { if body != nil { - return nil, errors.New("Cannot specify postBody and x-www-form-urlencoded form at the same time.") + return nil, fmt.Errorf("cannot specify postBody and x-www-form-urlencoded form at the same time.") } body = &bytes.Buffer{} body.WriteString(formParams.Encode()) @@ -486,14 +480,14 @@ func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err err return err } } else { - return errors.New("Unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined") + return fmt.Errorf("unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined") } } else if err = json.Unmarshal(b, v); err != nil { // simple model return err } return nil } - return errors.New("undefined response type") + return fmt.Errorf("undefined response type") } // Add a file to the multipart request @@ -550,7 +544,7 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e } if bodyBuf.Len() == 0 { - err = fmt.Errorf("invalid body type %s\n", contentType) + err = fmt.Errorf("invalid body type %s", contentType) return nil, err } return bodyBuf, nil @@ -630,73 +624,3 @@ func CacheExpires(r *http.Response) time.Time { func strlen(s string) int { return utf8.RuneCountInString(s) } - -// GenericOpenAPIError Provides access to the body, error and model on returned errors. -type GenericOpenAPIError struct { - statusCode int - body []byte - error string - model interface{} -} - -// Error returns non-empty string if there was an error. -func (e GenericOpenAPIError) Error() string { - // Prevent panic in case of negative value - if ApiErrorMaxCharacterLimit < 0 { - ApiErrorMaxCharacterLimit = 500 - } - - if len(e.body) <= ApiErrorMaxCharacterLimit { - return fmt.Sprintf("%s, status code %d, body: %s\n", e.error, e.statusCode, string(e.body)) - } else { - indexStart := ApiErrorMaxCharacterLimit / 2 - indexEnd := len(e.body) - ApiErrorMaxCharacterLimit/2 - numberTruncatedCharacters := indexEnd - indexStart - return fmt.Sprintf( - "%s, status code %d, body: %s [...truncated %d characters...] %s", - e.error, - e.statusCode, - string(e.body[:indexStart]), - numberTruncatedCharacters, - string(e.body[indexEnd:]), - ) - } -} - -// StatusCode returns the status code of the response -func (e GenericOpenAPIError) StatusCode() int { - return e.statusCode -} - -// Body returns the raw bytes of the response -func (e GenericOpenAPIError) Body() []byte { - return e.body -} - -// Model returns the unpacked model of the error -func (e GenericOpenAPIError) Model() interface{} { - return e.model -} - -// format error message using title and detail when model implements rfc7807 -func formatErrorMessage(status string, v interface{}) string { - str := "" - metaValue := reflect.ValueOf(v).Elem() - switch metaValue.Kind() { - case reflect.Map: - return status - case reflect.Struct: - field := metaValue.FieldByName("Title") - if field != (reflect.Value{}) { - str = fmt.Sprintf("%s", field.Interface()) - } - field = metaValue.FieldByName("Detail") - if field != (reflect.Value{}) { - str = fmt.Sprintf("%s (%s)", str, field.Interface()) - } - - // status title (detail) - return strings.TrimSpace(fmt.Sprintf("%s %s", status, str)) - } - return status -} diff --git a/services/mongodbflex/api_default.go b/services/mongodbflex/api_default.go index a06d8e11..585a21dd 100644 --- a/services/mongodbflex/api_default.go +++ b/services/mongodbflex/api_default.go @@ -18,6 +18,8 @@ import ( "net/http" "net/url" "strings" + + oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" ) // DefaultApiService DefaultApi service @@ -48,7 +50,7 @@ func (r ApiCloneInstanceRequest) Execute() (*CloneInstanceResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.CloneInstance") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/clone" @@ -99,41 +101,41 @@ func (r ApiCloneInstanceRequest) Execute() (*CloneInstanceResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -195,7 +197,7 @@ func (r ApiCreateInstanceRequest) Execute() (*CreateInstanceResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.CreateInstance") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances" @@ -245,52 +247,52 @@ func (r ApiCreateInstanceRequest) Execute() (*CreateInstanceResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 500 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -349,7 +351,7 @@ func (r ApiCreateUserRequest) Execute() (*CreateUserResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.CreateUser") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/users" @@ -400,63 +402,63 @@ func (r ApiCreateUserRequest) Execute() (*CreateUserResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 409 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 500 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -509,7 +511,7 @@ func (r ApiDeleteInstanceRequest) Execute() error { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.DeleteInstance") if err != nil { - return &GenericOpenAPIError{error: err.Error()} + return &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}" @@ -555,42 +557,42 @@ func (r ApiDeleteInstanceRequest) Execute() error { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return newErr } if localVarHTTPResponse.StatusCode == 404 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return newErr } if localVarHTTPResponse.StatusCode == 500 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return newErr } @@ -642,7 +644,7 @@ func (r ApiDeleteProjectRequest) Execute() error { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.DeleteProject") if err != nil { - return &GenericOpenAPIError{error: err.Error()} + return &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}" @@ -687,31 +689,31 @@ func (r ApiDeleteProjectRequest) Execute() error { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 409 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return newErr } if localVarHTTPResponse.StatusCode == 500 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return newErr } @@ -762,7 +764,7 @@ func (r ApiDeleteUserRequest) Execute() error { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.DeleteUser") if err != nil { - return &GenericOpenAPIError{error: err.Error()} + return &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/users/{userId}" @@ -809,31 +811,31 @@ func (r ApiDeleteUserRequest) Execute() error { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return newErr } if localVarHTTPResponse.StatusCode == 500 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return newErr } @@ -891,7 +893,7 @@ func (r ApiGetBackupRequest) Execute() (*GetBackupResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetBackup") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/backups/{backupId}" @@ -938,41 +940,41 @@ func (r ApiGetBackupRequest) Execute() (*GetBackupResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1029,7 +1031,7 @@ func (r ApiGetBackupsRequest) Execute() (*GetBackupsResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetBackups") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/backups" @@ -1075,41 +1077,41 @@ func (r ApiGetBackupsRequest) Execute() (*GetBackupsResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1195,7 +1197,7 @@ func (r ApiGetCPUMetricsRequest) Execute() (*GetMetricsResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetCPUMetrics") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/metrics/cpu" @@ -1254,52 +1256,52 @@ func (r ApiGetCPUMetricsRequest) Execute() (*GetMetricsResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 500 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1385,7 +1387,7 @@ func (r ApiGetDatabaseStorageMetricsRequest) Execute() (*GetMetricsResponse, err a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetDatabaseStorageMetrics") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/metrics/database" @@ -1444,52 +1446,52 @@ func (r ApiGetDatabaseStorageMetricsRequest) Execute() (*GetMetricsResponse, err } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 500 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1575,7 +1577,7 @@ func (r ApiGetDiskIOPSMetricsRequest) Execute() (*GetMetricsResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetDiskIOPSMetrics") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/metrics/disk-iops" @@ -1634,52 +1636,52 @@ func (r ApiGetDiskIOPSMetricsRequest) Execute() (*GetMetricsResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 500 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1765,7 +1767,7 @@ func (r ApiGetDiskUsageMetricsRequest) Execute() (*GetMetricsResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetDiskUsageMetrics") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/metrics/disk-use" @@ -1824,52 +1826,52 @@ func (r ApiGetDiskUsageMetricsRequest) Execute() (*GetMetricsResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 500 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1955,7 +1957,7 @@ func (r ApiGetExecutionTimesMetricsRequest) Execute() (*GetMetricsResponse, erro a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetExecutionTimesMetrics") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/metrics/exec-time" @@ -2014,52 +2016,52 @@ func (r ApiGetExecutionTimesMetricsRequest) Execute() (*GetMetricsResponse, erro } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 500 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -2112,7 +2114,7 @@ func (r ApiGetFlavorsRequest) Execute() (*GetFlavorsResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetFlavors") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/flavors" @@ -2157,41 +2159,41 @@ func (r ApiGetFlavorsRequest) Execute() (*GetFlavorsResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 500 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -2242,7 +2244,7 @@ func (r ApiGetInstanceRequest) Execute() (*GetInstanceResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetInstance") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}" @@ -2288,41 +2290,41 @@ func (r ApiGetInstanceRequest) Execute() (*GetInstanceResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 500 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -2376,7 +2378,7 @@ func (r ApiGetInstanceRestoresRequest) Execute() (*GetInstanceRestoresResponse, a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetInstanceRestores") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/restores" @@ -2422,41 +2424,41 @@ func (r ApiGetInstanceRestoresRequest) Execute() (*GetInstanceRestoresResponse, } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -2517,7 +2519,7 @@ func (r ApiGetInstancesRequest) Execute() (*GetInstancesResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetInstances") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances" @@ -2566,41 +2568,41 @@ func (r ApiGetInstancesRequest) Execute() (*GetInstancesResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 500 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -2691,7 +2693,7 @@ func (r ApiGetMemoryMetricsRequest) Execute() (*GetMetricsResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetMemoryMetrics") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/metrics/memory" @@ -2754,52 +2756,52 @@ func (r ApiGetMemoryMetricsRequest) Execute() (*GetMetricsResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 500 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -2853,7 +2855,7 @@ func (r ApiGetStorageRequest) Execute() (*GetStorageResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetStorage") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/storages/{flavor}" @@ -2899,41 +2901,41 @@ func (r ApiGetStorageRequest) Execute() (*GetStorageResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -2988,7 +2990,7 @@ func (r ApiGetUserRequest) Execute() (*GetUserResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetUser") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/users/{userId}" @@ -3035,41 +3037,41 @@ func (r ApiGetUserRequest) Execute() (*GetUserResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 500 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -3126,7 +3128,7 @@ func (r ApiGetUsersRequest) Execute() (*GetUsersResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetUsers") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/users" @@ -3172,41 +3174,41 @@ func (r ApiGetUsersRequest) Execute() (*GetUsersResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 500 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -3259,7 +3261,7 @@ func (r ApiGetVersionsRequest) Execute() (*GetVersionsResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetVersions") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/versions" @@ -3304,30 +3306,30 @@ func (r ApiGetVersionsRequest) Execute() (*GetVersionsResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -3386,7 +3388,7 @@ func (r ApiPartialUpdateInstanceRequest) Execute() (*UpdateInstanceResponse, err a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.PartialUpdateInstance") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}" @@ -3437,63 +3439,63 @@ func (r ApiPartialUpdateInstanceRequest) Execute() (*UpdateInstanceResponse, err } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 409 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 500 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -3555,7 +3557,7 @@ func (r ApiPartialUpdateUserRequest) Execute() error { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.PartialUpdateUser") if err != nil { - return &GenericOpenAPIError{error: err.Error()} + return &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/users/{userId}" @@ -3607,42 +3609,42 @@ func (r ApiPartialUpdateUserRequest) Execute() error { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return newErr } if localVarHTTPResponse.StatusCode == 404 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return newErr } if localVarHTTPResponse.StatusCode == 409 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return newErr } @@ -3700,7 +3702,7 @@ func (r ApiResetUserRequest) Execute() (*InstanceUser, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.ResetUser") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/users/{userId}/reset" @@ -3747,41 +3749,41 @@ func (r ApiResetUserRequest) Execute() (*InstanceUser, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 500 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -3846,7 +3848,7 @@ func (r ApiRestoreInstanceRequest) Execute() (*RestoreInstanceResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.RestoreInstance") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/restores" @@ -3897,41 +3899,41 @@ func (r ApiRestoreInstanceRequest) Execute() (*RestoreInstanceResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -3993,7 +3995,7 @@ func (r ApiUpdateBackupScheduleRequest) Execute() (*UpdateBackupSchedulePayload, a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.UpdateBackupSchedule") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/backups" @@ -4044,41 +4046,41 @@ func (r ApiUpdateBackupScheduleRequest) Execute() (*UpdateBackupSchedulePayload, } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -4140,7 +4142,7 @@ func (r ApiUpdateInstanceRequest) Execute() (*UpdateInstanceResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.UpdateInstance") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}" @@ -4191,63 +4193,63 @@ func (r ApiUpdateInstanceRequest) Execute() (*UpdateInstanceResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 409 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 500 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -4309,7 +4311,7 @@ func (r ApiUpdateUserRequest) Execute() error { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.UpdateUser") if err != nil { - return &GenericOpenAPIError{error: err.Error()} + return &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/users/{userId}" @@ -4361,42 +4363,42 @@ func (r ApiUpdateUserRequest) Execute() error { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return newErr } if localVarHTTPResponse.StatusCode == 404 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return newErr } if localVarHTTPResponse.StatusCode == 409 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return newErr } diff --git a/services/mongodbflex/client.go b/services/mongodbflex/client.go index d23c6964..6ad7e90d 100644 --- a/services/mongodbflex/client.go +++ b/services/mongodbflex/client.go @@ -15,7 +15,6 @@ import ( "context" "encoding/json" "encoding/xml" - "errors" "fmt" "io" "log" @@ -36,11 +35,6 @@ import ( "github.com/stackitcloud/stackit-sdk-go/core/config" ) -var ( - // When a response has a bad status, this limits the number of characters that are shown from the response body - ApiErrorMaxCharacterLimit = 500 -) - var ( jsonCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:vnd\.[^;]+\+)?json)`) xmlCheck = regexp.MustCompile(`(?i:(?:application|text)/xml)`) @@ -325,7 +319,7 @@ func (c *APIClient) prepareRequest( // add form parameters and file if available. if strings.HasPrefix(headerParams["Content-Type"], "multipart/form-data") && len(formParams) > 0 || (len(formFiles) > 0) { if body != nil { - return nil, errors.New("Cannot specify postBody and multipart form at the same time.") + return nil, fmt.Errorf("cannot specify postBody and multipart form at the same time.") } body = &bytes.Buffer{} w := multipart.NewWriter(body) @@ -366,7 +360,7 @@ func (c *APIClient) prepareRequest( if strings.HasPrefix(headerParams["Content-Type"], "application/x-www-form-urlencoded") && len(formParams) > 0 { if body != nil { - return nil, errors.New("Cannot specify postBody and x-www-form-urlencoded form at the same time.") + return nil, fmt.Errorf("cannot specify postBody and x-www-form-urlencoded form at the same time.") } body = &bytes.Buffer{} body.WriteString(formParams.Encode()) @@ -486,14 +480,14 @@ func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err err return err } } else { - return errors.New("Unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined") + return fmt.Errorf("unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined") } } else if err = json.Unmarshal(b, v); err != nil { // simple model return err } return nil } - return errors.New("undefined response type") + return fmt.Errorf("undefined response type") } // Add a file to the multipart request @@ -550,7 +544,7 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e } if bodyBuf.Len() == 0 { - err = fmt.Errorf("invalid body type %s\n", contentType) + err = fmt.Errorf("invalid body type %s", contentType) return nil, err } return bodyBuf, nil @@ -630,73 +624,3 @@ func CacheExpires(r *http.Response) time.Time { func strlen(s string) int { return utf8.RuneCountInString(s) } - -// GenericOpenAPIError Provides access to the body, error and model on returned errors. -type GenericOpenAPIError struct { - statusCode int - body []byte - error string - model interface{} -} - -// Error returns non-empty string if there was an error. -func (e GenericOpenAPIError) Error() string { - // Prevent panic in case of negative value - if ApiErrorMaxCharacterLimit < 0 { - ApiErrorMaxCharacterLimit = 500 - } - - if len(e.body) <= ApiErrorMaxCharacterLimit { - return fmt.Sprintf("%s, status code %d, body: %s\n", e.error, e.statusCode, string(e.body)) - } else { - indexStart := ApiErrorMaxCharacterLimit / 2 - indexEnd := len(e.body) - ApiErrorMaxCharacterLimit/2 - numberTruncatedCharacters := indexEnd - indexStart - return fmt.Sprintf( - "%s, status code %d, body: %s [...truncated %d characters...] %s", - e.error, - e.statusCode, - string(e.body[:indexStart]), - numberTruncatedCharacters, - string(e.body[indexEnd:]), - ) - } -} - -// StatusCode returns the status code of the response -func (e GenericOpenAPIError) StatusCode() int { - return e.statusCode -} - -// Body returns the raw bytes of the response -func (e GenericOpenAPIError) Body() []byte { - return e.body -} - -// Model returns the unpacked model of the error -func (e GenericOpenAPIError) Model() interface{} { - return e.model -} - -// format error message using title and detail when model implements rfc7807 -func formatErrorMessage(status string, v interface{}) string { - str := "" - metaValue := reflect.ValueOf(v).Elem() - switch metaValue.Kind() { - case reflect.Map: - return status - case reflect.Struct: - field := metaValue.FieldByName("Title") - if field != (reflect.Value{}) { - str = fmt.Sprintf("%s", field.Interface()) - } - field = metaValue.FieldByName("Detail") - if field != (reflect.Value{}) { - str = fmt.Sprintf("%s (%s)", str, field.Interface()) - } - - // status title (detail) - return strings.TrimSpace(fmt.Sprintf("%s %s", status, str)) - } - return status -} diff --git a/services/objectstorage/api_default.go b/services/objectstorage/api_default.go index 2d435b0b..d5405dfd 100644 --- a/services/objectstorage/api_default.go +++ b/services/objectstorage/api_default.go @@ -18,6 +18,8 @@ import ( "net/http" "net/url" "strings" + + oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" ) // DefaultApiService DefaultApi service @@ -40,7 +42,7 @@ func (r ApiCreateBucketRequest) Execute() (*CreateBucketResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.CreateBucket") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/project/{projectId}/bucket/{bucketName}" @@ -92,63 +94,63 @@ func (r ApiCreateBucketRequest) Execute() (*CreateBucketResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v ErrorMessage err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 409 { var v ErrorMessage err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 422 { var v ErrorMessage err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 500 { var v ErrorMessage err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -203,7 +205,7 @@ func (r ApiDeleteBucketRequest) Execute() (*DeleteBucketResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.DeleteBucket") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/project/{projectId}/bucket/{bucketName}" @@ -255,52 +257,52 @@ func (r ApiDeleteBucketRequest) Execute() (*DeleteBucketResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v ErrorMessage err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 422 { var v ErrorMessage err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 500 { var v ErrorMessage err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -354,7 +356,7 @@ func (r ApiGetBucketRequest) Execute() (*GetBucketResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetBucket") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/project/{projectId}/bucket/{bucketName}" @@ -406,63 +408,63 @@ func (r ApiGetBucketRequest) Execute() (*GetBucketResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 403 { var v ErrorMessage err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v ErrorMessage err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 422 { var v HTTPValidationError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 500 { var v ErrorMessage err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -515,7 +517,7 @@ func (r ApiGetBucketsRequest) Execute() (*GetBucketsResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetBuckets") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/project/{projectId}/buckets" @@ -560,63 +562,63 @@ func (r ApiGetBucketsRequest) Execute() (*GetBucketsResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 403 { var v ErrorMessage err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v ErrorMessage err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 422 { var v HTTPValidationError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 500 { var v ErrorMessage err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } diff --git a/services/objectstorage/client.go b/services/objectstorage/client.go index 49ff69e3..d7deaf62 100644 --- a/services/objectstorage/client.go +++ b/services/objectstorage/client.go @@ -15,7 +15,6 @@ import ( "context" "encoding/json" "encoding/xml" - "errors" "fmt" "io" "log" @@ -36,11 +35,6 @@ import ( "github.com/stackitcloud/stackit-sdk-go/core/config" ) -var ( - // When a response has a bad status, this limits the number of characters that are shown from the response body - ApiErrorMaxCharacterLimit = 500 -) - var ( jsonCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:vnd\.[^;]+\+)?json)`) xmlCheck = regexp.MustCompile(`(?i:(?:application|text)/xml)`) @@ -325,7 +319,7 @@ func (c *APIClient) prepareRequest( // add form parameters and file if available. if strings.HasPrefix(headerParams["Content-Type"], "multipart/form-data") && len(formParams) > 0 || (len(formFiles) > 0) { if body != nil { - return nil, errors.New("Cannot specify postBody and multipart form at the same time.") + return nil, fmt.Errorf("cannot specify postBody and multipart form at the same time.") } body = &bytes.Buffer{} w := multipart.NewWriter(body) @@ -366,7 +360,7 @@ func (c *APIClient) prepareRequest( if strings.HasPrefix(headerParams["Content-Type"], "application/x-www-form-urlencoded") && len(formParams) > 0 { if body != nil { - return nil, errors.New("Cannot specify postBody and x-www-form-urlencoded form at the same time.") + return nil, fmt.Errorf("cannot specify postBody and x-www-form-urlencoded form at the same time.") } body = &bytes.Buffer{} body.WriteString(formParams.Encode()) @@ -486,14 +480,14 @@ func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err err return err } } else { - return errors.New("Unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined") + return fmt.Errorf("unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined") } } else if err = json.Unmarshal(b, v); err != nil { // simple model return err } return nil } - return errors.New("undefined response type") + return fmt.Errorf("undefined response type") } // Add a file to the multipart request @@ -550,7 +544,7 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e } if bodyBuf.Len() == 0 { - err = fmt.Errorf("invalid body type %s\n", contentType) + err = fmt.Errorf("invalid body type %s", contentType) return nil, err } return bodyBuf, nil @@ -630,73 +624,3 @@ func CacheExpires(r *http.Response) time.Time { func strlen(s string) int { return utf8.RuneCountInString(s) } - -// GenericOpenAPIError Provides access to the body, error and model on returned errors. -type GenericOpenAPIError struct { - statusCode int - body []byte - error string - model interface{} -} - -// Error returns non-empty string if there was an error. -func (e GenericOpenAPIError) Error() string { - // Prevent panic in case of negative value - if ApiErrorMaxCharacterLimit < 0 { - ApiErrorMaxCharacterLimit = 500 - } - - if len(e.body) <= ApiErrorMaxCharacterLimit { - return fmt.Sprintf("%s, status code %d, body: %s\n", e.error, e.statusCode, string(e.body)) - } else { - indexStart := ApiErrorMaxCharacterLimit / 2 - indexEnd := len(e.body) - ApiErrorMaxCharacterLimit/2 - numberTruncatedCharacters := indexEnd - indexStart - return fmt.Sprintf( - "%s, status code %d, body: %s [...truncated %d characters...] %s", - e.error, - e.statusCode, - string(e.body[:indexStart]), - numberTruncatedCharacters, - string(e.body[indexEnd:]), - ) - } -} - -// StatusCode returns the status code of the response -func (e GenericOpenAPIError) StatusCode() int { - return e.statusCode -} - -// Body returns the raw bytes of the response -func (e GenericOpenAPIError) Body() []byte { - return e.body -} - -// Model returns the unpacked model of the error -func (e GenericOpenAPIError) Model() interface{} { - return e.model -} - -// format error message using title and detail when model implements rfc7807 -func formatErrorMessage(status string, v interface{}) string { - str := "" - metaValue := reflect.ValueOf(v).Elem() - switch metaValue.Kind() { - case reflect.Map: - return status - case reflect.Struct: - field := metaValue.FieldByName("Title") - if field != (reflect.Value{}) { - str = fmt.Sprintf("%s", field.Interface()) - } - field = metaValue.FieldByName("Detail") - if field != (reflect.Value{}) { - str = fmt.Sprintf("%s (%s)", str, field.Interface()) - } - - // status title (detail) - return strings.TrimSpace(fmt.Sprintf("%s %s", status, str)) - } - return status -} diff --git a/services/opensearch/api_default.go b/services/opensearch/api_default.go index 89740047..f2ffd116 100644 --- a/services/opensearch/api_default.go +++ b/services/opensearch/api_default.go @@ -18,6 +18,8 @@ import ( "net/http" "net/url" "strings" + + oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" ) // DefaultApiService DefaultApi service @@ -40,7 +42,7 @@ func (r ApiCreateCredentialsRequest) Execute() (*CredentialsResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.CreateCredentials") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/credentials" @@ -86,30 +88,30 @@ func (r ApiCreateCredentialsRequest) Execute() (*CredentialsResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -170,7 +172,7 @@ func (r ApiCreateInstanceRequest) Execute() (*InstanceId, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.CreateInstance") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances" @@ -220,41 +222,41 @@ func (r ApiCreateInstanceRequest) Execute() (*InstanceId, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 409 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -305,7 +307,7 @@ func (r ApiDeleteCredentialsRequest) Execute() error { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.DeleteCredentials") if err != nil { - return &GenericOpenAPIError{error: err.Error()} + return &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/credentials/{credentialsId}" @@ -352,20 +354,20 @@ func (r ApiDeleteCredentialsRequest) Execute() error { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return newErr } @@ -421,7 +423,7 @@ func (r ApiDeleteInstanceRequest) Execute() error { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.DeleteInstance") if err != nil { - return &GenericOpenAPIError{error: err.Error()} + return &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}" @@ -467,31 +469,31 @@ func (r ApiDeleteInstanceRequest) Execute() error { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return newErr } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return newErr } @@ -546,7 +548,7 @@ func (r ApiGetCredentialsRequest) Execute() (*CredentialsResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetCredentials") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/credentials/{credentialsId}" @@ -593,30 +595,30 @@ func (r ApiGetCredentialsRequest) Execute() (*CredentialsResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -673,7 +675,7 @@ func (r ApiGetCredentialsIdsRequest) Execute() (*CredentialsIdsResponse, error) a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetCredentialsIds") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/credentials" @@ -719,30 +721,30 @@ func (r ApiGetCredentialsIdsRequest) Execute() (*CredentialsIdsResponse, error) } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -796,7 +798,7 @@ func (r ApiGetInstanceRequest) Execute() (*Instance, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetInstance") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}" @@ -842,41 +844,41 @@ func (r ApiGetInstanceRequest) Execute() (*Instance, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 410 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -929,7 +931,7 @@ func (r ApiGetInstancesRequest) Execute() (*InstanceList, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetInstances") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances" @@ -974,30 +976,30 @@ func (r ApiGetInstancesRequest) Execute() (*InstanceList, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1047,7 +1049,7 @@ func (r ApiGetOfferingsRequest) Execute() (*OfferingList, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetOfferings") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/offerings" @@ -1092,20 +1094,20 @@ func (r ApiGetOfferingsRequest) Execute() (*OfferingList, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1163,7 +1165,7 @@ func (r ApiUpdateInstanceRequest) Execute() error { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.UpdateInstance") if err != nil { - return &GenericOpenAPIError{error: err.Error()} + return &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}" @@ -1214,31 +1216,31 @@ func (r ApiUpdateInstanceRequest) Execute() error { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return newErr } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return newErr } diff --git a/services/opensearch/client.go b/services/opensearch/client.go index 35dafdaa..d4dff716 100644 --- a/services/opensearch/client.go +++ b/services/opensearch/client.go @@ -15,7 +15,6 @@ import ( "context" "encoding/json" "encoding/xml" - "errors" "fmt" "io" "log" @@ -36,11 +35,6 @@ import ( "github.com/stackitcloud/stackit-sdk-go/core/config" ) -var ( - // When a response has a bad status, this limits the number of characters that are shown from the response body - ApiErrorMaxCharacterLimit = 500 -) - var ( jsonCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:vnd\.[^;]+\+)?json)`) xmlCheck = regexp.MustCompile(`(?i:(?:application|text)/xml)`) @@ -325,7 +319,7 @@ func (c *APIClient) prepareRequest( // add form parameters and file if available. if strings.HasPrefix(headerParams["Content-Type"], "multipart/form-data") && len(formParams) > 0 || (len(formFiles) > 0) { if body != nil { - return nil, errors.New("Cannot specify postBody and multipart form at the same time.") + return nil, fmt.Errorf("cannot specify postBody and multipart form at the same time.") } body = &bytes.Buffer{} w := multipart.NewWriter(body) @@ -366,7 +360,7 @@ func (c *APIClient) prepareRequest( if strings.HasPrefix(headerParams["Content-Type"], "application/x-www-form-urlencoded") && len(formParams) > 0 { if body != nil { - return nil, errors.New("Cannot specify postBody and x-www-form-urlencoded form at the same time.") + return nil, fmt.Errorf("cannot specify postBody and x-www-form-urlencoded form at the same time.") } body = &bytes.Buffer{} body.WriteString(formParams.Encode()) @@ -486,14 +480,14 @@ func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err err return err } } else { - return errors.New("Unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined") + return fmt.Errorf("unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined") } } else if err = json.Unmarshal(b, v); err != nil { // simple model return err } return nil } - return errors.New("undefined response type") + return fmt.Errorf("undefined response type") } // Add a file to the multipart request @@ -550,7 +544,7 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e } if bodyBuf.Len() == 0 { - err = fmt.Errorf("invalid body type %s\n", contentType) + err = fmt.Errorf("invalid body type %s", contentType) return nil, err } return bodyBuf, nil @@ -630,73 +624,3 @@ func CacheExpires(r *http.Response) time.Time { func strlen(s string) int { return utf8.RuneCountInString(s) } - -// GenericOpenAPIError Provides access to the body, error and model on returned errors. -type GenericOpenAPIError struct { - statusCode int - body []byte - error string - model interface{} -} - -// Error returns non-empty string if there was an error. -func (e GenericOpenAPIError) Error() string { - // Prevent panic in case of negative value - if ApiErrorMaxCharacterLimit < 0 { - ApiErrorMaxCharacterLimit = 500 - } - - if len(e.body) <= ApiErrorMaxCharacterLimit { - return fmt.Sprintf("%s, status code %d, body: %s\n", e.error, e.statusCode, string(e.body)) - } else { - indexStart := ApiErrorMaxCharacterLimit / 2 - indexEnd := len(e.body) - ApiErrorMaxCharacterLimit/2 - numberTruncatedCharacters := indexEnd - indexStart - return fmt.Sprintf( - "%s, status code %d, body: %s [...truncated %d characters...] %s", - e.error, - e.statusCode, - string(e.body[:indexStart]), - numberTruncatedCharacters, - string(e.body[indexEnd:]), - ) - } -} - -// StatusCode returns the status code of the response -func (e GenericOpenAPIError) StatusCode() int { - return e.statusCode -} - -// Body returns the raw bytes of the response -func (e GenericOpenAPIError) Body() []byte { - return e.body -} - -// Model returns the unpacked model of the error -func (e GenericOpenAPIError) Model() interface{} { - return e.model -} - -// format error message using title and detail when model implements rfc7807 -func formatErrorMessage(status string, v interface{}) string { - str := "" - metaValue := reflect.ValueOf(v).Elem() - switch metaValue.Kind() { - case reflect.Map: - return status - case reflect.Struct: - field := metaValue.FieldByName("Title") - if field != (reflect.Value{}) { - str = fmt.Sprintf("%s", field.Interface()) - } - field = metaValue.FieldByName("Detail") - if field != (reflect.Value{}) { - str = fmt.Sprintf("%s (%s)", str, field.Interface()) - } - - // status title (detail) - return strings.TrimSpace(fmt.Sprintf("%s %s", status, str)) - } - return status -} diff --git a/services/postgresflex/api_default.go b/services/postgresflex/api_default.go index cf17542a..8b94e3bb 100644 --- a/services/postgresflex/api_default.go +++ b/services/postgresflex/api_default.go @@ -18,6 +18,8 @@ import ( "net/http" "net/url" "strings" + + oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" ) // DefaultApiService DefaultApi service @@ -47,7 +49,7 @@ func (r ApiCreateInstanceRequest) Execute() (*CreateInstanceResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.CreateInstance") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances" @@ -97,20 +99,20 @@ func (r ApiCreateInstanceRequest) Execute() (*CreateInstanceResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } return localVarReturnValue, newErr @@ -118,10 +120,10 @@ func (r ApiCreateInstanceRequest) Execute() (*CreateInstanceResponse, error) { err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -180,7 +182,7 @@ func (r ApiCreateUserRequest) Execute() (*CreateUserResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.CreateUser") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/users" @@ -231,20 +233,20 @@ func (r ApiCreateUserRequest) Execute() (*CreateUserResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } return localVarReturnValue, newErr @@ -252,10 +254,10 @@ func (r ApiCreateUserRequest) Execute() (*CreateUserResponse, error) { err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -308,7 +310,7 @@ func (r ApiDeleteInstanceRequest) Execute() error { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.DeleteInstance") if err != nil { - return &GenericOpenAPIError{error: err.Error()} + return &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}" @@ -354,20 +356,20 @@ func (r ApiDeleteInstanceRequest) Execute() error { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return newErr } return newErr @@ -422,7 +424,7 @@ func (r ApiDeleteUserRequest) Execute() error { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.DeleteUser") if err != nil { - return &GenericOpenAPIError{error: err.Error()} + return &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/users/{userId}" @@ -469,20 +471,20 @@ func (r ApiDeleteUserRequest) Execute() error { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return newErr } return newErr @@ -541,7 +543,7 @@ func (r ApiGetBackupRequest) Execute() (*BackupResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetBackup") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/backups/{backupId}" @@ -588,20 +590,20 @@ func (r ApiGetBackupRequest) Execute() (*BackupResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } return localVarReturnValue, newErr @@ -609,10 +611,10 @@ func (r ApiGetBackupRequest) Execute() (*BackupResponse, error) { err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -669,7 +671,7 @@ func (r ApiGetBackupsRequest) Execute() (*BackupsResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetBackups") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/backups" @@ -715,20 +717,20 @@ func (r ApiGetBackupsRequest) Execute() (*BackupsResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } return localVarReturnValue, newErr @@ -736,10 +738,10 @@ func (r ApiGetBackupsRequest) Execute() (*BackupsResponse, error) { err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -792,7 +794,7 @@ func (r ApiGetFlavorsRequest) Execute() (*FlavorsResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetFlavors") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/flavors" @@ -837,20 +839,20 @@ func (r ApiGetFlavorsRequest) Execute() (*FlavorsResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } return localVarReturnValue, newErr @@ -858,10 +860,10 @@ func (r ApiGetFlavorsRequest) Execute() (*FlavorsResponse, error) { err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -912,7 +914,7 @@ func (r ApiGetInstanceRequest) Execute() (*InstanceResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetInstance") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}" @@ -958,20 +960,20 @@ func (r ApiGetInstanceRequest) Execute() (*InstanceResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } return localVarReturnValue, newErr @@ -979,10 +981,10 @@ func (r ApiGetInstanceRequest) Execute() (*InstanceResponse, error) { err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1035,7 +1037,7 @@ func (r ApiGetInstancesRequest) Execute() (*InstancesResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetInstances") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances" @@ -1080,20 +1082,20 @@ func (r ApiGetInstancesRequest) Execute() (*InstancesResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } return localVarReturnValue, newErr @@ -1101,10 +1103,10 @@ func (r ApiGetInstancesRequest) Execute() (*InstancesResponse, error) { err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1155,7 +1157,7 @@ func (r ApiGetStoragesRequest) Execute() (*StoragesResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetStorages") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/storages/{flavorId}" @@ -1201,20 +1203,20 @@ func (r ApiGetStoragesRequest) Execute() (*StoragesResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } return localVarReturnValue, newErr @@ -1222,10 +1224,10 @@ func (r ApiGetStoragesRequest) Execute() (*StoragesResponse, error) { err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1280,7 +1282,7 @@ func (r ApiGetUserRequest) Execute() (*UserResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetUser") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/users/{userId}" @@ -1327,20 +1329,20 @@ func (r ApiGetUserRequest) Execute() (*UserResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } return localVarReturnValue, newErr @@ -1348,10 +1350,10 @@ func (r ApiGetUserRequest) Execute() (*UserResponse, error) { err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1408,7 +1410,7 @@ func (r ApiGetUsersRequest) Execute() (*UsersResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetUsers") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/users" @@ -1454,20 +1456,20 @@ func (r ApiGetUsersRequest) Execute() (*UsersResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } return localVarReturnValue, newErr @@ -1475,10 +1477,10 @@ func (r ApiGetUsersRequest) Execute() (*UsersResponse, error) { err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1539,7 +1541,7 @@ func (r ApiGetVersionsRequest) Execute() (*VersionsResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetVersions") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/versions" @@ -1587,20 +1589,20 @@ func (r ApiGetVersionsRequest) Execute() (*VersionsResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } return localVarReturnValue, newErr @@ -1608,10 +1610,10 @@ func (r ApiGetVersionsRequest) Execute() (*VersionsResponse, error) { err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1669,7 +1671,7 @@ func (r ApiUpdateBackupScheduleRequest) Execute() error { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.UpdateBackupSchedule") if err != nil { - return &GenericOpenAPIError{error: err.Error()} + return &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/backups" @@ -1720,20 +1722,20 @@ func (r ApiUpdateBackupScheduleRequest) Execute() error { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return newErr } return newErr @@ -1796,7 +1798,7 @@ func (r ApiUpdateInstanceRequest) Execute() (*UpdateInstanceResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.UpdateInstance") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}" @@ -1847,20 +1849,20 @@ func (r ApiUpdateInstanceRequest) Execute() (*UpdateInstanceResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v InstanceError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } return localVarReturnValue, newErr @@ -1868,10 +1870,10 @@ func (r ApiUpdateInstanceRequest) Execute() (*UpdateInstanceResponse, error) { err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } diff --git a/services/postgresflex/client.go b/services/postgresflex/client.go index 3ffc39ca..c556a32d 100644 --- a/services/postgresflex/client.go +++ b/services/postgresflex/client.go @@ -15,7 +15,6 @@ import ( "context" "encoding/json" "encoding/xml" - "errors" "fmt" "io" "log" @@ -36,11 +35,6 @@ import ( "github.com/stackitcloud/stackit-sdk-go/core/config" ) -var ( - // When a response has a bad status, this limits the number of characters that are shown from the response body - ApiErrorMaxCharacterLimit = 500 -) - var ( jsonCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:vnd\.[^;]+\+)?json)`) xmlCheck = regexp.MustCompile(`(?i:(?:application|text)/xml)`) @@ -325,7 +319,7 @@ func (c *APIClient) prepareRequest( // add form parameters and file if available. if strings.HasPrefix(headerParams["Content-Type"], "multipart/form-data") && len(formParams) > 0 || (len(formFiles) > 0) { if body != nil { - return nil, errors.New("Cannot specify postBody and multipart form at the same time.") + return nil, fmt.Errorf("cannot specify postBody and multipart form at the same time.") } body = &bytes.Buffer{} w := multipart.NewWriter(body) @@ -366,7 +360,7 @@ func (c *APIClient) prepareRequest( if strings.HasPrefix(headerParams["Content-Type"], "application/x-www-form-urlencoded") && len(formParams) > 0 { if body != nil { - return nil, errors.New("Cannot specify postBody and x-www-form-urlencoded form at the same time.") + return nil, fmt.Errorf("cannot specify postBody and x-www-form-urlencoded form at the same time.") } body = &bytes.Buffer{} body.WriteString(formParams.Encode()) @@ -486,14 +480,14 @@ func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err err return err } } else { - return errors.New("Unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined") + return fmt.Errorf("unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined") } } else if err = json.Unmarshal(b, v); err != nil { // simple model return err } return nil } - return errors.New("undefined response type") + return fmt.Errorf("undefined response type") } // Add a file to the multipart request @@ -550,7 +544,7 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e } if bodyBuf.Len() == 0 { - err = fmt.Errorf("invalid body type %s\n", contentType) + err = fmt.Errorf("invalid body type %s", contentType) return nil, err } return bodyBuf, nil @@ -630,73 +624,3 @@ func CacheExpires(r *http.Response) time.Time { func strlen(s string) int { return utf8.RuneCountInString(s) } - -// GenericOpenAPIError Provides access to the body, error and model on returned errors. -type GenericOpenAPIError struct { - statusCode int - body []byte - error string - model interface{} -} - -// Error returns non-empty string if there was an error. -func (e GenericOpenAPIError) Error() string { - // Prevent panic in case of negative value - if ApiErrorMaxCharacterLimit < 0 { - ApiErrorMaxCharacterLimit = 500 - } - - if len(e.body) <= ApiErrorMaxCharacterLimit { - return fmt.Sprintf("%s, status code %d, body: %s\n", e.error, e.statusCode, string(e.body)) - } else { - indexStart := ApiErrorMaxCharacterLimit / 2 - indexEnd := len(e.body) - ApiErrorMaxCharacterLimit/2 - numberTruncatedCharacters := indexEnd - indexStart - return fmt.Sprintf( - "%s, status code %d, body: %s [...truncated %d characters...] %s", - e.error, - e.statusCode, - string(e.body[:indexStart]), - numberTruncatedCharacters, - string(e.body[indexEnd:]), - ) - } -} - -// StatusCode returns the status code of the response -func (e GenericOpenAPIError) StatusCode() int { - return e.statusCode -} - -// Body returns the raw bytes of the response -func (e GenericOpenAPIError) Body() []byte { - return e.body -} - -// Model returns the unpacked model of the error -func (e GenericOpenAPIError) Model() interface{} { - return e.model -} - -// format error message using title and detail when model implements rfc7807 -func formatErrorMessage(status string, v interface{}) string { - str := "" - metaValue := reflect.ValueOf(v).Elem() - switch metaValue.Kind() { - case reflect.Map: - return status - case reflect.Struct: - field := metaValue.FieldByName("Title") - if field != (reflect.Value{}) { - str = fmt.Sprintf("%s", field.Interface()) - } - field = metaValue.FieldByName("Detail") - if field != (reflect.Value{}) { - str = fmt.Sprintf("%s (%s)", str, field.Interface()) - } - - // status title (detail) - return strings.TrimSpace(fmt.Sprintf("%s %s", status, str)) - } - return status -} diff --git a/services/postgresql/api_default.go b/services/postgresql/api_default.go index c49a98ad..bd377617 100644 --- a/services/postgresql/api_default.go +++ b/services/postgresql/api_default.go @@ -18,6 +18,8 @@ import ( "net/http" "net/url" "strings" + + oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" ) // DefaultApiService DefaultApi service @@ -40,7 +42,7 @@ func (r ApiCreateCredentialsRequest) Execute() (*CredentialsResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.CreateCredentials") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/credentials" @@ -86,30 +88,30 @@ func (r ApiCreateCredentialsRequest) Execute() (*CredentialsResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -170,7 +172,7 @@ func (r ApiCreateInstanceRequest) Execute() (*InstanceId, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.CreateInstance") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances" @@ -220,41 +222,41 @@ func (r ApiCreateInstanceRequest) Execute() (*InstanceId, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 409 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -305,7 +307,7 @@ func (r ApiDeleteCredentialsRequest) Execute() error { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.DeleteCredentials") if err != nil { - return &GenericOpenAPIError{error: err.Error()} + return &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/credentials/{credentialsId}" @@ -352,20 +354,20 @@ func (r ApiDeleteCredentialsRequest) Execute() error { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return newErr } @@ -421,7 +423,7 @@ func (r ApiDeleteInstanceRequest) Execute() error { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.DeleteInstance") if err != nil { - return &GenericOpenAPIError{error: err.Error()} + return &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}" @@ -467,31 +469,31 @@ func (r ApiDeleteInstanceRequest) Execute() error { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return newErr } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return newErr } @@ -546,7 +548,7 @@ func (r ApiGetCredentialsRequest) Execute() (*CredentialsResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetCredentials") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/credentials/{credentialsId}" @@ -593,30 +595,30 @@ func (r ApiGetCredentialsRequest) Execute() (*CredentialsResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -673,7 +675,7 @@ func (r ApiGetCredentialsIdsRequest) Execute() (*CredentialsIdsResponse, error) a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetCredentialsIds") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/credentials" @@ -719,30 +721,30 @@ func (r ApiGetCredentialsIdsRequest) Execute() (*CredentialsIdsResponse, error) } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -796,7 +798,7 @@ func (r ApiGetInstanceRequest) Execute() (*Instance, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetInstance") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}" @@ -842,41 +844,41 @@ func (r ApiGetInstanceRequest) Execute() (*Instance, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 410 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -929,7 +931,7 @@ func (r ApiGetInstancesRequest) Execute() (*InstanceList, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetInstances") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances" @@ -974,30 +976,30 @@ func (r ApiGetInstancesRequest) Execute() (*InstanceList, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1047,7 +1049,7 @@ func (r ApiGetOfferingsRequest) Execute() (*OfferingList, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetOfferings") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/offerings" @@ -1092,20 +1094,20 @@ func (r ApiGetOfferingsRequest) Execute() (*OfferingList, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1163,7 +1165,7 @@ func (r ApiUpdateInstanceRequest) Execute() error { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.UpdateInstance") if err != nil { - return &GenericOpenAPIError{error: err.Error()} + return &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}" @@ -1214,31 +1216,31 @@ func (r ApiUpdateInstanceRequest) Execute() error { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return newErr } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return newErr } diff --git a/services/postgresql/client.go b/services/postgresql/client.go index e1b71b16..152154ee 100644 --- a/services/postgresql/client.go +++ b/services/postgresql/client.go @@ -15,7 +15,6 @@ import ( "context" "encoding/json" "encoding/xml" - "errors" "fmt" "io" "log" @@ -36,11 +35,6 @@ import ( "github.com/stackitcloud/stackit-sdk-go/core/config" ) -var ( - // When a response has a bad status, this limits the number of characters that are shown from the response body - ApiErrorMaxCharacterLimit = 500 -) - var ( jsonCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:vnd\.[^;]+\+)?json)`) xmlCheck = regexp.MustCompile(`(?i:(?:application|text)/xml)`) @@ -325,7 +319,7 @@ func (c *APIClient) prepareRequest( // add form parameters and file if available. if strings.HasPrefix(headerParams["Content-Type"], "multipart/form-data") && len(formParams) > 0 || (len(formFiles) > 0) { if body != nil { - return nil, errors.New("Cannot specify postBody and multipart form at the same time.") + return nil, fmt.Errorf("cannot specify postBody and multipart form at the same time.") } body = &bytes.Buffer{} w := multipart.NewWriter(body) @@ -366,7 +360,7 @@ func (c *APIClient) prepareRequest( if strings.HasPrefix(headerParams["Content-Type"], "application/x-www-form-urlencoded") && len(formParams) > 0 { if body != nil { - return nil, errors.New("Cannot specify postBody and x-www-form-urlencoded form at the same time.") + return nil, fmt.Errorf("cannot specify postBody and x-www-form-urlencoded form at the same time.") } body = &bytes.Buffer{} body.WriteString(formParams.Encode()) @@ -486,14 +480,14 @@ func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err err return err } } else { - return errors.New("Unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined") + return fmt.Errorf("unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined") } } else if err = json.Unmarshal(b, v); err != nil { // simple model return err } return nil } - return errors.New("undefined response type") + return fmt.Errorf("undefined response type") } // Add a file to the multipart request @@ -550,7 +544,7 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e } if bodyBuf.Len() == 0 { - err = fmt.Errorf("invalid body type %s\n", contentType) + err = fmt.Errorf("invalid body type %s", contentType) return nil, err } return bodyBuf, nil @@ -630,73 +624,3 @@ func CacheExpires(r *http.Response) time.Time { func strlen(s string) int { return utf8.RuneCountInString(s) } - -// GenericOpenAPIError Provides access to the body, error and model on returned errors. -type GenericOpenAPIError struct { - statusCode int - body []byte - error string - model interface{} -} - -// Error returns non-empty string if there was an error. -func (e GenericOpenAPIError) Error() string { - // Prevent panic in case of negative value - if ApiErrorMaxCharacterLimit < 0 { - ApiErrorMaxCharacterLimit = 500 - } - - if len(e.body) <= ApiErrorMaxCharacterLimit { - return fmt.Sprintf("%s, status code %d, body: %s\n", e.error, e.statusCode, string(e.body)) - } else { - indexStart := ApiErrorMaxCharacterLimit / 2 - indexEnd := len(e.body) - ApiErrorMaxCharacterLimit/2 - numberTruncatedCharacters := indexEnd - indexStart - return fmt.Sprintf( - "%s, status code %d, body: %s [...truncated %d characters...] %s", - e.error, - e.statusCode, - string(e.body[:indexStart]), - numberTruncatedCharacters, - string(e.body[indexEnd:]), - ) - } -} - -// StatusCode returns the status code of the response -func (e GenericOpenAPIError) StatusCode() int { - return e.statusCode -} - -// Body returns the raw bytes of the response -func (e GenericOpenAPIError) Body() []byte { - return e.body -} - -// Model returns the unpacked model of the error -func (e GenericOpenAPIError) Model() interface{} { - return e.model -} - -// format error message using title and detail when model implements rfc7807 -func formatErrorMessage(status string, v interface{}) string { - str := "" - metaValue := reflect.ValueOf(v).Elem() - switch metaValue.Kind() { - case reflect.Map: - return status - case reflect.Struct: - field := metaValue.FieldByName("Title") - if field != (reflect.Value{}) { - str = fmt.Sprintf("%s", field.Interface()) - } - field = metaValue.FieldByName("Detail") - if field != (reflect.Value{}) { - str = fmt.Sprintf("%s (%s)", str, field.Interface()) - } - - // status title (detail) - return strings.TrimSpace(fmt.Sprintf("%s %s", status, str)) - } - return status -} diff --git a/services/rabbitmq/api_default.go b/services/rabbitmq/api_default.go index ffecfb5a..72453bdf 100644 --- a/services/rabbitmq/api_default.go +++ b/services/rabbitmq/api_default.go @@ -18,6 +18,8 @@ import ( "net/http" "net/url" "strings" + + oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" ) // DefaultApiService DefaultApi service @@ -40,7 +42,7 @@ func (r ApiCreateCredentialsRequest) Execute() (*CredentialsResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.CreateCredentials") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/credentials" @@ -86,30 +88,30 @@ func (r ApiCreateCredentialsRequest) Execute() (*CredentialsResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -170,7 +172,7 @@ func (r ApiCreateInstanceRequest) Execute() (*InstanceId, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.CreateInstance") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances" @@ -220,41 +222,41 @@ func (r ApiCreateInstanceRequest) Execute() (*InstanceId, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 409 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -305,7 +307,7 @@ func (r ApiDeleteCredentialsRequest) Execute() error { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.DeleteCredentials") if err != nil { - return &GenericOpenAPIError{error: err.Error()} + return &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/credentials/{credentialsId}" @@ -352,20 +354,20 @@ func (r ApiDeleteCredentialsRequest) Execute() error { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return newErr } @@ -421,7 +423,7 @@ func (r ApiDeleteInstanceRequest) Execute() error { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.DeleteInstance") if err != nil { - return &GenericOpenAPIError{error: err.Error()} + return &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}" @@ -467,31 +469,31 @@ func (r ApiDeleteInstanceRequest) Execute() error { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return newErr } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return newErr } @@ -546,7 +548,7 @@ func (r ApiGetCredentialsRequest) Execute() (*CredentialsResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetCredentials") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/credentials/{credentialsId}" @@ -593,30 +595,30 @@ func (r ApiGetCredentialsRequest) Execute() (*CredentialsResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -673,7 +675,7 @@ func (r ApiGetCredentialsIdsRequest) Execute() (*CredentialsIdsResponse, error) a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetCredentialsIds") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/credentials" @@ -719,30 +721,30 @@ func (r ApiGetCredentialsIdsRequest) Execute() (*CredentialsIdsResponse, error) } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -796,7 +798,7 @@ func (r ApiGetInstanceRequest) Execute() (*Instance, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetInstance") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}" @@ -842,41 +844,41 @@ func (r ApiGetInstanceRequest) Execute() (*Instance, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 410 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -929,7 +931,7 @@ func (r ApiGetInstancesRequest) Execute() (*InstanceList, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetInstances") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances" @@ -974,30 +976,30 @@ func (r ApiGetInstancesRequest) Execute() (*InstanceList, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1047,7 +1049,7 @@ func (r ApiGetOfferingsRequest) Execute() (*OfferingList, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetOfferings") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/offerings" @@ -1092,20 +1094,20 @@ func (r ApiGetOfferingsRequest) Execute() (*OfferingList, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1163,7 +1165,7 @@ func (r ApiUpdateInstanceRequest) Execute() error { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.UpdateInstance") if err != nil { - return &GenericOpenAPIError{error: err.Error()} + return &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}" @@ -1214,31 +1216,31 @@ func (r ApiUpdateInstanceRequest) Execute() error { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return newErr } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return newErr } diff --git a/services/rabbitmq/client.go b/services/rabbitmq/client.go index 242f7381..5f66bf81 100644 --- a/services/rabbitmq/client.go +++ b/services/rabbitmq/client.go @@ -15,7 +15,6 @@ import ( "context" "encoding/json" "encoding/xml" - "errors" "fmt" "io" "log" @@ -36,11 +35,6 @@ import ( "github.com/stackitcloud/stackit-sdk-go/core/config" ) -var ( - // When a response has a bad status, this limits the number of characters that are shown from the response body - ApiErrorMaxCharacterLimit = 500 -) - var ( jsonCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:vnd\.[^;]+\+)?json)`) xmlCheck = regexp.MustCompile(`(?i:(?:application|text)/xml)`) @@ -325,7 +319,7 @@ func (c *APIClient) prepareRequest( // add form parameters and file if available. if strings.HasPrefix(headerParams["Content-Type"], "multipart/form-data") && len(formParams) > 0 || (len(formFiles) > 0) { if body != nil { - return nil, errors.New("Cannot specify postBody and multipart form at the same time.") + return nil, fmt.Errorf("cannot specify postBody and multipart form at the same time.") } body = &bytes.Buffer{} w := multipart.NewWriter(body) @@ -366,7 +360,7 @@ func (c *APIClient) prepareRequest( if strings.HasPrefix(headerParams["Content-Type"], "application/x-www-form-urlencoded") && len(formParams) > 0 { if body != nil { - return nil, errors.New("Cannot specify postBody and x-www-form-urlencoded form at the same time.") + return nil, fmt.Errorf("cannot specify postBody and x-www-form-urlencoded form at the same time.") } body = &bytes.Buffer{} body.WriteString(formParams.Encode()) @@ -486,14 +480,14 @@ func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err err return err } } else { - return errors.New("Unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined") + return fmt.Errorf("unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined") } } else if err = json.Unmarshal(b, v); err != nil { // simple model return err } return nil } - return errors.New("undefined response type") + return fmt.Errorf("undefined response type") } // Add a file to the multipart request @@ -550,7 +544,7 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e } if bodyBuf.Len() == 0 { - err = fmt.Errorf("invalid body type %s\n", contentType) + err = fmt.Errorf("invalid body type %s", contentType) return nil, err } return bodyBuf, nil @@ -630,73 +624,3 @@ func CacheExpires(r *http.Response) time.Time { func strlen(s string) int { return utf8.RuneCountInString(s) } - -// GenericOpenAPIError Provides access to the body, error and model on returned errors. -type GenericOpenAPIError struct { - statusCode int - body []byte - error string - model interface{} -} - -// Error returns non-empty string if there was an error. -func (e GenericOpenAPIError) Error() string { - // Prevent panic in case of negative value - if ApiErrorMaxCharacterLimit < 0 { - ApiErrorMaxCharacterLimit = 500 - } - - if len(e.body) <= ApiErrorMaxCharacterLimit { - return fmt.Sprintf("%s, status code %d, body: %s\n", e.error, e.statusCode, string(e.body)) - } else { - indexStart := ApiErrorMaxCharacterLimit / 2 - indexEnd := len(e.body) - ApiErrorMaxCharacterLimit/2 - numberTruncatedCharacters := indexEnd - indexStart - return fmt.Sprintf( - "%s, status code %d, body: %s [...truncated %d characters...] %s", - e.error, - e.statusCode, - string(e.body[:indexStart]), - numberTruncatedCharacters, - string(e.body[indexEnd:]), - ) - } -} - -// StatusCode returns the status code of the response -func (e GenericOpenAPIError) StatusCode() int { - return e.statusCode -} - -// Body returns the raw bytes of the response -func (e GenericOpenAPIError) Body() []byte { - return e.body -} - -// Model returns the unpacked model of the error -func (e GenericOpenAPIError) Model() interface{} { - return e.model -} - -// format error message using title and detail when model implements rfc7807 -func formatErrorMessage(status string, v interface{}) string { - str := "" - metaValue := reflect.ValueOf(v).Elem() - switch metaValue.Kind() { - case reflect.Map: - return status - case reflect.Struct: - field := metaValue.FieldByName("Title") - if field != (reflect.Value{}) { - str = fmt.Sprintf("%s", field.Interface()) - } - field = metaValue.FieldByName("Detail") - if field != (reflect.Value{}) { - str = fmt.Sprintf("%s (%s)", str, field.Interface()) - } - - // status title (detail) - return strings.TrimSpace(fmt.Sprintf("%s %s", status, str)) - } - return status -} diff --git a/services/redis/api_default.go b/services/redis/api_default.go index 193085e6..df0051f7 100644 --- a/services/redis/api_default.go +++ b/services/redis/api_default.go @@ -18,6 +18,8 @@ import ( "net/http" "net/url" "strings" + + oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" ) // DefaultApiService DefaultApi service @@ -40,7 +42,7 @@ func (r ApiCreateCredentialsRequest) Execute() (*CredentialsResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.CreateCredentials") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/credentials" @@ -86,30 +88,30 @@ func (r ApiCreateCredentialsRequest) Execute() (*CredentialsResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -170,7 +172,7 @@ func (r ApiCreateInstanceRequest) Execute() (*InstanceId, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.CreateInstance") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances" @@ -220,41 +222,41 @@ func (r ApiCreateInstanceRequest) Execute() (*InstanceId, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 409 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -305,7 +307,7 @@ func (r ApiDeleteCredentialsRequest) Execute() error { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.DeleteCredentials") if err != nil { - return &GenericOpenAPIError{error: err.Error()} + return &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/credentials/{credentialsId}" @@ -352,20 +354,20 @@ func (r ApiDeleteCredentialsRequest) Execute() error { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return newErr } @@ -421,7 +423,7 @@ func (r ApiDeleteInstanceRequest) Execute() error { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.DeleteInstance") if err != nil { - return &GenericOpenAPIError{error: err.Error()} + return &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}" @@ -467,31 +469,31 @@ func (r ApiDeleteInstanceRequest) Execute() error { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return newErr } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return newErr } @@ -546,7 +548,7 @@ func (r ApiGetCredentialsRequest) Execute() (*CredentialsResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetCredentials") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/credentials/{credentialsId}" @@ -593,30 +595,30 @@ func (r ApiGetCredentialsRequest) Execute() (*CredentialsResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -673,7 +675,7 @@ func (r ApiGetCredentialsIdsRequest) Execute() (*CredentialsIdsResponse, error) a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetCredentialsIds") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/credentials" @@ -719,30 +721,30 @@ func (r ApiGetCredentialsIdsRequest) Execute() (*CredentialsIdsResponse, error) } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -796,7 +798,7 @@ func (r ApiGetInstanceRequest) Execute() (*Instance, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetInstance") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}" @@ -842,41 +844,41 @@ func (r ApiGetInstanceRequest) Execute() (*Instance, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 410 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -929,7 +931,7 @@ func (r ApiGetInstancesRequest) Execute() (*InstanceList, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetInstances") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances" @@ -974,30 +976,30 @@ func (r ApiGetInstancesRequest) Execute() (*InstanceList, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1047,7 +1049,7 @@ func (r ApiGetOfferingsRequest) Execute() (*OfferingList, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetOfferings") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/offerings" @@ -1092,20 +1094,20 @@ func (r ApiGetOfferingsRequest) Execute() (*OfferingList, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1163,7 +1165,7 @@ func (r ApiUpdateInstanceRequest) Execute() error { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.UpdateInstance") if err != nil { - return &GenericOpenAPIError{error: err.Error()} + return &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}" @@ -1214,31 +1216,31 @@ func (r ApiUpdateInstanceRequest) Execute() error { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return newErr } if localVarHTTPResponse.StatusCode == 404 { var v Error err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return newErr } diff --git a/services/redis/client.go b/services/redis/client.go index b1b034c5..f36c495b 100644 --- a/services/redis/client.go +++ b/services/redis/client.go @@ -15,7 +15,6 @@ import ( "context" "encoding/json" "encoding/xml" - "errors" "fmt" "io" "log" @@ -36,11 +35,6 @@ import ( "github.com/stackitcloud/stackit-sdk-go/core/config" ) -var ( - // When a response has a bad status, this limits the number of characters that are shown from the response body - ApiErrorMaxCharacterLimit = 500 -) - var ( jsonCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:vnd\.[^;]+\+)?json)`) xmlCheck = regexp.MustCompile(`(?i:(?:application|text)/xml)`) @@ -325,7 +319,7 @@ func (c *APIClient) prepareRequest( // add form parameters and file if available. if strings.HasPrefix(headerParams["Content-Type"], "multipart/form-data") && len(formParams) > 0 || (len(formFiles) > 0) { if body != nil { - return nil, errors.New("Cannot specify postBody and multipart form at the same time.") + return nil, fmt.Errorf("cannot specify postBody and multipart form at the same time.") } body = &bytes.Buffer{} w := multipart.NewWriter(body) @@ -366,7 +360,7 @@ func (c *APIClient) prepareRequest( if strings.HasPrefix(headerParams["Content-Type"], "application/x-www-form-urlencoded") && len(formParams) > 0 { if body != nil { - return nil, errors.New("Cannot specify postBody and x-www-form-urlencoded form at the same time.") + return nil, fmt.Errorf("cannot specify postBody and x-www-form-urlencoded form at the same time.") } body = &bytes.Buffer{} body.WriteString(formParams.Encode()) @@ -486,14 +480,14 @@ func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err err return err } } else { - return errors.New("Unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined") + return fmt.Errorf("unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined") } } else if err = json.Unmarshal(b, v); err != nil { // simple model return err } return nil } - return errors.New("undefined response type") + return fmt.Errorf("undefined response type") } // Add a file to the multipart request @@ -550,7 +544,7 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e } if bodyBuf.Len() == 0 { - err = fmt.Errorf("invalid body type %s\n", contentType) + err = fmt.Errorf("invalid body type %s", contentType) return nil, err } return bodyBuf, nil @@ -630,73 +624,3 @@ func CacheExpires(r *http.Response) time.Time { func strlen(s string) int { return utf8.RuneCountInString(s) } - -// GenericOpenAPIError Provides access to the body, error and model on returned errors. -type GenericOpenAPIError struct { - statusCode int - body []byte - error string - model interface{} -} - -// Error returns non-empty string if there was an error. -func (e GenericOpenAPIError) Error() string { - // Prevent panic in case of negative value - if ApiErrorMaxCharacterLimit < 0 { - ApiErrorMaxCharacterLimit = 500 - } - - if len(e.body) <= ApiErrorMaxCharacterLimit { - return fmt.Sprintf("%s, status code %d, body: %s\n", e.error, e.statusCode, string(e.body)) - } else { - indexStart := ApiErrorMaxCharacterLimit / 2 - indexEnd := len(e.body) - ApiErrorMaxCharacterLimit/2 - numberTruncatedCharacters := indexEnd - indexStart - return fmt.Sprintf( - "%s, status code %d, body: %s [...truncated %d characters...] %s", - e.error, - e.statusCode, - string(e.body[:indexStart]), - numberTruncatedCharacters, - string(e.body[indexEnd:]), - ) - } -} - -// StatusCode returns the status code of the response -func (e GenericOpenAPIError) StatusCode() int { - return e.statusCode -} - -// Body returns the raw bytes of the response -func (e GenericOpenAPIError) Body() []byte { - return e.body -} - -// Model returns the unpacked model of the error -func (e GenericOpenAPIError) Model() interface{} { - return e.model -} - -// format error message using title and detail when model implements rfc7807 -func formatErrorMessage(status string, v interface{}) string { - str := "" - metaValue := reflect.ValueOf(v).Elem() - switch metaValue.Kind() { - case reflect.Map: - return status - case reflect.Struct: - field := metaValue.FieldByName("Title") - if field != (reflect.Value{}) { - str = fmt.Sprintf("%s", field.Interface()) - } - field = metaValue.FieldByName("Detail") - if field != (reflect.Value{}) { - str = fmt.Sprintf("%s (%s)", str, field.Interface()) - } - - // status title (detail) - return strings.TrimSpace(fmt.Sprintf("%s %s", status, str)) - } - return status -} diff --git a/services/resourcemanager/api_default.go b/services/resourcemanager/api_default.go index ca359339..844a21ab 100644 --- a/services/resourcemanager/api_default.go +++ b/services/resourcemanager/api_default.go @@ -19,6 +19,8 @@ import ( "reflect" "strings" "time" + + oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" ) // DefaultApiService DefaultApi service @@ -45,7 +47,7 @@ func (r ApiCreateProjectRequest) Execute() (*ProjectResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.CreateProject") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v2/projects" @@ -91,52 +93,52 @@ func (r ApiCreateProjectRequest) Execute() (*ProjectResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v ErrorResponse err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 403 { var v ErrorResponse err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 409 { var v ErrorResponse err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -184,7 +186,7 @@ func (r ApiDeleteProjectRequest) Execute() error { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.DeleteProject") if err != nil { - return &GenericOpenAPIError{error: err.Error()} + return &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v2/projects/{containerId}" @@ -229,20 +231,20 @@ func (r ApiDeleteProjectRequest) Execute() error { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 409 { var v ErrorResponse err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return newErr } @@ -300,7 +302,7 @@ func (r ApiGetProjectRequest) Execute() (*ProjectResponseWithParents, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetProject") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v2/projects/{containerId}" @@ -348,41 +350,41 @@ func (r ApiGetProjectRequest) Execute() (*ProjectResponseWithParents, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 403 { var v ErrorResponse err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v ErrorResponse err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -479,7 +481,7 @@ func (r ApiGetProjectsRequest) Execute() (*AllProjectsResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetProjects") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v2/projects" @@ -549,52 +551,52 @@ func (r ApiGetProjectsRequest) Execute() (*AllProjectsResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v ErrorResponse err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 403 { var v ErrorResponse err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 409 { var v ErrorResponse err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -656,7 +658,7 @@ func (r ApiUpdateProjectRequest) Execute() (*ProjectResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.UpdateProject") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v2/projects/{containerId}" @@ -703,52 +705,52 @@ func (r ApiUpdateProjectRequest) Execute() (*ProjectResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 403 { var v ErrorResponse err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v ErrorResponse err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 409 { var v ErrorResponse err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } diff --git a/services/resourcemanager/client.go b/services/resourcemanager/client.go index 42da9e01..18602c36 100644 --- a/services/resourcemanager/client.go +++ b/services/resourcemanager/client.go @@ -15,7 +15,6 @@ import ( "context" "encoding/json" "encoding/xml" - "errors" "fmt" "io" "log" @@ -36,11 +35,6 @@ import ( "github.com/stackitcloud/stackit-sdk-go/core/config" ) -var ( - // When a response has a bad status, this limits the number of characters that are shown from the response body - ApiErrorMaxCharacterLimit = 500 -) - var ( jsonCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:vnd\.[^;]+\+)?json)`) xmlCheck = regexp.MustCompile(`(?i:(?:application|text)/xml)`) @@ -325,7 +319,7 @@ func (c *APIClient) prepareRequest( // add form parameters and file if available. if strings.HasPrefix(headerParams["Content-Type"], "multipart/form-data") && len(formParams) > 0 || (len(formFiles) > 0) { if body != nil { - return nil, errors.New("Cannot specify postBody and multipart form at the same time.") + return nil, fmt.Errorf("cannot specify postBody and multipart form at the same time.") } body = &bytes.Buffer{} w := multipart.NewWriter(body) @@ -366,7 +360,7 @@ func (c *APIClient) prepareRequest( if strings.HasPrefix(headerParams["Content-Type"], "application/x-www-form-urlencoded") && len(formParams) > 0 { if body != nil { - return nil, errors.New("Cannot specify postBody and x-www-form-urlencoded form at the same time.") + return nil, fmt.Errorf("cannot specify postBody and x-www-form-urlencoded form at the same time.") } body = &bytes.Buffer{} body.WriteString(formParams.Encode()) @@ -486,14 +480,14 @@ func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err err return err } } else { - return errors.New("Unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined") + return fmt.Errorf("unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined") } } else if err = json.Unmarshal(b, v); err != nil { // simple model return err } return nil } - return errors.New("undefined response type") + return fmt.Errorf("undefined response type") } // Add a file to the multipart request @@ -550,7 +544,7 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e } if bodyBuf.Len() == 0 { - err = fmt.Errorf("invalid body type %s\n", contentType) + err = fmt.Errorf("invalid body type %s", contentType) return nil, err } return bodyBuf, nil @@ -630,73 +624,3 @@ func CacheExpires(r *http.Response) time.Time { func strlen(s string) int { return utf8.RuneCountInString(s) } - -// GenericOpenAPIError Provides access to the body, error and model on returned errors. -type GenericOpenAPIError struct { - statusCode int - body []byte - error string - model interface{} -} - -// Error returns non-empty string if there was an error. -func (e GenericOpenAPIError) Error() string { - // Prevent panic in case of negative value - if ApiErrorMaxCharacterLimit < 0 { - ApiErrorMaxCharacterLimit = 500 - } - - if len(e.body) <= ApiErrorMaxCharacterLimit { - return fmt.Sprintf("%s, status code %d, body: %s\n", e.error, e.statusCode, string(e.body)) - } else { - indexStart := ApiErrorMaxCharacterLimit / 2 - indexEnd := len(e.body) - ApiErrorMaxCharacterLimit/2 - numberTruncatedCharacters := indexEnd - indexStart - return fmt.Sprintf( - "%s, status code %d, body: %s [...truncated %d characters...] %s", - e.error, - e.statusCode, - string(e.body[:indexStart]), - numberTruncatedCharacters, - string(e.body[indexEnd:]), - ) - } -} - -// StatusCode returns the status code of the response -func (e GenericOpenAPIError) StatusCode() int { - return e.statusCode -} - -// Body returns the raw bytes of the response -func (e GenericOpenAPIError) Body() []byte { - return e.body -} - -// Model returns the unpacked model of the error -func (e GenericOpenAPIError) Model() interface{} { - return e.model -} - -// format error message using title and detail when model implements rfc7807 -func formatErrorMessage(status string, v interface{}) string { - str := "" - metaValue := reflect.ValueOf(v).Elem() - switch metaValue.Kind() { - case reflect.Map: - return status - case reflect.Struct: - field := metaValue.FieldByName("Title") - if field != (reflect.Value{}) { - str = fmt.Sprintf("%s", field.Interface()) - } - field = metaValue.FieldByName("Detail") - if field != (reflect.Value{}) { - str = fmt.Sprintf("%s (%s)", str, field.Interface()) - } - - // status title (detail) - return strings.TrimSpace(fmt.Sprintf("%s %s", status, str)) - } - return status -} diff --git a/services/secretsmanager/api_default.go b/services/secretsmanager/api_default.go index 189b2488..39425494 100644 --- a/services/secretsmanager/api_default.go +++ b/services/secretsmanager/api_default.go @@ -18,6 +18,8 @@ import ( "net/http" "net/url" "strings" + + oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" ) // DefaultApiService DefaultApi service @@ -46,7 +48,7 @@ func (r ApiCreateAclRequest) Execute() (*Acl, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.CreateAcl") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/acls" @@ -97,20 +99,20 @@ func (r ApiCreateAclRequest) Execute() (*Acl, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -169,7 +171,7 @@ func (r ApiCreateInstanceRequest) Execute() (*Instance, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.CreateInstance") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances" @@ -219,20 +221,20 @@ func (r ApiCreateInstanceRequest) Execute() (*Instance, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -289,7 +291,7 @@ func (r ApiCreateUserRequest) Execute() (*User, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.CreateUser") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/users" @@ -340,20 +342,20 @@ func (r ApiCreateUserRequest) Execute() (*User, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -407,7 +409,7 @@ func (r ApiDeleteAclRequest) Execute() error { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.DeleteAcl") if err != nil { - return &GenericOpenAPIError{error: err.Error()} + return &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/acls/{aclId}" @@ -454,10 +456,10 @@ func (r ApiDeleteAclRequest) Execute() error { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } return newErr } @@ -513,7 +515,7 @@ func (r ApiDeleteInstanceRequest) Execute() error { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.DeleteInstance") if err != nil { - return &GenericOpenAPIError{error: err.Error()} + return &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}" @@ -559,10 +561,10 @@ func (r ApiDeleteInstanceRequest) Execute() error { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } return newErr } @@ -616,7 +618,7 @@ func (r ApiDeleteUserRequest) Execute() error { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.DeleteUser") if err != nil { - return &GenericOpenAPIError{error: err.Error()} + return &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/users/{userId}" @@ -663,10 +665,10 @@ func (r ApiDeleteUserRequest) Execute() error { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } return newErr } @@ -724,7 +726,7 @@ func (r ApiGetAclRequest) Execute() (*Acl, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetAcl") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/acls/{aclId}" @@ -771,20 +773,20 @@ func (r ApiGetAclRequest) Execute() (*Acl, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -841,7 +843,7 @@ func (r ApiGetAclsRequest) Execute() (*AclList, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetAcls") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/acls" @@ -887,20 +889,20 @@ func (r ApiGetAclsRequest) Execute() (*AclList, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -954,7 +956,7 @@ func (r ApiGetInstanceRequest) Execute() (*Instance, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetInstance") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}" @@ -1000,20 +1002,20 @@ func (r ApiGetInstanceRequest) Execute() (*Instance, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1066,7 +1068,7 @@ func (r ApiGetInstancesRequest) Execute() (*InstanceList, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetInstances") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances" @@ -1111,20 +1113,20 @@ func (r ApiGetInstancesRequest) Execute() (*InstanceList, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1176,7 +1178,7 @@ func (r ApiGetUserRequest) Execute() (*User, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetUser") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/users/{userId}" @@ -1223,20 +1225,20 @@ func (r ApiGetUserRequest) Execute() (*User, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1293,7 +1295,7 @@ func (r ApiGetUsersRequest) Execute() (*UserList, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetUsers") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/users" @@ -1339,20 +1341,20 @@ func (r ApiGetUsersRequest) Execute() (*UserList, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1412,7 +1414,7 @@ func (r ApiUpdateAclRequest) Execute() error { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.UpdateAcl") if err != nil { - return &GenericOpenAPIError{error: err.Error()} + return &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/acls/{aclId}" @@ -1464,10 +1466,10 @@ func (r ApiUpdateAclRequest) Execute() error { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } return newErr } @@ -1530,7 +1532,7 @@ func (r ApiUpdateUserRequest) Execute() error { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.UpdateUser") if err != nil { - return &GenericOpenAPIError{error: err.Error()} + return &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/instances/{instanceId}/users/{userId}" @@ -1582,10 +1584,10 @@ func (r ApiUpdateUserRequest) Execute() error { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } return newErr } diff --git a/services/secretsmanager/client.go b/services/secretsmanager/client.go index 355899e1..f8024e66 100644 --- a/services/secretsmanager/client.go +++ b/services/secretsmanager/client.go @@ -15,7 +15,6 @@ import ( "context" "encoding/json" "encoding/xml" - "errors" "fmt" "io" "log" @@ -36,11 +35,6 @@ import ( "github.com/stackitcloud/stackit-sdk-go/core/config" ) -var ( - // When a response has a bad status, this limits the number of characters that are shown from the response body - ApiErrorMaxCharacterLimit = 500 -) - var ( jsonCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:vnd\.[^;]+\+)?json)`) xmlCheck = regexp.MustCompile(`(?i:(?:application|text)/xml)`) @@ -325,7 +319,7 @@ func (c *APIClient) prepareRequest( // add form parameters and file if available. if strings.HasPrefix(headerParams["Content-Type"], "multipart/form-data") && len(formParams) > 0 || (len(formFiles) > 0) { if body != nil { - return nil, errors.New("Cannot specify postBody and multipart form at the same time.") + return nil, fmt.Errorf("cannot specify postBody and multipart form at the same time.") } body = &bytes.Buffer{} w := multipart.NewWriter(body) @@ -366,7 +360,7 @@ func (c *APIClient) prepareRequest( if strings.HasPrefix(headerParams["Content-Type"], "application/x-www-form-urlencoded") && len(formParams) > 0 { if body != nil { - return nil, errors.New("Cannot specify postBody and x-www-form-urlencoded form at the same time.") + return nil, fmt.Errorf("cannot specify postBody and x-www-form-urlencoded form at the same time.") } body = &bytes.Buffer{} body.WriteString(formParams.Encode()) @@ -486,14 +480,14 @@ func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err err return err } } else { - return errors.New("Unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined") + return fmt.Errorf("unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined") } } else if err = json.Unmarshal(b, v); err != nil { // simple model return err } return nil } - return errors.New("undefined response type") + return fmt.Errorf("undefined response type") } // Add a file to the multipart request @@ -550,7 +544,7 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e } if bodyBuf.Len() == 0 { - err = fmt.Errorf("invalid body type %s\n", contentType) + err = fmt.Errorf("invalid body type %s", contentType) return nil, err } return bodyBuf, nil @@ -630,73 +624,3 @@ func CacheExpires(r *http.Response) time.Time { func strlen(s string) int { return utf8.RuneCountInString(s) } - -// GenericOpenAPIError Provides access to the body, error and model on returned errors. -type GenericOpenAPIError struct { - statusCode int - body []byte - error string - model interface{} -} - -// Error returns non-empty string if there was an error. -func (e GenericOpenAPIError) Error() string { - // Prevent panic in case of negative value - if ApiErrorMaxCharacterLimit < 0 { - ApiErrorMaxCharacterLimit = 500 - } - - if len(e.body) <= ApiErrorMaxCharacterLimit { - return fmt.Sprintf("%s, status code %d, body: %s\n", e.error, e.statusCode, string(e.body)) - } else { - indexStart := ApiErrorMaxCharacterLimit / 2 - indexEnd := len(e.body) - ApiErrorMaxCharacterLimit/2 - numberTruncatedCharacters := indexEnd - indexStart - return fmt.Sprintf( - "%s, status code %d, body: %s [...truncated %d characters...] %s", - e.error, - e.statusCode, - string(e.body[:indexStart]), - numberTruncatedCharacters, - string(e.body[indexEnd:]), - ) - } -} - -// StatusCode returns the status code of the response -func (e GenericOpenAPIError) StatusCode() int { - return e.statusCode -} - -// Body returns the raw bytes of the response -func (e GenericOpenAPIError) Body() []byte { - return e.body -} - -// Model returns the unpacked model of the error -func (e GenericOpenAPIError) Model() interface{} { - return e.model -} - -// format error message using title and detail when model implements rfc7807 -func formatErrorMessage(status string, v interface{}) string { - str := "" - metaValue := reflect.ValueOf(v).Elem() - switch metaValue.Kind() { - case reflect.Map: - return status - case reflect.Struct: - field := metaValue.FieldByName("Title") - if field != (reflect.Value{}) { - str = fmt.Sprintf("%s", field.Interface()) - } - field = metaValue.FieldByName("Detail") - if field != (reflect.Value{}) { - str = fmt.Sprintf("%s (%s)", str, field.Interface()) - } - - // status title (detail) - return strings.TrimSpace(fmt.Sprintf("%s %s", status, str)) - } - return status -} diff --git a/services/ske/api_default.go b/services/ske/api_default.go index cc7b9274..b78334ad 100644 --- a/services/ske/api_default.go +++ b/services/ske/api_default.go @@ -18,6 +18,8 @@ import ( "net/http" "net/url" "strings" + + oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" ) // DefaultApiService DefaultApi service @@ -46,7 +48,7 @@ func (r ApiCreateOrUpdateClusterRequest) Execute() (*ClusterResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.CreateOrUpdateCluster") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/clusters/{clusterName}" @@ -97,50 +99,50 @@ func (r ApiCreateOrUpdateClusterRequest) Execute() (*ClusterResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v map[string]interface{} err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v map[string]interface{} err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } var v RuntimeError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -193,7 +195,7 @@ func (r ApiCreateProjectRequest) Execute() (*ProjectResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.CreateProject") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}" @@ -238,39 +240,39 @@ func (r ApiCreateProjectRequest) Execute() (*ProjectResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v map[string]interface{} err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } var v RuntimeError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -321,7 +323,7 @@ func (r ApiDeleteClusterRequest) Execute() (map[string]interface{}, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.DeleteCluster") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/clusters/{clusterName}" @@ -367,28 +369,28 @@ func (r ApiDeleteClusterRequest) Execute() (map[string]interface{}, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } var v RuntimeError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -441,7 +443,7 @@ func (r ApiDeleteProjectRequest) Execute() (map[string]interface{}, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.DeleteProject") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}" @@ -486,39 +488,39 @@ func (r ApiDeleteProjectRequest) Execute() (map[string]interface{}, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v map[string]interface{} err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } var v RuntimeError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -569,7 +571,7 @@ func (r ApiGetClusterRequest) Execute() (*ClusterResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetCluster") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/clusters/{clusterName}" @@ -615,28 +617,28 @@ func (r ApiGetClusterRequest) Execute() (*ClusterResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } var v RuntimeError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -689,7 +691,7 @@ func (r ApiGetClustersRequest) Execute() (*ClustersResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetClusters") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/clusters" @@ -734,50 +736,50 @@ func (r ApiGetClustersRequest) Execute() (*ClustersResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v map[string]interface{} err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v map[string]interface{} err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } var v RuntimeError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -828,7 +830,7 @@ func (r ApiGetCredentialsRequest) Execute() (*CredentialsResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetCredentials") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/clusters/{clusterName}/credentials" @@ -874,50 +876,50 @@ func (r ApiGetCredentialsRequest) Execute() (*CredentialsResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 400 { var v map[string]interface{} err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } if localVarHTTPResponse.StatusCode == 404 { var v map[string]interface{} err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } var v RuntimeError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -969,7 +971,7 @@ func (r ApiGetOptionsRequest) Execute() (*ProviderOptions, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetOptions") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/provider-options" @@ -1013,28 +1015,28 @@ func (r ApiGetOptionsRequest) Execute() (*ProviderOptions, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } var v RuntimeError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1081,7 +1083,7 @@ func (r ApiGetProjectRequest) Execute() (*ProjectResponse, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.GetProject") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}" @@ -1126,39 +1128,39 @@ func (r ApiGetProjectRequest) Execute() (*ProjectResponse, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v map[string]interface{} err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } var v RuntimeError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1209,7 +1211,7 @@ func (r ApiTriggerHibernateRequest) Execute() (map[string]interface{}, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.TriggerHibernate") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/clusters/{clusterName}/hibernate" @@ -1255,39 +1257,39 @@ func (r ApiTriggerHibernateRequest) Execute() (map[string]interface{}, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v map[string]interface{} err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } var v RuntimeError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1341,7 +1343,7 @@ func (r ApiTriggerMaintenanceRequest) Execute() (map[string]interface{}, error) a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.TriggerMaintenance") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/clusters/{clusterName}/maintenance" @@ -1387,39 +1389,39 @@ func (r ApiTriggerMaintenanceRequest) Execute() (map[string]interface{}, error) } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v map[string]interface{} err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } var v RuntimeError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1473,7 +1475,7 @@ func (r ApiTriggerReconcileRequest) Execute() (map[string]interface{}, error) { a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.TriggerReconcile") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/clusters/{clusterName}/reconcile" @@ -1519,39 +1521,39 @@ func (r ApiTriggerReconcileRequest) Execute() (map[string]interface{}, error) { } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v map[string]interface{} err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } var v RuntimeError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } @@ -1605,7 +1607,7 @@ func (r ApiTriggerRotateCredentialsRequest) Execute() (map[string]interface{}, e a := r.apiService localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DefaultApiService.TriggerRotateCredentials") if err != nil { - return localVarReturnValue, &GenericOpenAPIError{error: err.Error()} + return localVarReturnValue, &oapiError.GenericOpenAPIError{ErrorMessage: err.Error()} } localVarPath := localBasePath + "/v1/projects/{projectId}/clusters/{clusterName}/rotate-credentials" @@ -1651,39 +1653,39 @@ func (r ApiTriggerRotateCredentialsRequest) Execute() (map[string]interface{}, e } if localVarHTTPResponse.StatusCode >= 300 { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: localVarHTTPResponse.Status, + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: localVarHTTPResponse.Status, } if localVarHTTPResponse.StatusCode == 404 { var v map[string]interface{} err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } var v RuntimeError err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr.error = err.Error() + newErr.ErrorMessage = err.Error() return localVarReturnValue, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.ErrorMessage = oapiError.FormatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.Model = v return localVarReturnValue, newErr } err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type")) if err != nil { - newErr := &GenericOpenAPIError{ - statusCode: localVarHTTPResponse.StatusCode, - body: localVarBody, - error: err.Error(), + newErr := &oapiError.GenericOpenAPIError{ + StatusCode: localVarHTTPResponse.StatusCode, + Body: localVarBody, + ErrorMessage: err.Error(), } return localVarReturnValue, newErr } diff --git a/services/ske/client.go b/services/ske/client.go index 58716f1e..f5a45ee9 100644 --- a/services/ske/client.go +++ b/services/ske/client.go @@ -15,7 +15,6 @@ import ( "context" "encoding/json" "encoding/xml" - "errors" "fmt" "io" "log" @@ -36,11 +35,6 @@ import ( "github.com/stackitcloud/stackit-sdk-go/core/config" ) -var ( - // When a response has a bad status, this limits the number of characters that are shown from the response body - ApiErrorMaxCharacterLimit = 500 -) - var ( jsonCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:vnd\.[^;]+\+)?json)`) xmlCheck = regexp.MustCompile(`(?i:(?:application|text)/xml)`) @@ -325,7 +319,7 @@ func (c *APIClient) prepareRequest( // add form parameters and file if available. if strings.HasPrefix(headerParams["Content-Type"], "multipart/form-data") && len(formParams) > 0 || (len(formFiles) > 0) { if body != nil { - return nil, errors.New("Cannot specify postBody and multipart form at the same time.") + return nil, fmt.Errorf("cannot specify postBody and multipart form at the same time.") } body = &bytes.Buffer{} w := multipart.NewWriter(body) @@ -366,7 +360,7 @@ func (c *APIClient) prepareRequest( if strings.HasPrefix(headerParams["Content-Type"], "application/x-www-form-urlencoded") && len(formParams) > 0 { if body != nil { - return nil, errors.New("Cannot specify postBody and x-www-form-urlencoded form at the same time.") + return nil, fmt.Errorf("cannot specify postBody and x-www-form-urlencoded form at the same time.") } body = &bytes.Buffer{} body.WriteString(formParams.Encode()) @@ -486,14 +480,14 @@ func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err err return err } } else { - return errors.New("Unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined") + return fmt.Errorf("unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined") } } else if err = json.Unmarshal(b, v); err != nil { // simple model return err } return nil } - return errors.New("undefined response type") + return fmt.Errorf("undefined response type") } // Add a file to the multipart request @@ -550,7 +544,7 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e } if bodyBuf.Len() == 0 { - err = fmt.Errorf("invalid body type %s\n", contentType) + err = fmt.Errorf("invalid body type %s", contentType) return nil, err } return bodyBuf, nil @@ -630,73 +624,3 @@ func CacheExpires(r *http.Response) time.Time { func strlen(s string) int { return utf8.RuneCountInString(s) } - -// GenericOpenAPIError Provides access to the body, error and model on returned errors. -type GenericOpenAPIError struct { - statusCode int - body []byte - error string - model interface{} -} - -// Error returns non-empty string if there was an error. -func (e GenericOpenAPIError) Error() string { - // Prevent panic in case of negative value - if ApiErrorMaxCharacterLimit < 0 { - ApiErrorMaxCharacterLimit = 500 - } - - if len(e.body) <= ApiErrorMaxCharacterLimit { - return fmt.Sprintf("%s, status code %d, body: %s\n", e.error, e.statusCode, string(e.body)) - } else { - indexStart := ApiErrorMaxCharacterLimit / 2 - indexEnd := len(e.body) - ApiErrorMaxCharacterLimit/2 - numberTruncatedCharacters := indexEnd - indexStart - return fmt.Sprintf( - "%s, status code %d, body: %s [...truncated %d characters...] %s", - e.error, - e.statusCode, - string(e.body[:indexStart]), - numberTruncatedCharacters, - string(e.body[indexEnd:]), - ) - } -} - -// StatusCode returns the status code of the response -func (e GenericOpenAPIError) StatusCode() int { - return e.statusCode -} - -// Body returns the raw bytes of the response -func (e GenericOpenAPIError) Body() []byte { - return e.body -} - -// Model returns the unpacked model of the error -func (e GenericOpenAPIError) Model() interface{} { - return e.model -} - -// format error message using title and detail when model implements rfc7807 -func formatErrorMessage(status string, v interface{}) string { - str := "" - metaValue := reflect.ValueOf(v).Elem() - switch metaValue.Kind() { - case reflect.Map: - return status - case reflect.Struct: - field := metaValue.FieldByName("Title") - if field != (reflect.Value{}) { - str = fmt.Sprintf("%s", field.Interface()) - } - field = metaValue.FieldByName("Detail") - if field != (reflect.Value{}) { - str = fmt.Sprintf("%s (%s)", str, field.Interface()) - } - - // status title (detail) - return strings.TrimSpace(fmt.Sprintf("%s %s", status, str)) - } - return status -}