diff --git a/cli/azd/pkg/azsdk/zip_deploy_client.go b/cli/azd/pkg/azsdk/zip_deploy_client.go index cdc21e2b254..738e6281ed3 100644 --- a/cli/azd/pkg/azsdk/zip_deploy_client.go +++ b/cli/azd/pkg/azsdk/zip_deploy_client.go @@ -89,7 +89,7 @@ func (c *ZipDeployClient) BeginDeploy( response, err := c.pipeline.Do(request) if err != nil { - return nil, runtime.NewResponseError(response) + return nil, httputil.HandleRequestError(response, err) } defer response.Body.Close() @@ -181,7 +181,7 @@ func (h *deployPollingHandler) Poll(ctx context.Context) (*http.Response, error) response, err := h.pipeline.Do(req) if err != nil { - return nil, runtime.NewResponseError(response) + return nil, httputil.HandleRequestError(response, err) } if !runtime.HasStatusCode(response, http.StatusAccepted) && !runtime.HasStatusCode(response, http.StatusOK) { diff --git a/cli/azd/pkg/graphsdk/application_request_builders.go b/cli/azd/pkg/graphsdk/application_request_builders.go index 55518133a31..36116a726e1 100644 --- a/cli/azd/pkg/graphsdk/application_request_builders.go +++ b/cli/azd/pkg/graphsdk/application_request_builders.go @@ -30,7 +30,7 @@ func (c *ApplicationListRequestBuilder) Get(ctx context.Context) (*ApplicationLi res, err := c.client.pipeline.Do(req) if err != nil { - return nil, runtime.NewResponseError(res) + return nil, httputil.HandleRequestError(res, err) } if !runtime.HasStatusCode(res, http.StatusOK) { @@ -53,7 +53,7 @@ func (c *ApplicationListRequestBuilder) Post(ctx context.Context, application *A res, err := c.client.pipeline.Do(req) if err != nil { - return nil, runtime.NewResponseError(res) + return nil, httputil.HandleRequestError(res, err) } if !runtime.HasStatusCode(res, http.StatusCreated) { @@ -83,7 +83,7 @@ func (c *ApplicationItemRequestBuilder) Get(ctx context.Context) (*Application, res, err := c.client.pipeline.Do(req) if err != nil { - return nil, runtime.NewResponseError(res) + return nil, httputil.HandleRequestError(res, err) } if !runtime.HasStatusCode(res, http.StatusOK) { @@ -114,7 +114,7 @@ func (c *ApplicationItemRequestBuilder) RemovePassword(ctx context.Context, keyI res, err := c.client.pipeline.Do(req) if err != nil { - return runtime.NewResponseError(res) + return httputil.HandleRequestError(res, err) } if !runtime.HasStatusCode(res, http.StatusNoContent) { @@ -143,7 +143,7 @@ func (c *ApplicationItemRequestBuilder) AddPassword(ctx context.Context) (*Appli res, err := c.client.pipeline.Do(req) if err != nil { - return nil, runtime.NewResponseError(res) + return nil, httputil.HandleRequestError(res, err) } if !runtime.HasStatusCode(res, http.StatusOK) { diff --git a/cli/azd/pkg/graphsdk/me_request_builder.go b/cli/azd/pkg/graphsdk/me_request_builder.go index 113de258d5a..7848024259e 100644 --- a/cli/azd/pkg/graphsdk/me_request_builder.go +++ b/cli/azd/pkg/graphsdk/me_request_builder.go @@ -29,7 +29,7 @@ func (b *MeItemRequestBuilder) Get(ctx context.Context) (*UserProfile, error) { res, err := b.client.pipeline.Do(req) if err != nil { - return nil, runtime.NewResponseError(res) + return nil, httputil.HandleRequestError(res, err) } if !runtime.HasStatusCode(res, http.StatusOK) { diff --git a/cli/azd/pkg/graphsdk/service_principal_request_builders.go b/cli/azd/pkg/graphsdk/service_principal_request_builders.go index 95a061d74ef..2a431a10e4f 100644 --- a/cli/azd/pkg/graphsdk/service_principal_request_builders.go +++ b/cli/azd/pkg/graphsdk/service_principal_request_builders.go @@ -25,7 +25,7 @@ func (c *ServicePrincipalListRequestBuilder) Get(ctx context.Context) (*ServiceP res, err := c.client.pipeline.Do(req) if err != nil { - return nil, runtime.NewResponseError(res) + return nil, httputil.HandleRequestError(res, err) } if !runtime.HasStatusCode(res, http.StatusOK) { @@ -55,7 +55,7 @@ func (c *ServicePrincipalListRequestBuilder) Post( res, err := c.client.pipeline.Do(req) if err != nil { - return nil, runtime.NewResponseError(res) + return nil, httputil.HandleRequestError(res, err) } if !runtime.HasStatusCode(res, http.StatusCreated) { @@ -85,7 +85,7 @@ func (b *ServicePrincipalItemRequestBuilder) Get(ctx context.Context) (*ServiceP res, err := b.client.pipeline.Do(req) if err != nil { - return nil, runtime.NewResponseError(res) + return nil, httputil.HandleRequestError(res, err) } if !runtime.HasStatusCode(res, http.StatusOK) { diff --git a/cli/azd/pkg/httputil/util.go b/cli/azd/pkg/httputil/util.go index 5c669c3dd62..4dfd5908465 100644 --- a/cli/azd/pkg/httputil/util.go +++ b/cli/azd/pkg/httputil/util.go @@ -5,6 +5,8 @@ import ( "fmt" "io" "net/http" + + "github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime" ) // Reads the raw HTTP response and attempt to convert it into the specified type @@ -25,3 +27,12 @@ func ReadRawResponse[T any](response *http.Response) (*T, error) { return instance, nil } + +// Handles and errors executing the http request +func HandleRequestError(response *http.Response, err error) error { + if response == nil { + return fmt.Errorf("failed executing request: %w", err) + } + + return runtime.NewResponseError(response) +}