From 79b00fc4fbcfaed3f85c2fc77ca899e6457fe7d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Espino=20Garc=C3=ADa?= Date: Thu, 3 Oct 2024 09:50:49 +0200 Subject: [PATCH] Fix error code extraction (#129) --- server/android_notification_server.go | 6 +++--- server/android_notification_test.go | 30 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/server/android_notification_server.go b/server/android_notification_server.go index d96f951..66cbf5c 100644 --- a/server/android_notification_server.go +++ b/server/android_notification_server.go @@ -248,10 +248,10 @@ func getErrorCode(err error) (string, bool) { return "", false } - code, ok := errorValue.FieldByName("ErrorCode").Interface().(string) - if !ok { + codeValue := errorValue.FieldByName("ErrorCode") + if !codeValue.IsValid() { return "", false } - return code, true + return codeValue.String(), true } diff --git a/server/android_notification_test.go b/server/android_notification_test.go index 0102d7e..510035e 100644 --- a/server/android_notification_test.go +++ b/server/android_notification_test.go @@ -5,6 +5,8 @@ package server import ( "encoding/json" + "errors" + "net/http" "os" "testing" @@ -48,3 +50,31 @@ func TestAndroidInitialize(t *testing.T) { require.NoError(t, f.Close()) } + +// Copied from firebase.google.com/go/v4@v4.14.0/internal/errors.go +type ErrorCode string +type FirebaseError struct { + ErrorCode ErrorCode + String string + Response *http.Response + Ext map[string]interface{} +} + +func (fe *FirebaseError) Error() string { + return fe.String +} + +func TestGetErrorCode(t *testing.T) { + var errorCode ErrorCode = "some error code" + err := &FirebaseError{ + ErrorCode: errorCode, + } + + extractedCode, found := getErrorCode(err) + require.True(t, found) + require.Equal(t, string(errorCode), extractedCode) + + extractedCode, found = getErrorCode(errors.New("non firebase error")) + require.Equal(t, "", extractedCode) + require.False(t, found) +}