This repository has been archived by the owner on Jun 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy patherrors.go
167 lines (157 loc) · 4.94 KB
/
errors.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package push
import (
"errors"
"fmt"
"time"
)
// Error responses from Apple
type Error struct {
Reason error
Status int // http StatusCode
Timestamp time.Time
}
// Service error responses.
var (
// These could be checked prior to sending the request to Apple.
ErrPayloadEmpty = errors.New("PayloadEmpty")
ErrPayloadTooLarge = errors.New("PayloadTooLarge")
// Device token errors.
ErrMissingDeviceToken = errors.New("MissingDeviceToken")
ErrBadDeviceToken = errors.New("BadDeviceToken")
ErrTooManyRequests = errors.New("TooManyRequests")
// Header errors.
ErrBadMessageID = errors.New("BadMessageID")
ErrBadExpirationDate = errors.New("BadExpirationDate")
ErrBadPriority = errors.New("BadPriority")
ErrBadTopic = errors.New("BadTopic")
ErrInvalidPushType = errors.New("InvalidPushType")
// Certificate and topic errors.
ErrBadCertificate = errors.New("BadCertificate")
ErrBadCertificateEnvironment = errors.New("BadCertificateEnvironment")
ErrForbidden = errors.New("Forbidden")
ErrMissingTopic = errors.New("MissingTopic")
ErrTopicDisallowed = errors.New("TopicDisallowed")
ErrUnregistered = errors.New("Unregistered")
ErrDeviceTokenNotForTopic = errors.New("DeviceTokenNotForTopic")
// These errors should never happen when using Push.
ErrDuplicateHeaders = errors.New("DuplicateHeaders")
ErrBadPath = errors.New("BadPath")
ErrMethodNotAllowed = errors.New("MethodNotAllowed")
// Fatal server errors.
ErrIdleTimeout = errors.New("IdleTimeout")
ErrShutdown = errors.New("Shutdown")
ErrInternalServerError = errors.New("InternalServerError")
ErrServiceUnavailable = errors.New("ServiceUnavailable")
)
// mapErrorReason converts Apple error responses into exported Err variables
// for comparisons.
func mapErrorReason(reason string) error {
var e error
switch reason {
case "PayloadEmpty":
e = ErrPayloadEmpty
case "PayloadTooLarge":
e = ErrPayloadTooLarge
case "BadTopic":
e = ErrBadTopic
case "TopicDisallowed":
e = ErrTopicDisallowed
case "BadMessageId":
e = ErrBadMessageID
case "BadExpirationDate":
e = ErrBadExpirationDate
case "BadPriority":
e = ErrBadPriority
case "MissingDeviceToken":
e = ErrMissingDeviceToken
case "BadDeviceToken":
e = ErrBadDeviceToken
case "DeviceTokenNotForTopic":
e = ErrDeviceTokenNotForTopic
case "Unregistered":
e = ErrUnregistered
case "DuplicateHeaders":
e = ErrDuplicateHeaders
case "BadCertificateEnvironment":
e = ErrBadCertificateEnvironment
case "BadCertificate":
e = ErrBadCertificate
case "Forbidden":
e = ErrForbidden
case "BadPath":
e = ErrBadPath
case "MethodNotAllowed":
e = ErrMethodNotAllowed
case "TooManyRequests":
e = ErrTooManyRequests
case "IdleTimeout":
e = ErrIdleTimeout
case "Shutdown":
e = ErrShutdown
case "InternalServerError":
e = ErrInternalServerError
case "ServiceUnavailable":
e = ErrServiceUnavailable
case "MissingTopic":
e = ErrMissingTopic
case "InvalidPushType":
e = ErrInvalidPushType
default:
e = errors.New(reason)
}
return e
}
func (e *Error) Error() string {
switch e.Reason {
case ErrPayloadEmpty:
return "the message payload was empty"
case ErrPayloadTooLarge:
return "the message payload was too large"
case ErrMissingDeviceToken:
return "device token was not specified"
case ErrBadDeviceToken:
return "bad device token"
case ErrTooManyRequests:
return "too many requests were made consecutively to the same device token"
case ErrBadMessageID:
return "the ID header value is bad"
case ErrBadExpirationDate:
return "the Expiration header value is bad"
case ErrBadPriority:
return "the apns-priority value is bad"
case ErrBadTopic:
return "the Topic header was invalid"
case ErrBadCertificate:
return "the certificate was bad"
case ErrBadCertificateEnvironment:
return "certificate was for the wrong environment"
case ErrForbidden:
return "there was an error with the certificate"
case ErrMissingTopic:
return "the Topic header of the request was not specified and was required"
case ErrInvalidPushType:
return "the apns-push-type value is invalid"
case ErrTopicDisallowed:
return "pushing to this topic is not allowed"
case ErrUnregistered:
return fmt.Sprintf("device token is inactive for the specified topic (last invalid at %v)", e.Timestamp)
case ErrDeviceTokenNotForTopic:
return "device token does not match the specified topic"
case ErrDuplicateHeaders:
return "one or more headers were repeated"
case ErrBadPath:
return "the request contained a bad :path"
case ErrMethodNotAllowed:
return "the specified :method was not POST"
case ErrIdleTimeout:
return "idle time out"
case ErrShutdown:
return "the server is shutting down"
case ErrInternalServerError:
return "an internal server error occurred"
case ErrServiceUnavailable:
return "the service is unavailable"
default:
return fmt.Sprintf("unknown error: %v", e.Reason.Error())
}
}