fix nil pointer dereferrence on notification errors

This commit is contained in:
Andre Medeiros 2022-06-13 16:01:36 -04:00
parent fb01cf91bc
commit 6b7abe4eb0
3 changed files with 29 additions and 3 deletions

View file

@ -327,13 +327,23 @@ func (nc *notificationsConsumer) Consume(delivery rmq.Delivery) {
}
res, err := client.Push(notification)
if err != nil || !res.Sent() {
if err != nil {
_ = nc.statsd.Incr("apns.notification.errors", []string{}, 1)
nc.logger.Error("failed to send notification",
zap.Error(err),
zap.Int64("account#id", id),
zap.String("account#username", account.NormalizedUsername()),
zap.String("device#token", device.APNSToken),
)
// Delete device as notifications might have been disabled here
_ = nc.deviceRepo.Delete(nc, device.APNSToken)
} else if !res.Sent() {
_ = nc.statsd.Incr("apns.notification.errors", []string{}, 1)
nc.logger.Error("notification not sent",
zap.Int64("account#id", id),
zap.String("account#username", account.NormalizedUsername()),
zap.String("device#token", device.APNSToken),
zap.Int("response#status", res.StatusCode),
zap.String("response#reason", res.Reason),
)

View file

@ -412,7 +412,7 @@ func (sc *subredditsConsumer) Consume(delivery rmq.Delivery) {
}
res, err := client.Push(notification)
if err != nil || !res.Sent() {
if err != nil {
_ = sc.statsd.Incr("apns.notification.errors", []string{}, 1)
sc.logger.Error("failed to send notification",
zap.Error(err),
@ -420,6 +420,14 @@ func (sc *subredditsConsumer) Consume(delivery rmq.Delivery) {
zap.String("subreddit#name", subreddit.NormalizedName()),
zap.String("post#id", post.ID),
zap.String("apns", watcher.Device.APNSToken),
)
} else if !res.Sent() {
_ = sc.statsd.Incr("apns.notification.errors", []string{}, 1)
sc.logger.Error("notificaion not sent",
zap.Int64("subreddit#id", id),
zap.String("subreddit#name", subreddit.NormalizedName()),
zap.String("post#id", post.ID),
zap.String("apns", watcher.Device.APNSToken),
zap.Int("response#status", res.StatusCode),
zap.String("response#reason", res.Reason),
)

View file

@ -291,7 +291,7 @@ func (tc *trendingConsumer) Consume(delivery rmq.Delivery) {
}
res, err := client.Push(notification)
if err != nil || !res.Sent() {
if err != nil {
_ = tc.statsd.Incr("apns.notification.errors", []string{}, 1)
tc.logger.Error("failed to send notification",
zap.Error(err),
@ -299,6 +299,14 @@ func (tc *trendingConsumer) Consume(delivery rmq.Delivery) {
zap.String("subreddit#name", subreddit.NormalizedName()),
zap.String("post#id", post.ID),
zap.String("apns", watcher.Device.APNSToken),
)
} else if !res.Sent() {
_ = tc.statsd.Incr("apns.notification.errors", []string{}, 1)
tc.logger.Error("notification not sent",
zap.Int64("subreddit#id", id),
zap.String("subreddit#name", subreddit.NormalizedName()),
zap.String("post#id", post.ID),
zap.String("apns", watcher.Device.APNSToken),
zap.Int("response#status", res.StatusCode),
zap.String("response#reason", res.Reason),
)