Skip to content

Commit 79b00fc

Browse files
authored
Fix error code extraction (#129)
1 parent 681c1cb commit 79b00fc

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

server/android_notification_server.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,10 @@ func getErrorCode(err error) (string, bool) {
248248
return "", false
249249
}
250250

251-
code, ok := errorValue.FieldByName("ErrorCode").Interface().(string)
252-
if !ok {
251+
codeValue := errorValue.FieldByName("ErrorCode")
252+
if !codeValue.IsValid() {
253253
return "", false
254254
}
255255

256-
return code, true
256+
return codeValue.String(), true
257257
}

server/android_notification_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ package server
55

66
import (
77
"encoding/json"
8+
"errors"
9+
"net/http"
810
"os"
911
"testing"
1012

@@ -48,3 +50,31 @@ func TestAndroidInitialize(t *testing.T) {
4850

4951
require.NoError(t, f.Close())
5052
}
53+
54+
// Copied from firebase.google.com/go/[email protected]/internal/errors.go
55+
type ErrorCode string
56+
type FirebaseError struct {
57+
ErrorCode ErrorCode
58+
String string
59+
Response *http.Response
60+
Ext map[string]interface{}
61+
}
62+
63+
func (fe *FirebaseError) Error() string {
64+
return fe.String
65+
}
66+
67+
func TestGetErrorCode(t *testing.T) {
68+
var errorCode ErrorCode = "some error code"
69+
err := &FirebaseError{
70+
ErrorCode: errorCode,
71+
}
72+
73+
extractedCode, found := getErrorCode(err)
74+
require.True(t, found)
75+
require.Equal(t, string(errorCode), extractedCode)
76+
77+
extractedCode, found = getErrorCode(errors.New("non firebase error"))
78+
require.Equal(t, "", extractedCode)
79+
require.False(t, found)
80+
}

0 commit comments

Comments
 (0)