Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Improve HTTP error message response #216

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading