From d710c4820679c731afc2d1be15e4f490416c9108 Mon Sep 17 00:00:00 2001 From: John Stairs Date: Mon, 13 Dec 2021 21:31:52 +0000 Subject: [PATCH] Handle error response XML with whitespace --- sdk/storage/azblob/zc_storage_error.go | 7 +++- sdk/storage/azblob/zc_storage_error_test.go | 36 +++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 sdk/storage/azblob/zc_storage_error_test.go diff --git a/sdk/storage/azblob/zc_storage_error.go b/sdk/storage/azblob/zc_storage_error.go index 58dc7d214353..329f680ed3c0 100644 --- a/sdk/storage/azblob/zc_storage_error.go +++ b/sdk/storage/azblob/zc_storage_error.go @@ -8,10 +8,11 @@ import ( "encoding/xml" "errors" "fmt" - "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" "net/http" "sort" "strings" + + "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" ) // InternalError is an internal error type that all errors get wrapped in. @@ -193,8 +194,12 @@ func (e *StorageError) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err switch tt := t.(type) { case xml.StartElement: tokName = tt.Name.Local + case xml.EndElement: + tokName = "" case xml.CharData: switch tokName { + case "": + continue case "Message": e.description = string(tt) default: diff --git a/sdk/storage/azblob/zc_storage_error_test.go b/sdk/storage/azblob/zc_storage_error_test.go new file mode 100644 index 000000000000..03d6c6784f9c --- /dev/null +++ b/sdk/storage/azblob/zc_storage_error_test.go @@ -0,0 +1,36 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package azblob + +import ( + "encoding/xml" + "testing" + + "github.com/stretchr/testify/assert" +) + +func (s *azblobUnrecordedTestSuite) TestErrorResponseUnmarshal() { + t := s.T() + + cases := []struct { + name string + input string + }{ + {"singleline", "ContainerAlreadyExistsThe specified container already exists.\nRequestId:73b2473b-c1c8-4162-97bb-dc171bff61c9\nTime:2021-12-13T19:45:40.679Z"}, + {"multiline", "\n\n ContainerAlreadyExists\n The specified container already exists.\nRequestId:73b2473b-c1c8-4162-97bb-dc171bff61c9\nTime:2021-12-13T19:45:40.679Z\n"}, + } + + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + _assert := assert.New(t) + se := StorageError{} + _assert.Nil(xml.Unmarshal([]byte(c.input), &se)) + + _assert.Contains(se.details, "Code") + _assert.Equal("ContainerAlreadyExists", se.details["Code"]) + + _assert.Equal("The specified container already exists.\nRequestId:73b2473b-c1c8-4162-97bb-dc171bff61c9\nTime:2021-12-13T19:45:40.679Z", se.description) + }) + } +}