Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error code extraction #129

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading