File: unmarshall_error.go

package info (click to toggle)
golang-github-aws-aws-sdk-go 1.49.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 312,636 kB
  • sloc: makefile: 120
file content (87 lines) | stat: -rw-r--r-- 2,242 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package simpledb

import (
	"encoding/xml"
	"io"
	"io/ioutil"
	"strings"

	"github.com/aws/aws-sdk-go/aws/awserr"
	"github.com/aws/aws-sdk-go/aws/request"
	"github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil"
)

type xmlErrorDetail struct {
	Code    string `xml:"Code"`
	Message string `xml:"Message"`
}
type xmlErrorMessage struct {
	XMLName   xml.Name         `xml:"Response"`
	Errors    []xmlErrorDetail `xml:"Errors>Error"`
	RequestID string           `xml:"RequestID"`
}

type xmlErrorResponse struct {
	Code        string
	Message     string
	RequestID   string
	OtherErrors []xmlErrorDetail
}

func (r *xmlErrorResponse) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	var errResp xmlErrorMessage
	if err := d.DecodeElement(&errResp, &start); err != nil {
		return err
	}

	r.RequestID = errResp.RequestID
	if len(errResp.Errors) == 0 {
		r.Code = "MissingError"
		r.Message = "missing error code in SimpleDB XML error response"
	} else {
		r.Code = errResp.Errors[0].Code
		r.Message = errResp.Errors[0].Message
		r.OtherErrors = errResp.Errors[1:]
	}

	return nil
}

func unmarshalError(r *request.Request) {
	defer r.HTTPResponse.Body.Close()
	defer io.Copy(ioutil.Discard, r.HTTPResponse.Body)

	if r.HTTPResponse.ContentLength == int64(0) {
		// No body, use status code to generate an awserr.Error
		r.Error = awserr.NewRequestFailure(
			awserr.New(strings.Replace(r.HTTPResponse.Status, " ", "", -1), r.HTTPResponse.Status, nil),
			r.HTTPResponse.StatusCode,
			r.RequestID,
		)
		return
	}

	var errResp xmlErrorResponse
	err := xmlutil.UnmarshalXMLError(&errResp, r.HTTPResponse.Body)
	if err != nil {
		r.Error = awserr.NewRequestFailure(
			awserr.New(request.ErrCodeSerialization, "failed to unmarshal error message", err),
			r.HTTPResponse.StatusCode,
			r.RequestID,
		)
		return
	}

	var otherErrs []error
	for _, e := range errResp.OtherErrors {
		otherErrs = append(otherErrs, awserr.New(e.Code, e.Message, nil))
	}

	// If there are multiple error codes, return only the first as the
	// aws.Error interface only supports one error code.
	r.Error = awserr.NewRequestFailure(
		awserr.NewBatchError(errResp.Code, errResp.Message, otherErrs),
		r.HTTPResponse.StatusCode,
		errResp.RequestID,
	)
}