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

sdk/client/resourcemanager: parsing additional details from the azure error responses where possible #676

Merged
merged 1 commit into from
Oct 13, 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
10 changes: 6 additions & 4 deletions sdk/client/resourcemanager/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,14 @@ func parseErrorFromApiResponse(response http.Response) (*Error, error) {
var err3 resourceManagerErrorType2
if err = json.Unmarshal(respBody, &err3); err == nil && err3.Error.Code != "" && err3.Error.Message != "" {
activityId := ""
code := err3.Error.Code
topLevelCode := err3.Error.Code
messages := []string{
err3.Error.Message,
}
nestedCode := ""
for _, v := range err3.Error.Details {
code = v.Code
nestedCode = v.Code
messages = append(messages, v.Message)
if v.PossibleCauses != "" {
messages = append(messages, fmt.Sprintf("Possible Causes: %q", v.PossibleCauses))
}
Expand All @@ -127,9 +129,9 @@ func parseErrorFromApiResponse(response http.Response) (*Error, error) {
}
return &Error{
ActivityId: activityId,
Code: code,
Code: nestedCode,
Message: strings.Join(messages, "\n"),
Status: "Unknown",
Status: topLevelCode,
FullHttpBody: string(respBody),
}, nil
}
Expand Down
28 changes: 26 additions & 2 deletions sdk/client/resourcemanager/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,32 @@ func TestParseErrorFromApiResponse_ResourceManagerType2(t *testing.T) {
ActivityId: "abc123",
Code: "FailedToStartOperation",
FullHttpBody: body,
Message: "Failed to start operation. Verify input and try operation again.\nPossible Causes: \"Invalid parameters were specified.\"\nRecommended Action: \"Verify the input and try again.\"",
Status: "Unknown",
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)
if err != nil {
t.Fatalf("parsing error from api response: %+v", err)
}
if actual == nil {
t.Fatalf("`actual` was nil")
}
if !reflect.DeepEqual(*actual, expected) {
t.Fatalf("expected and actual didn't match. Expected: %+v\n\n Actual: %+v", expected, actual)
}
}

func TestParseErrorFromApiResponse_ResourceManagerType2Alt(t *testing.T) {
body := `{"error":{"code":"ValidationError","message":"The request is not valid.","details":[{"code":"InvalidDevCenterName","target":"Name","message":"Resource name is not valid. It must be between 3 and 26 characters and can only include alphanumeric characters and hyphens.","details":[],"additionalInfo":[]}]}}`
input := http.Response{
Body: io.NopCloser(bytes.NewReader([]byte(body))),
}
expected := Error{
ActivityId: "",
Code: "InvalidDevCenterName",
FullHttpBody: body,
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)
if err != nil {
Expand Down
Loading