Skip to content

Commit

Permalink
Get status code from Firebase error
Browse files Browse the repository at this point in the history
  • Loading branch information
larkox committed Sep 3, 2024
1 parent 7351c50 commit 9cf4e32
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion server/android_notification_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"reflect"
"strconv"
"time"

Expand Down Expand Up @@ -178,7 +180,16 @@ 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)
statusCode, hasStatusCode := getStatusCode(err)
me.logger.Errorf(
"Failed to send FCM push sid=%v did=%v err=%v type=%v hasStatusCode=%v statusCode=%v",
msg.ServerID,
msg.DeviceID,
err,
me.AndroidPushSettings.Type,
hasStatusCode,
statusCode,
)

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

func getStatusCode(err error) (int, bool) {
if err == nil {
return 0, false
}

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

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

response, ok := errorValue.FieldByName("Response").Interface().(*http.Response)
if !ok {
return 0, false
}

return response.StatusCode, true
}

0 comments on commit 9cf4e32

Please sign in to comment.