Skip to content

Commit

Permalink
Fixes issue handling nil response with error on graph calls (#1008)
Browse files Browse the repository at this point in the history
Resolves issue #997 with bad error handling in executing MS graph requests.
  • Loading branch information
wbreza committed Oct 26, 2022
1 parent b67a149 commit fd3ed3b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
4 changes: 2 additions & 2 deletions cli/azd/pkg/azsdk/zip_deploy_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions cli/azd/pkg/graphsdk/application_request_builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion cli/azd/pkg/graphsdk/me_request_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions cli/azd/pkg/graphsdk/service_principal_request_builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions cli/azd/pkg/httputil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

0 comments on commit fd3ed3b

Please sign in to comment.