Skip to content

Commit

Permalink
Fix error code extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
larkox committed Oct 1, 2024
1 parent 681c1cb commit 302343d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
6 changes: 3 additions & 3 deletions server/android_notification_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
30 changes: 30 additions & 0 deletions server/android_notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package server

import (
"encoding/json"
"errors"
"net/http"
"os"
"testing"

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

require.NoError(t, f.Close())
}

// Copied from firebase.google.com/go/[email protected]/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)
}

0 comments on commit 302343d

Please sign in to comment.