-
Notifications
You must be signed in to change notification settings - Fork 36
/
unmarshal_error.go
52 lines (47 loc) · 1.26 KB
/
unmarshal_error.go
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
package kscquery
import (
"encoding/json"
"fmt"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/request"
"io/ioutil"
)
type xmlErrorResponse struct {
Error Error `json:"Error"`
RequestID string `json:"RequestID"`
}
type Error struct {
Code string `json:"Code"`
Message string `json:"Message"`
}
// UnmarshalErrorHandler is a name request handler to unmarshal request errors
var UnmarshalErrorHandler = request.NamedHandler{Name: "kscsdk.query.UnmarshalError", Fn: UnmarshalError}
// UnmarshalError unmarshals an error response for an AWS Query service.
func UnmarshalError(r *request.Request) {
defer r.HTTPResponse.Body.Close()
resp := xmlErrorResponse{}
if r.DataFilled() {
body, err := ioutil.ReadAll(r.HTTPResponse.Body)
if err != nil {
fmt.Printf("read body err, %v\n", err)
return
}
if err = json.Unmarshal(body,&resp); err != nil {
fmt.Printf("Unmarshal err, %v\n", err)
return
}
r.Error = awserr.NewRequestFailure(
awserr.New(resp.Error.Code, resp.Error.Message, nil),
r.HTTPResponse.StatusCode,
resp.RequestID,
)
return
}else {
r.Error = awserr.NewRequestFailure(
awserr.New("ServiceUnavailableException", "service is unavailable", nil),
r.HTTPResponse.StatusCode,
r.RequestID,
)
return
}
}