Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log and return ErrorResponseCode #517

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,8 @@ See the following error format.
"platform": "android",
"token": "*******",
"message": "Hello World Android!",
"error": "InvalidRegistration"
"error":"invalid registration token",
"error_reason":"InvalidRegistration"
},
{
"type": "failed-push",
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.15
require (
github.com/apex/gateway v1.1.2
github.com/appleboy/gin-status-api v1.1.0
github.com/appleboy/go-fcm v0.1.5
github.com/appleboy/go-fcm v0.1.6-0.20210717010109-0181be2279cd
github.com/appleboy/gofight/v2 v2.1.2
github.com/asdine/storm/v3 v3.2.1
github.com/buger/jsonparser v1.1.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ github.com/appleboy/gin-status-api v1.1.0 h1:zoXePlNxk/Aa3Jmh8TI2xX0KTF8iET/QwOM
github.com/appleboy/gin-status-api v1.1.0/go.mod h1:qUmpFERWhlzRX4Hx+fEznIio8gXAXEDpEnb0Ald1d+g=
github.com/appleboy/go-fcm v0.1.5 h1:fKbcZf/7vwGsvDkcop8a+kCHnK+tt4wXX0X7uEzwI6E=
github.com/appleboy/go-fcm v0.1.5/go.mod h1:MSxZ4LqGRsnywOjnlXJXMqbjZrG4vf+0oHitfC9HRH0=
github.com/appleboy/go-fcm v0.1.6-0.20210717010109-0181be2279cd h1:M/LhFbSWAwLmdhjOVBik1Ekj6wAgxS3H3XtV5czaxE4=
github.com/appleboy/go-fcm v0.1.6-0.20210717010109-0181be2279cd/go.mod h1:MSxZ4LqGRsnywOjnlXJXMqbjZrG4vf+0oHitfC9HRH0=
github.com/appleboy/gofight/v2 v2.1.2 h1:VOy3jow4vIK8BRQJoC/I9muxyYlJ2yb9ht2hZoS3rf4=
github.com/appleboy/gofight/v2 v2.1.2/go.mod h1:frW+U1QZEdDgixycTj4CygQ48yLTUhplt43+Wczp3rw=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
Expand Down
43 changes: 23 additions & 20 deletions logx/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ var (

// LogPushEntry is push response log
type LogPushEntry struct {
ID string `json:"notif_id,omitempty"`
Type string `json:"type"`
Platform string `json:"platform"`
Token string `json:"token"`
Message string `json:"message"`
Error string `json:"error"`
ID string `json:"notif_id,omitempty"`
Type string `json:"type"`
Platform string `json:"platform"`
Token string `json:"token"`
Message string `json:"message"`
Error string `json:"error"`
ErrorReason string `json:"error_reason,omitempty"`
}

var isTerm bool
Expand Down Expand Up @@ -175,25 +176,27 @@ func GetLogPushEntry(input *InputLog) LogPushEntry {
}

return LogPushEntry{
ID: input.ID,
Type: input.Status,
Platform: plat,
Token: token,
Message: input.Message,
Error: errMsg,
ID: input.ID,
Type: input.Status,
Platform: plat,
Token: token,
Message: input.Message,
Error: errMsg,
ErrorReason: input.ErrorReason,
}
}

// InputLog log request
type InputLog struct {
ID string
Status string
Token string
Message string
Platform int
Error error
HideToken bool
Format string
ID string
Status string
Token string
Message string
Platform int
Error error
ErrorReason string
HideToken bool
Format string
}

// LogPush record user push request and server response.
Expand Down
5 changes: 2 additions & 3 deletions notify/notification_apns.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"time"

"github.com/appleboy/gorush/config"
"github.com/appleboy/gorush/core"
"github.com/appleboy/gorush/logx"
"github.com/appleboy/gorush/status"

Expand Down Expand Up @@ -427,7 +426,7 @@ Retry:
}

// apns server error
errLog := logPush(cfg, core.FailedPush, token, req, err)
errLog := logErrorPush(cfg, token, req, err, res.Reason)
resp.Logs = append(resp.Logs, errLog)

status.StatStorage.AddIosError(1)
Expand All @@ -439,7 +438,7 @@ Retry:
}

if res != nil && res.Sent() {
logPush(cfg, core.SucceededPush, token, req, nil)
logErrorPush(cfg, token, req, nil, res.Reason)
status.StatStorage.AddIosSuccess(1)
}

Expand Down
34 changes: 24 additions & 10 deletions notify/notification_fcm.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,12 @@ Retry:
logx.LogError.Error("FCM server send message error: " + err.Error())

if req.IsTopic() {
errLog := logPush(cfg, core.FailedPush, req.To, req, err)
errLog := logErrorPush(cfg, req.To, req, err, "")
resp.Logs = append(resp.Logs, errLog)
status.StatStorage.AddAndroidError(1)
} else {
for _, token := range req.Tokens {
errLog := logPush(cfg, core.FailedPush, token, req, err)
errLog := logErrorPush(cfg, token, req, err, "")
resp.Logs = append(resp.Logs, errLog)
}
status.StatStorage.AddAndroidError(int64(len(req.Tokens)))
Expand Down Expand Up @@ -185,12 +185,12 @@ Retry:
newTokens = append(newTokens, to)
}

errLog := logPush(cfg, core.FailedPush, to, req, result.Error)
errLog := logErrorPush(cfg, to, req, result.Error, result.ErrorResponseCode)
resp.Logs = append(resp.Logs, errLog)
continue
}

logPush(cfg, core.SucceededPush, to, req, nil)
logSuccessPush(cfg, to, req)
}

// result from Send messages to topics
Expand All @@ -204,10 +204,10 @@ Retry:
logx.LogAccess.Debug("Send Topic Message: ", to)
// Success
if res.MessageID != 0 {
logPush(cfg, core.SucceededPush, to, req, nil)
logSuccessPush(cfg, to, req)
} else {
// failure
errLog := logPush(cfg, core.FailedPush, to, req, res.Error)
errLog := logErrorPush(cfg, to, req, res.Error, res.ErrorResponseCode)
resp.Logs = append(resp.Logs, errLog)
}
}
Expand All @@ -217,7 +217,7 @@ Retry:
newTokens = append(newTokens, res.FailedRegistrationIDs...)

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

Expand All @@ -232,14 +232,28 @@ Retry:
return resp, nil
}

func logPush(cfg *config.ConfYaml, status, token string, req *PushNotification, err error) logx.LogPushEntry {
func logErrorPush(cfg *config.ConfYaml, token string, req *PushNotification, err error, errorReason string) logx.LogPushEntry {
return logx.LogPush(&logx.InputLog{
ID: req.ID,
Status: core.FailedPush,
Token: token,
Message: req.Message,
Platform: req.Platform,
Error: err,
ErrorReason: errorReason,
HideToken: cfg.Log.HideToken,
Format: cfg.Log.Format,
})
}

func logSuccessPush(cfg *config.ConfYaml, token string, req *PushNotification) logx.LogPushEntry {
return logx.LogPush(&logx.InputLog{
ID: req.ID,
Status: status,
Status: core.SucceededPush,
Token: token,
Message: req.Message,
Platform: req.Platform,
Error: err,
Error: nil,
HideToken: cfg.Log.HideToken,
Format: cfg.Log.Format,
})
Expand Down
3 changes: 1 addition & 2 deletions notify/notification_hms.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"sync"

"github.com/appleboy/gorush/config"
"github.com/appleboy/gorush/core"
"github.com/appleboy/gorush/logx"
"github.com/appleboy/gorush/status"
c "github.com/msalihkarakasli/go-hms-push/push/config"
Expand Down Expand Up @@ -199,7 +198,7 @@ Retry:
res, err := client.SendMessage(context.Background(), notification)
if err != nil {
// Send Message error
errLog := logPush(cfg, core.FailedPush, req.To, req, err)
errLog := logErrorPush(cfg, req.To, req, err, "")
resp.Logs = append(resp.Logs, errLog)
logx.LogError.Error("HMS server send message error: " + err.Error())
return
Expand Down