Skip to content

SDK LRO Poller: Rest the result.HttpResponse.Body before returning #1124

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion sdk/client/resourcemanager/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ API Response:

// parseErrorFromApiResponse parses the error from the API Response
// into an Error type, which allows for better surfacing of errors
func parseErrorFromApiResponse(response http.Response) (*Error, error) {
func parseErrorFromApiResponse(response *http.Response) (*Error, error) {
respBody, err := io.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("parsing response body: %+v", err)
Expand Down
27 changes: 17 additions & 10 deletions sdk/client/resourcemanager/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestParseErrorFromApiResponse_LongRunningOperation(t *testing.T) {
Message: "The specified name is already in use.",
Status: "Failed",
}
actual, err := parseErrorFromApiResponse(input)
actual, err := parseErrorFromApiResponse(&input)
if err != nil {
t.Fatalf("parsing error from api response: %+v", err)
}
Expand All @@ -34,6 +34,13 @@ func TestParseErrorFromApiResponse_LongRunningOperation(t *testing.T) {
if !reflect.DeepEqual(*actual, expected) {
t.Fatalf("expected and actual didn't match. Expected: %+v\n\n Actual: %+v", expected, actual)
}
b, err := io.ReadAll(input.Body)
if err != nil {
t.Fatalf("reading the response body after parsing: %v", err)
}
if !reflect.DeepEqual(string(b), body) {
t.Fatalf("expected and actual didn't match. Expected: %+v\n\n Actual: %+v", body, string(b))
}
}

func TestParseErrorFromApiResponse_ResourceManagerType1(t *testing.T) {
Expand All @@ -49,7 +56,7 @@ func TestParseErrorFromApiResponse_ResourceManagerType1(t *testing.T) {
Message: "Cannot update the site 'func-tfbug-b78d100425' because it uses AlwaysOn feature which is not allowed in the target compute mode.",
Status: "Unknown",
}
actual, err := parseErrorFromApiResponse(input)
actual, err := parseErrorFromApiResponse(&input)
if err != nil {
t.Fatalf("parsing error from api response: %+v", err)
}
Expand All @@ -74,7 +81,7 @@ func TestParseErrorFromApiResponse_ResourceManagerType2(t *testing.T) {
Message: "Failed to start operation. Verify input and try operation again.\nFailed to start operation. Verify input and try operation again.\nPossible Causes: \"Invalid parameters were specified.\"\nRecommended Action: \"Verify the input and try again.\"",
Status: "BadRequest",
}
actual, err := parseErrorFromApiResponse(input)
actual, err := parseErrorFromApiResponse(&input)
if err != nil {
t.Fatalf("parsing error from api response: %+v", err)
}
Expand All @@ -98,7 +105,7 @@ func TestParseErrorFromApiResponse_ResourceManagerType2Alt(t *testing.T) {
Message: "The request is not valid.\nResource name is not valid. It must be between 3 and 26 characters and can only include alphanumeric characters and hyphens.",
Status: "ValidationError",
}
actual, err := parseErrorFromApiResponse(input)
actual, err := parseErrorFromApiResponse(&input)
if err != nil {
t.Fatalf("parsing error from api response: %+v", err)
}
Expand All @@ -123,7 +130,7 @@ func TestParseErrorFromApiResponse_ResourceManagerType3(t *testing.T) {
Message: "Outbound Trust/Untrust certificate can not be deleted from rule stack SUBSCRIPTION~XXXXXXXX-XXXX-XXXX-XXXXXXXXXXXX~RG~acctestRG-PAN-230725055238618023~STACK~testAcc-palrs-230725055238618023 as associated rule list(s) already has SSL outbound inspection set with status code BadRequest",
Status: "Failed",
}
actual, err := parseErrorFromApiResponse(input)
actual, err := parseErrorFromApiResponse(&input)
if err != nil {
t.Fatalf("parsing error from api response: %+v", err)
}
Expand All @@ -147,7 +154,7 @@ func TestParseErrorFromApiResponse_EmptyResponseShouldOutputHttpResponseAnyway(t
Message: "Couldn't parse Azure API Response into a friendly error - please see the original HTTP Response for more details (and file a bug so we can fix this!).",
Status: "Unknown",
}
actual, err := parseErrorFromApiResponse(input)
actual, err := parseErrorFromApiResponse(&input)
if err != nil {
t.Fatalf("parsing error from api response: %+v", err)
}
Expand All @@ -172,7 +179,7 @@ func TestParseErrorFromApiResponse_UnexpectedHtmlShouldOutputHttpResponseAnyway(
Message: "Couldn't parse Azure API Response into a friendly error - please see the original HTTP Response for more details (and file a bug so we can fix this!).",
Status: "Unknown",
}
actual, err := parseErrorFromApiResponse(input)
actual, err := parseErrorFromApiResponse(&input)
if err != nil {
t.Fatalf("parsing error from api response: %+v", err)
}
Expand All @@ -197,7 +204,7 @@ func TestParseErrorFromApiResponse_UnexpectedLiteralStringShouldOutputHttpRespon
Message: "Couldn't parse Azure API Response into a friendly error - please see the original HTTP Response for more details (and file a bug so we can fix this!).",
Status: "Unknown",
}
actual, err := parseErrorFromApiResponse(input)
actual, err := parseErrorFromApiResponse(&input)
if err != nil {
t.Fatalf("parsing error from api response: %+v", err)
}
Expand All @@ -222,7 +229,7 @@ func TestParseErrorFromApiResponse_UnexpectedLiteralQuotedStringShouldOutputHttp
Message: "Couldn't parse Azure API Response into a friendly error - please see the original HTTP Response for more details (and file a bug so we can fix this!).",
Status: "Unknown",
}
actual, err := parseErrorFromApiResponse(input)
actual, err := parseErrorFromApiResponse(&input)
if err != nil {
t.Fatalf("parsing error from api response: %+v", err)
}
Expand All @@ -246,7 +253,7 @@ func TestParseErrorFromApiResponse_UnexpectedShouldOutputHttpResponseAnyway(t *t
Message: "Couldn't parse Azure API Response into a friendly error - please see the original HTTP Response for more details (and file a bug so we can fix this!).",
Status: "Unknown",
}
actual, err := parseErrorFromApiResponse(input)
actual, err := parseErrorFromApiResponse(&input)
if err != nil {
t.Fatalf("parsing error from api response: %+v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions sdk/client/resourcemanager/poller_lro.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (p *longRunningOperationPoller) Poll(ctx context.Context) (result *pollers.
}

if result.Status == pollers.PollingStatusFailed {
lroError, parseError := parseErrorFromApiResponse(*result.HttpResponse.Response)
lroError, parseError := parseErrorFromApiResponse(result.HttpResponse.Response)
if parseError != nil {
return nil, parseError
}
Expand All @@ -202,7 +202,7 @@ func (p *longRunningOperationPoller) Poll(ctx context.Context) (result *pollers.
}

if result.Status == pollers.PollingStatusCancelled {
lroError, parseError := parseErrorFromApiResponse(*result.HttpResponse.Response)
lroError, parseError := parseErrorFromApiResponse(result.HttpResponse.Response)
if parseError != nil {
return nil, parseError
}
Expand Down
Loading