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 90abf32
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 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)
}
16 changes: 8 additions & 8 deletions utils/qovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -1725,11 +1725,11 @@ func DeleteServices(client *qovery.APIClient, envId string, serviceIds []string,
return DeleteServices(client, envId, serviceIds, serviceType)
}

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()
func DeployService(client *qovery.APIClient, envId string, serviceId string, serviceType ServiceType, request interface{}, watchFlag bool) (string, *HttpResponseError) {
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 90abf32

Please sign in to comment.