Skip to content

Commit

Permalink
chore: Improve HTTP error message response
Browse files Browse the repository at this point in the history
Dedicated structure to return HTTP error messages.
ATM only for DeployService method
  • Loading branch information
mzottola committed Nov 6, 2023
1 parent e2e4eb7 commit 1c06c9c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
25 changes: 25 additions & 0 deletions utils/error.go
Original file line number Diff line number Diff line change
@@ -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)
}
14 changes: 7 additions & 7 deletions utils/qovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 1c06c9c

Please sign in to comment.