Skip to content

Commit 12209c4

Browse files
author
tomoya-yokota
committed
Add error_reason response
1 parent 9a765e7 commit 12209c4

File tree

5 files changed

+52
-36
lines changed

5 files changed

+52
-36
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,8 @@ See the following error format.
986986
"platform": "android",
987987
"token": "*******",
988988
"message": "Hello World Android!",
989-
"error": "InvalidRegistration"
989+
"error":"invalid registration token",
990+
"error_reason":"InvalidRegistration"
990991
},
991992
{
992993
"type": "failed-push",

logx/log.go

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ var (
2323

2424
// LogPushEntry is push response log
2525
type LogPushEntry struct {
26-
ID string `json:"notif_id,omitempty"`
27-
Type string `json:"type"`
28-
Platform string `json:"platform"`
29-
Token string `json:"token"`
30-
Message string `json:"message"`
31-
Error string `json:"error"`
26+
ID string `json:"notif_id,omitempty"`
27+
Type string `json:"type"`
28+
Platform string `json:"platform"`
29+
Token string `json:"token"`
30+
Message string `json:"message"`
31+
Error string `json:"error"`
32+
ErrorReason string `json:"error_reason,omitempty"`
3233
}
3334

3435
var isTerm bool
@@ -175,25 +176,27 @@ func GetLogPushEntry(input *InputLog) LogPushEntry {
175176
}
176177

177178
return LogPushEntry{
178-
ID: input.ID,
179-
Type: input.Status,
180-
Platform: plat,
181-
Token: token,
182-
Message: input.Message,
183-
Error: errMsg,
179+
ID: input.ID,
180+
Type: input.Status,
181+
Platform: plat,
182+
Token: token,
183+
Message: input.Message,
184+
Error: errMsg,
185+
ErrorReason: input.ErrorReason,
184186
}
185187
}
186188

187189
// InputLog log request
188190
type InputLog struct {
189-
ID string
190-
Status string
191-
Token string
192-
Message string
193-
Platform int
194-
Error error
195-
HideToken bool
196-
Format string
191+
ID string
192+
Status string
193+
Token string
194+
Message string
195+
Platform int
196+
Error error
197+
ErrorReason string
198+
HideToken bool
199+
Format string
197200
}
198201

199202
// LogPush record user push request and server response.

notify/notification_apns.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"time"
1313

1414
"github.com/appleboy/gorush/config"
15-
"github.com/appleboy/gorush/core"
1615
"github.com/appleboy/gorush/logx"
1716
"github.com/appleboy/gorush/status"
1817

@@ -427,7 +426,7 @@ Retry:
427426
}
428427

429428
// apns server error
430-
errLog := logPush(cfg, core.FailedPush, token, req, err)
429+
errLog := logErrorPush(cfg, token, req, err, res.Reason)
431430
resp.Logs = append(resp.Logs, errLog)
432431

433432
status.StatStorage.AddIosError(1)
@@ -439,7 +438,7 @@ Retry:
439438
}
440439

441440
if res != nil && res.Sent() {
442-
logPush(cfg, core.SucceededPush, token, req, nil)
441+
logErrorPush(cfg, token, req, nil, res.Reason)
443442
status.StatStorage.AddIosSuccess(1)
444443
}
445444

notify/notification_fcm.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,12 @@ Retry:
148148
logx.LogError.Error("FCM server send message error: " + err.Error())
149149

150150
if req.IsTopic() {
151-
errLog := logPush(cfg, core.FailedPush, req.To, req, err)
151+
errLog := logErrorPush(cfg, req.To, req, err, "")
152152
resp.Logs = append(resp.Logs, errLog)
153153
status.StatStorage.AddAndroidError(1)
154154
} else {
155155
for _, token := range req.Tokens {
156-
errLog := logPush(cfg, core.FailedPush, token, req, err)
156+
errLog := logErrorPush(cfg, token, req, err, "")
157157
resp.Logs = append(resp.Logs, errLog)
158158
}
159159
status.StatStorage.AddAndroidError(int64(len(req.Tokens)))
@@ -185,12 +185,12 @@ Retry:
185185
newTokens = append(newTokens, to)
186186
}
187187

188-
errLog := logPush(cfg, core.FailedPush, to, req, result.Error)
188+
errLog := logErrorPush(cfg, to, req, result.Error, result.ErrorResponseCode)
189189
resp.Logs = append(resp.Logs, errLog)
190190
continue
191191
}
192192

193-
logPush(cfg, core.SucceededPush, to, req, nil)
193+
logSuccessPush(cfg, to, req)
194194
}
195195

196196
// result from Send messages to topics
@@ -204,10 +204,10 @@ Retry:
204204
logx.LogAccess.Debug("Send Topic Message: ", to)
205205
// Success
206206
if res.MessageID != 0 {
207-
logPush(cfg, core.SucceededPush, to, req, nil)
207+
logSuccessPush(cfg, to, req)
208208
} else {
209209
// failure
210-
errLog := logPush(cfg, core.FailedPush, to, req, res.Error)
210+
errLog := logErrorPush(cfg, to, req, res.Error, res.ErrorResponseCode)
211211
resp.Logs = append(resp.Logs, errLog)
212212
}
213213
}
@@ -217,7 +217,7 @@ Retry:
217217
newTokens = append(newTokens, res.FailedRegistrationIDs...)
218218

219219
// nolint
220-
errLog := logPush(cfg, core.FailedPush, notification.To, req, errors.New("device group: partial success or all fails"))
220+
errLog := logErrorPush(cfg, notification.To, req, errors.New("device group: partial success or all fails"), res.ErrorResponseCode)
221221
resp.Logs = append(resp.Logs, errLog)
222222
}
223223

@@ -232,14 +232,28 @@ Retry:
232232
return resp, nil
233233
}
234234

235-
func logPush(cfg *config.ConfYaml, status, token string, req *PushNotification, err error) logx.LogPushEntry {
235+
func logErrorPush(cfg *config.ConfYaml, token string, req *PushNotification, err error, errorReason string) logx.LogPushEntry {
236+
return logx.LogPush(&logx.InputLog{
237+
ID: req.ID,
238+
Status: core.FailedPush,
239+
Token: token,
240+
Message: req.Message,
241+
Platform: req.Platform,
242+
Error: err,
243+
ErrorReason: errorReason,
244+
HideToken: cfg.Log.HideToken,
245+
Format: cfg.Log.Format,
246+
})
247+
}
248+
249+
func logSuccessPush(cfg *config.ConfYaml, token string, req *PushNotification) logx.LogPushEntry {
236250
return logx.LogPush(&logx.InputLog{
237251
ID: req.ID,
238-
Status: status,
252+
Status: core.SucceededPush,
239253
Token: token,
240254
Message: req.Message,
241255
Platform: req.Platform,
242-
Error: err,
256+
Error: nil,
243257
HideToken: cfg.Log.HideToken,
244258
Format: cfg.Log.Format,
245259
})

notify/notification_hms.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"sync"
77

88
"github.com/appleboy/gorush/config"
9-
"github.com/appleboy/gorush/core"
109
"github.com/appleboy/gorush/logx"
1110
"github.com/appleboy/gorush/status"
1211
c "github.com/msalihkarakasli/go-hms-push/push/config"
@@ -199,7 +198,7 @@ Retry:
199198
res, err := client.SendMessage(context.Background(), notification)
200199
if err != nil {
201200
// Send Message error
202-
errLog := logPush(cfg, core.FailedPush, req.To, req, err)
201+
errLog := logErrorPush(cfg, req.To, req, err, "")
203202
resp.Logs = append(resp.Logs, errLog)
204203
logx.LogError.Error("HMS server send message error: " + err.Error())
205204
return

0 commit comments

Comments
 (0)