unmarshall_error.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package simpledb
  2. import (
  3. "encoding/xml"
  4. "io"
  5. "io/ioutil"
  6. "strings"
  7. "github.com/aws/aws-sdk-go/aws/awserr"
  8. "github.com/aws/aws-sdk-go/aws/request"
  9. )
  10. type xmlErrorDetail struct {
  11. Code string `xml:"Code"`
  12. Message string `xml:"Message"`
  13. }
  14. type xmlErrorResponse struct {
  15. XMLName xml.Name `xml:"Response"`
  16. Errors []xmlErrorDetail `xml:"Errors>Error"`
  17. RequestID string `xml:"RequestID"`
  18. }
  19. func unmarshalError(r *request.Request) {
  20. defer r.HTTPResponse.Body.Close()
  21. defer io.Copy(ioutil.Discard, r.HTTPResponse.Body)
  22. if r.HTTPResponse.ContentLength == int64(0) {
  23. // No body, use status code to generate an awserr.Error
  24. r.Error = awserr.NewRequestFailure(
  25. awserr.New(strings.Replace(r.HTTPResponse.Status, " ", "", -1), r.HTTPResponse.Status, nil),
  26. r.HTTPResponse.StatusCode,
  27. "",
  28. )
  29. return
  30. }
  31. resp := &xmlErrorResponse{}
  32. err := xml.NewDecoder(r.HTTPResponse.Body).Decode(resp)
  33. if err != nil && err != io.EOF {
  34. r.Error = awserr.New("SerializationError", "failed to decode SimpleDB XML error response", nil)
  35. } else if len(resp.Errors) == 0 {
  36. r.Error = awserr.New("MissingError", "missing error code in SimpleDB XML error response", nil)
  37. } else {
  38. // If there are multiple error codes, return only the first as the aws.Error interface only supports
  39. // one error code.
  40. r.Error = awserr.NewRequestFailure(
  41. awserr.New(resp.Errors[0].Code, resp.Errors[0].Message, nil),
  42. r.HTTPResponse.StatusCode,
  43. resp.RequestID,
  44. )
  45. }
  46. }