Skip to content

Commit

Permalink
[Core] extending parsing for storage (Azure#17579)
Browse files Browse the repository at this point in the history
* adding tests and extending parsing for storage

* using an if-else statement
  • Loading branch information
seankane-msft authored Apr 13, 2022
1 parent c9d31dc commit 917ea6d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
7 changes: 7 additions & 0 deletions sdk/azcore/internal/shared/response_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ func extractErrorCodeJSON(body []byte) string {
return ""
}
rawObj = unwrapped
} else if wrapped, ok := rawObj["odata.error"]; ok {
// check if this a wrapped odata error, i.e. { "odata.error": { ... } }
unwrapped, ok := wrapped.(map[string]any)
if !ok {
return ""
}
rawObj = unwrapped
}

// now check for the error code
Expand Down
28 changes: 28 additions & 0 deletions sdk/azcore/internal/shared/response_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,31 @@ ERROR CODE UNAVAILABLE
t.Fatalf("\ngot:\n%s\nwant:\n%s\n", got, want)
}
}

func TestExtractErrorCodeFromJSON(t *testing.T) {
errorBody := []byte(`{"odata.error": {
"code": "ResourceNotFound",
"message": {
"lang": "en-us",
"value": "The specified resource does not exist.\nRequestID:b2437f3b-ca2d-47a1-95a7-92f73a768a1c\n"
}
}
}`)
code := extractErrorCodeJSON(errorBody)
if code != "ResourceNotFound" {
t.Fatalf("expected %s got %s", "ResourceNotFound", code)
}

errorBody = []byte(`{"error": {
"code": "ResourceNotFound",
"message": {
"lang": "en-us",
"value": "The specified resource does not exist.\nRequestID:b2437f3b-ca2d-47a1-95a7-92f73a768a1c\n"
}
}
}`)
code = extractErrorCodeJSON(errorBody)
if code != "ResourceNotFound" {
t.Fatalf("expected %s got %s", "ResourceNotFound", code)
}
}

0 comments on commit 917ea6d

Please sign in to comment.