From 1c06c9cb605b4c759736d41b548e29939a984c55 Mon Sep 17 00:00:00 2001 From: Melvin Zottola Date: Mon, 6 Nov 2023 16:57:08 +0100 Subject: [PATCH] chore: Improve HTTP error message response Dedicated structure to return HTTP error messages. ATM only for DeployService method --- utils/error.go | 25 +++++++++++++++++++++++++ utils/qovery.go | 14 +++++++------- 2 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 utils/error.go diff --git a/utils/error.go b/utils/error.go new file mode 100644 index 00000000..e15a0948 --- /dev/null +++ b/utils/error.go @@ -0,0 +1,25 @@ +package utils + +import ( + "fmt" + "io" + "net/http" +) + +type HttpResponseError struct { + Code int + Message string +} + +func toHttpResponseError(response *http.Response) *HttpResponseError { + body, _ := io.ReadAll(response.Body) + response.Body.Close() + return &HttpResponseError{ + Code: response.StatusCode, + Message: string(body), + } +} + +func (m *HttpResponseError) Error() string { + return fmt.Sprintf("\nHTTP Response Code: %d\nError Message: %s", m.Code, m.Message) +} diff --git a/utils/qovery.go b/utils/qovery.go index 0057b99f..f9f37c89 100644 --- a/utils/qovery.go +++ b/utils/qovery.go @@ -1726,10 +1726,10 @@ func DeleteServices(client *qovery.APIClient, envId string, serviceIds []string, } func DeployService(client *qovery.APIClient, envId string, serviceId string, serviceType ServiceType, request interface{}, watchFlag bool) (string, error) { - statuses, _, err := client.EnvironmentMainCallsAPI.GetEnvironmentStatuses(context.Background(), envId).Execute() + statuses, resp, err := client.EnvironmentMainCallsAPI.GetEnvironmentStatuses(context.Background(), envId).Execute() if err != nil { - return "", err + return "", toHttpResponseError(resp) } if IsTerminalState(statuses.GetEnvironment().State) { @@ -1738,9 +1738,9 @@ func DeployService(client *qovery.APIClient, envId string, serviceId string, ser for _, application := range statuses.GetApplications() { if application.Id == serviceId && IsTerminalState(application.State) { req := request.(qovery.DeployRequest) - _, _, err := client.ApplicationActionsAPI.DeployApplication(context.Background(), serviceId).DeployRequest(req).Execute() + _, resp, err := client.ApplicationActionsAPI.DeployApplication(context.Background(), serviceId).DeployRequest(req).Execute() if err != nil { - return "", err + return "", toHttpResponseError(resp) } // get current deployment id @@ -1757,7 +1757,7 @@ func DeployService(client *qovery.APIClient, envId string, serviceId string, ser if database.Id == serviceId && IsTerminalState(database.State) { _, _, err := client.DatabaseActionsAPI.DeployDatabase(context.Background(), serviceId).Execute() if err != nil { - return "", err + return "", toHttpResponseError(resp) } if watchFlag { @@ -1773,7 +1773,7 @@ func DeployService(client *qovery.APIClient, envId string, serviceId string, ser req := request.(qovery.ContainerDeployRequest) _, _, err := client.ContainerActionsAPI.DeployContainer(context.Background(), serviceId).ContainerDeployRequest(req).Execute() if err != nil { - return "", err + return "", toHttpResponseError(resp) } if watchFlag { @@ -1789,7 +1789,7 @@ func DeployService(client *qovery.APIClient, envId string, serviceId string, ser req := request.(qovery.JobDeployRequest) _, _, err := client.JobActionsAPI.DeployJob(context.Background(), serviceId).JobDeployRequest(req).Execute() if err != nil { - return "", err + return "", toHttpResponseError(resp) } if watchFlag {