error_utils.go (1465B)
1 package xml 2 3 import ( 4 "encoding/xml" 5 "fmt" 6 "io" 7 ) 8 9 // ErrorComponents represents the error response fields 10 // that will be deserialized from an xml error response body 11 type ErrorComponents struct { 12 Code string 13 Message string 14 RequestID string 15 } 16 17 // GetErrorResponseComponents returns the error fields from an xml error response body 18 func GetErrorResponseComponents(r io.Reader, noErrorWrapping bool) (ErrorComponents, error) { 19 if noErrorWrapping { 20 var errResponse noWrappedErrorResponse 21 if err := xml.NewDecoder(r).Decode(&errResponse); err != nil && err != io.EOF { 22 return ErrorComponents{}, fmt.Errorf("error while deserializing xml error response: %w", err) 23 } 24 return ErrorComponents(errResponse), nil 25 } 26 27 var errResponse wrappedErrorResponse 28 if err := xml.NewDecoder(r).Decode(&errResponse); err != nil && err != io.EOF { 29 return ErrorComponents{}, fmt.Errorf("error while deserializing xml error response: %w", err) 30 } 31 return ErrorComponents(errResponse), nil 32 } 33 34 // noWrappedErrorResponse represents the error response body with 35 // no internal Error wrapping 36 type noWrappedErrorResponse struct { 37 Code string `xml:"Code"` 38 Message string `xml:"Message"` 39 RequestID string `xml:"RequestId"` 40 } 41 42 // wrappedErrorResponse represents the error response body 43 // wrapped within Error 44 type wrappedErrorResponse struct { 45 Code string `xml:"Error>Code"` 46 Message string `xml:"Error>Message"` 47 RequestID string `xml:"RequestId"` 48 }