-
Notifications
You must be signed in to change notification settings - Fork 16
/
decoder.go
90 lines (75 loc) · 2.14 KB
/
decoder.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
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
88
89
90
package mq_http_sdk
import (
"bytes"
"encoding/xml"
"github.com/gogap/errors"
"io"
)
type MQDecoder interface {
Decode(reader io.Reader, v interface{}) (err error)
DecodeError(bodyBytes []byte, resource string, requestId string) (decodedError error, err error)
}
type AliyunMQDecoder struct {
}
type AliyunMQAckMsgDecoder struct {
v AckMessageErrorResponse
}
func NewAliyunMQDecoder() MQDecoder {
return &AliyunMQDecoder{}
}
func (p *AliyunMQDecoder) Decode(reader io.Reader, v interface{}) (err error) {
decoder := xml.NewDecoder(reader)
err = decoder.Decode(&v)
return
}
func (p *AliyunMQDecoder) DecodeError(bodyBytes []byte, resource string, requestId string) (decodedError error, err error) {
bodyReader := bytes.NewReader(bodyBytes)
errResp := ErrorResponse{}
decoder := xml.NewDecoder(bodyReader)
err = decoder.Decode(&errResp)
if err == nil {
decodedError = ParseError(errResp, resource)
}
return
}
func NewAliyunMQAckMsgDecoder() MQDecoder {
return &AliyunMQAckMsgDecoder{}
}
func (p *AliyunMQAckMsgDecoder) Decode(reader io.Reader, v interface{}) (err error) {
decoder := xml.NewDecoder(reader)
err = decoder.Decode(&v)
if err == io.EOF {
err = nil
}
return
}
func (p *AliyunMQAckMsgDecoder) DecodeError(bodyBytes []byte, resource string, requestId string) (decodedError error, err error) {
bodyReader := bytes.NewReader(bodyBytes)
decoder := xml.NewDecoder(bodyReader)
err = decoder.Decode(&p.v)
if err != nil {
// not receipt handle error
bodyReader.Seek(0, 0)
errResp := ErrorResponse{}
err = decoder.Decode(&errResp)
if err == nil {
decodedError = ParseError(errResp, resource)
}
} else {
var errorItems []ErrAckItem
for _, v := range p.v.FailedMessages {
errorItems = append(errorItems, ErrAckItem{
ErrorCode: v.ErrorCode,
ErrorHandle: v.ReceiptHandle,
ErrorMsg: v.ErrorMessage,
})
}
decodedError = ErrAckMessage.New(errors.Params{
"Code": p.v.FailedMessages[0].ErrorCode,
"Message": p.v.FailedMessages[0].ErrorMessage,
"ReceiptHandle": p.v.FailedMessages[0].ReceiptHandle,
"RequestId": requestId,
}).WithContext("Detail", errorItems)
}
return
}