From 9cf4e328e3da46b862f234cff8d434f9fea7afd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Espino=20Garc=C3=ADa?= Date: Tue, 3 Sep 2024 11:13:29 +0200 Subject: [PATCH 1/2] Get status code from Firebase error --- server/android_notification_server.go | 36 ++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/server/android_notification_server.go b/server/android_notification_server.go index d9105f3..cdae3e2 100644 --- a/server/android_notification_server.go +++ b/server/android_notification_server.go @@ -8,7 +8,9 @@ import ( "encoding/json" "errors" "fmt" + "net/http" "os" + "reflect" "strconv" "time" @@ -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) @@ -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 +} From a349421f6f0d5d482e29ef468da48406f8146289 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Espino=20Garc=C3=ADa?= Date: Wed, 4 Sep 2024 11:25:14 +0200 Subject: [PATCH 2/2] Address feedback --- server/android_notification_server.go | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/server/android_notification_server.go b/server/android_notification_server.go index cdae3e2..02bd063 100644 --- a/server/android_notification_server.go +++ b/server/android_notification_server.go @@ -8,7 +8,6 @@ import ( "encoding/json" "errors" "fmt" - "net/http" "os" "reflect" "strconv" @@ -180,15 +179,18 @@ func (me *AndroidNotificationServer) SendNotification(msg *PushNotification) Pus } if err != nil { - statusCode, hasStatusCode := getStatusCode(err) + errorCode, hasStatusCode := getErrorCode(err) + if !hasStatusCode { + errorCode = "NONE" + } + me.logger.Errorf( - "Failed to send FCM push sid=%v did=%v err=%v type=%v hasStatusCode=%v statusCode=%v", + "Failed to send FCM push sid=%v did=%v err=%v type=%v errorCode=%v", msg.ServerID, msg.DeviceID, err, me.AndroidPushSettings.Type, - hasStatusCode, - statusCode, + errorCode, ) if messaging.IsUnregistered(err) { @@ -232,25 +234,25 @@ func (me *AndroidNotificationServer) SendNotification(msg *PushNotification) Pus return NewOkPushResponse() } -func getStatusCode(err error) (int, bool) { +func getErrorCode(err error) (string, bool) { if err == nil { - return 0, false + return "", false } errorPointer := reflect.ValueOf(err) if errorPointer.Kind() != reflect.Ptr { - return 0, false + return "", false } errorValue := errorPointer.Elem() if errorValue.Kind() != reflect.Struct { - return 0, false + return "", false } - response, ok := errorValue.FieldByName("Response").Interface().(*http.Response) + code, ok := errorValue.FieldByName("ErrorCode").Interface().(string) if !ok { - return 0, false + return "", false } - return response.StatusCode, true + return code, true }