Skip to content

Commit

Permalink
Get status code from Firebase error (#126)
Browse files Browse the repository at this point in the history
* Get status code from Firebase error

* Address feedback

---------

Co-authored-by: Mattermost Build <[email protected]>
  • Loading branch information
larkox and mattermost-build authored Sep 6, 2024
1 parent 7351c50 commit 83b3754
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion server/android_notification_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"os"
"reflect"
"strconv"
"time"

Expand Down Expand Up @@ -178,7 +179,19 @@ func (me *AndroidNotificationServer) SendNotification(msg *PushNotification) Pus
}

if err != nil {
me.logger.Errorf("Failed to send FCM push sid=%v did=%v err=%v type=%v", msg.ServerID, msg.DeviceID, err, me.AndroidPushSettings.Type)
errorCode, hasStatusCode := getErrorCode(err)
if !hasStatusCode {
errorCode = "NONE"
}

me.logger.Errorf(
"Failed to send FCM push sid=%v did=%v err=%v type=%v errorCode=%v",
msg.ServerID,
msg.DeviceID,
err,
me.AndroidPushSettings.Type,
errorCode,
)

if messaging.IsUnregistered(err) {
me.logger.Infof("Android response failure sending remove code: type=%v", me.AndroidPushSettings.Type)
Expand Down Expand Up @@ -220,3 +233,26 @@ func (me *AndroidNotificationServer) SendNotification(msg *PushNotification) Pus
}
return NewOkPushResponse()
}

func getErrorCode(err error) (string, bool) {
if err == nil {
return "", false
}

errorPointer := reflect.ValueOf(err)
if errorPointer.Kind() != reflect.Ptr {
return "", false
}

errorValue := errorPointer.Elem()
if errorValue.Kind() != reflect.Struct {
return "", false
}

code, ok := errorValue.FieldByName("ErrorCode").Interface().(string)
if !ok {
return "", false
}

return code, true
}

0 comments on commit 83b3754

Please sign in to comment.