From 917ea6d8d5d24a6fe2c875a8164d1405bdc47fb1 Mon Sep 17 00:00:00 2001 From: Sean Kane <68240067+seankane-msft@users.noreply.github.com> Date: Wed, 13 Apr 2022 17:34:07 -0400 Subject: [PATCH] [Core] extending parsing for storage (#17579) * adding tests and extending parsing for storage * using an if-else statement --- sdk/azcore/internal/shared/response_error.go | 7 +++++ .../internal/shared/response_error_test.go | 28 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/sdk/azcore/internal/shared/response_error.go b/sdk/azcore/internal/shared/response_error.go index 02d5f38df384..419e08f06ad9 100644 --- a/sdk/azcore/internal/shared/response_error.go +++ b/sdk/azcore/internal/shared/response_error.go @@ -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 diff --git a/sdk/azcore/internal/shared/response_error_test.go b/sdk/azcore/internal/shared/response_error_test.go index 180fa44f1f2c..9cd9b2525ce2 100644 --- a/sdk/azcore/internal/shared/response_error_test.go +++ b/sdk/azcore/internal/shared/response_error_test.go @@ -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) + } +}