forked from rb-go/go-fcm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
executable file
·156 lines (122 loc) · 4.53 KB
/
response.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
package fcm
import (
"errors"
)
var (
// ErrMissingRegistration occurs if registration token is not set.
ErrMissingRegistration = errors.New("missing registration token")
// ErrInvalidRegistration occurs if registration token is invalid.
ErrInvalidRegistration = errors.New("invalid registration token")
// ErrNotRegistered occurs when application was deleted from device and
// token is not registered in FCM.
ErrNotRegistered = errors.New("unregistered device")
// ErrInvalidPackageName occurs if package name in message is invalid.
ErrInvalidPackageName = errors.New("invalid package name")
// ErrMismatchSenderID occurs when application has a new registration token.
ErrMismatchSenderID = errors.New("mismatched sender id")
// ErrMessageTooBig occurs when message is too big.
ErrMessageTooBig = errors.New("message is too big")
// ErrInvalidDataKey occurs if data key is invalid.
ErrInvalidDataKey = errors.New("invalid data key")
// ErrInvalidTTL occurs when message has invalid TTL.
ErrInvalidTTL = errors.New("invalid time to live")
// ErrUnavailable occurs when FCM service is unavailable. It makes sense
// to retry after this error.
ErrUnavailable = connectionError("timeout")
// ErrInternalServerError is internal FCM error. It makes sense to retry
// after this error.
ErrInternalServerError = serverError("internal server error")
// ErrDeviceMessageRateExceeded occurs when client sent to many requests to
// the device.
ErrDeviceMessageRateExceeded = errors.New("device message rate exceeded")
// ErrTopicsMessageRateExceeded occurs when client sent to many requests to
// the topics.
ErrTopicsMessageRateExceeded = errors.New("topics message rate exceeded")
// ErrInvalidParameters occurs when provided parameters have the right name and type
ErrInvalidParameters = errors.New("check that the provided parameters have the right name and type")
// ErrUnknown for unknown error type
ErrUnknown = errors.New("unknown error type")
)
var (
errMap = map[string]error{
"MissingRegistration": ErrMissingRegistration,
"InvalidRegistration": ErrInvalidRegistration,
"NotRegistered": ErrNotRegistered,
"InvalidPackageName": ErrInvalidPackageName,
"MismatchSenderId": ErrMismatchSenderID,
"MessageTooBig": ErrMessageTooBig,
"InvalidDataKey": ErrInvalidDataKey,
"InvalidTtl": ErrInvalidTTL,
"Unavailable": ErrUnavailable,
"InternalServerError": ErrInternalServerError,
"DeviceMessageRateExceeded": ErrDeviceMessageRateExceeded,
"TopicsMessageRateExceeded": ErrTopicsMessageRateExceeded,
"InvalidParameters": ErrInvalidParameters,
}
)
// connectionError represents connection errors such as timeout error, etc.
// Implements `net.Error` interface.
type connectionError string
func (err connectionError) Error() string {
return string(err)
}
func (err connectionError) Temporary() bool {
return true
}
func (err connectionError) Timeout() bool {
return true
}
// serverError represents internal server errors.
// Implements `net.Error` interface.
type serverError string
func (err serverError) Error() string {
return string(err)
}
func (serverError) Temporary() bool {
return true
}
func (serverError) Timeout() bool {
return false
}
// Response represents the FCM server's response to the application
// server's sent message.
type Response struct {
MulticastID int64 `json:"multicast_id"`
Success int `json:"success"`
Failure int `json:"failure"`
CanonicalIDs int `json:"canonical_ids"`
Results []Result `json:"results"`
// Device Group HTTP Response
FailedRegistrationIDs []string `json:"failed_registration_ids"`
// Topic HTTP response
MessageID int64 `json:"message_id"`
Error string `json:"error"`
}
// Result represents the status of a processed message.
type Result struct {
MessageID string `json:"message_id"`
RegistrationID string `json:"registration_id"`
Error string `json:"error"`
}
func GetErrorByString(errString string) error {
if val, ok := errMap[errString]; ok {
return val
}
return ErrUnknown
}
func IsUnregisteredErrorByError(err error) bool {
switch err {
case ErrNotRegistered, ErrMismatchSenderID, ErrMissingRegistration, ErrInvalidRegistration:
return true
default:
return false
}
}
func IsUnregisteredErrorByErrorString(errString string) bool {
switch GetErrorByString(errString) {
case ErrNotRegistered, ErrMismatchSenderID, ErrMissingRegistration, ErrInvalidRegistration:
return true
default:
return false
}
}