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) +}