-
Notifications
You must be signed in to change notification settings - Fork 7
/
error.go
133 lines (111 loc) · 3.79 KB
/
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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package alimns
import (
"encoding/xml"
"github.com/xiaojiaoyu100/cast"
)
// MnsError 错误码
type MnsError string
func (e MnsError) Error() string {
return string(e)
}
// customize code
const (
unknownError = MnsError("Unknown")
visibilityTimeoutError = MnsError("VisibilityTimeoutOutOfRange")
createQueueNoContentError = MnsError("CreateQueueNoContent")
createQueueConflictError = MnsError("CreateQueueConflict")
messageBodyLimitError = MnsError("MessageBodyLimit")
sendMessageTimeoutError = MnsError("SendMessageTimeout")
messageDelaySecondsOutOfRangeError = MnsError("MessageDelaySecondsOutOfRange")
batchSendMessageTryLimitError = MnsError("BatchSendMessageTryLimit")
batchSendMessageNumLimitError = MnsError("BatchSendMessageNumLimit")
handleCrashError = MnsError("handleCrash")
queueNumExceededLimitError = MnsError("QueueNumExceededLimit")
subscriptionNameLengthError = MnsError("SubscriptionNameLengthError")
subscriptionNameInvalidError = MnsError("SubscriptionNameInvalid")
subscriptionAlreadyExistError = MnsError("SubscriptionAlreadyExist")
endpointInvalidError = MnsError("EndpointInvalid")
invalidArgumentError = MnsError("InvalidArgument")
topicAlreadyExistError = MnsError("TopicAlreadyExist")
topicNameLengthError = MnsError("TopicNameLengthError")
topicNotExistError = MnsError("TopicNotExist")
)
// aliyun mns error code
const (
messageNotExistError = MnsError("MessageNotExist")
queueNotExistError = MnsError("QueueNotExist")
internalError = MnsError("InternalError")
)
// IsUnknown 是否是未知错误
func IsUnknown(err error) bool {
return err == unknownError
}
// IsVisibilityTimeout 是否是可见时间不在范围
func IsVisibilityTimeout(err error) bool {
return err == visibilityTimeoutError
}
// IsCreateQueueNoContent 是否是createQueueNoContentError
func IsCreateQueueNoContent(err error) bool {
return err == createQueueNoContentError
}
// IsCreateQueueConflict 是否是createQueueConflictError
func IsCreateQueueConflict(err error) bool {
return err == createQueueConflictError
}
// IsMessageBodyLimit 是否超出范围
func IsMessageBodyLimit(err error) bool {
return err == messageBodyLimitError
}
// IsSendMessageTimeout 是否发送消息超时
func IsSendMessageTimeout(err error) bool {
return err == sendMessageTimeoutError
}
// IsMessageDelaySecondsOutOfRange 延时时长是否合理
func IsMessageDelaySecondsOutOfRange(err error) bool {
return err == messageDelaySecondsOutOfRangeError
}
// IsHandleCrash 是否是处理函数崩溃错误
func IsHandleCrash(err error) bool {
return err == handleCrashError
}
// IsInternalError 是否内部错误
func IsInternalError(err error) bool {
return err == internalError
}
// RespErr 阿里云回复错误
type RespErr struct {
XMLName xml.Name `xml:"Error"`
XMLNs string `xml:"xmlns,attr"`
Code string `xml:"Code"`
Message string `xml:"Message"`
RequestID string `xml:"RequestId"`
HostID string `xml:"HostId"`
}
func shouldRetry(err error) bool {
return IsInternalError(err) || cast.ShouldRetry(err)
}
// Backoff backoff
type Backoff interface {
Backoff() int
}
// BackoffError represents the backoff error.
type BackoffError struct {
Err error
// visibility timeout
N int
}
// Backoff returns the message visibility time.
func (err BackoffError) Backoff() int {
if err.N < minVisibilityTimeout || err.N > maxVisibilityTimeout {
return minVisibilityTimeout
}
return err.N
}
// Unwrap get the underlying error.
func (err BackoffError) Unwrap() error {
return err.Err
}
// Error implements the error interface.
func (err BackoffError) Error() string {
return err.Err.Error()
}