Skip to content

Commit 917ea6d

Browse files
[Core] extending parsing for storage (Azure#17579)
* adding tests and extending parsing for storage * using an if-else statement
1 parent c9d31dc commit 917ea6d

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

sdk/azcore/internal/shared/response_error.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ func extractErrorCodeJSON(body []byte) string {
5959
return ""
6060
}
6161
rawObj = unwrapped
62+
} else if wrapped, ok := rawObj["odata.error"]; ok {
63+
// check if this a wrapped odata error, i.e. { "odata.error": { ... } }
64+
unwrapped, ok := wrapped.(map[string]any)
65+
if !ok {
66+
return ""
67+
}
68+
rawObj = unwrapped
6269
}
6370

6471
// now check for the error code

sdk/azcore/internal/shared/response_error_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,3 +420,31 @@ ERROR CODE UNAVAILABLE
420420
t.Fatalf("\ngot:\n%s\nwant:\n%s\n", got, want)
421421
}
422422
}
423+
424+
func TestExtractErrorCodeFromJSON(t *testing.T) {
425+
errorBody := []byte(`{"odata.error": {
426+
"code": "ResourceNotFound",
427+
"message": {
428+
"lang": "en-us",
429+
"value": "The specified resource does not exist.\nRequestID:b2437f3b-ca2d-47a1-95a7-92f73a768a1c\n"
430+
}
431+
}
432+
}`)
433+
code := extractErrorCodeJSON(errorBody)
434+
if code != "ResourceNotFound" {
435+
t.Fatalf("expected %s got %s", "ResourceNotFound", code)
436+
}
437+
438+
errorBody = []byte(`{"error": {
439+
"code": "ResourceNotFound",
440+
"message": {
441+
"lang": "en-us",
442+
"value": "The specified resource does not exist.\nRequestID:b2437f3b-ca2d-47a1-95a7-92f73a768a1c\n"
443+
}
444+
}
445+
}`)
446+
code = extractErrorCodeJSON(errorBody)
447+
if code != "ResourceNotFound" {
448+
t.Fatalf("expected %s got %s", "ResourceNotFound", code)
449+
}
450+
}

0 commit comments

Comments
 (0)