diff options
Diffstat (limited to 'vendor/github.com/aws/aws-sdk-go/private/protocol/query/unmarshal_error.go')
-rw-r--r-- | vendor/github.com/aws/aws-sdk-go/private/protocol/query/unmarshal_error.go | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/vendor/github.com/aws/aws-sdk-go/private/protocol/query/unmarshal_error.go b/vendor/github.com/aws/aws-sdk-go/private/protocol/query/unmarshal_error.go new file mode 100644 index 0000000..f214296 --- /dev/null +++ b/vendor/github.com/aws/aws-sdk-go/private/protocol/query/unmarshal_error.go | |||
@@ -0,0 +1,66 @@ | |||
1 | package query | ||
2 | |||
3 | import ( | ||
4 | "encoding/xml" | ||
5 | "io/ioutil" | ||
6 | |||
7 | "github.com/aws/aws-sdk-go/aws/awserr" | ||
8 | "github.com/aws/aws-sdk-go/aws/request" | ||
9 | ) | ||
10 | |||
11 | type xmlErrorResponse struct { | ||
12 | XMLName xml.Name `xml:"ErrorResponse"` | ||
13 | Code string `xml:"Error>Code"` | ||
14 | Message string `xml:"Error>Message"` | ||
15 | RequestID string `xml:"RequestId"` | ||
16 | } | ||
17 | |||
18 | type xmlServiceUnavailableResponse struct { | ||
19 | XMLName xml.Name `xml:"ServiceUnavailableException"` | ||
20 | } | ||
21 | |||
22 | // UnmarshalErrorHandler is a name request handler to unmarshal request errors | ||
23 | var UnmarshalErrorHandler = request.NamedHandler{Name: "awssdk.query.UnmarshalError", Fn: UnmarshalError} | ||
24 | |||
25 | // UnmarshalError unmarshals an error response for an AWS Query service. | ||
26 | func UnmarshalError(r *request.Request) { | ||
27 | defer r.HTTPResponse.Body.Close() | ||
28 | |||
29 | bodyBytes, err := ioutil.ReadAll(r.HTTPResponse.Body) | ||
30 | if err != nil { | ||
31 | r.Error = awserr.New("SerializationError", "failed to read from query HTTP response body", err) | ||
32 | return | ||
33 | } | ||
34 | |||
35 | // First check for specific error | ||
36 | resp := xmlErrorResponse{} | ||
37 | decodeErr := xml.Unmarshal(bodyBytes, &resp) | ||
38 | if decodeErr == nil { | ||
39 | reqID := resp.RequestID | ||
40 | if reqID == "" { | ||
41 | reqID = r.RequestID | ||
42 | } | ||
43 | r.Error = awserr.NewRequestFailure( | ||
44 | awserr.New(resp.Code, resp.Message, nil), | ||
45 | r.HTTPResponse.StatusCode, | ||
46 | reqID, | ||
47 | ) | ||
48 | return | ||
49 | } | ||
50 | |||
51 | // Check for unhandled error | ||
52 | servUnavailResp := xmlServiceUnavailableResponse{} | ||
53 | unavailErr := xml.Unmarshal(bodyBytes, &servUnavailResp) | ||
54 | if unavailErr == nil { | ||
55 | r.Error = awserr.NewRequestFailure( | ||
56 | awserr.New("ServiceUnavailableException", "service is unavailable", nil), | ||
57 | r.HTTPResponse.StatusCode, | ||
58 | r.RequestID, | ||
59 | ) | ||
60 | return | ||
61 | } | ||
62 | |||
63 | // Failed to retrieve any error message from the response body | ||
64 | r.Error = awserr.New("SerializationError", | ||
65 | "failed to decode query XML error response", decodeErr) | ||
66 | } | ||