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(internal): make HasPlatformErrorCode work with wrapped errors #557

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions internal/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package internal

import (
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
Expand Down Expand Up @@ -97,8 +98,8 @@ func (fe *FirebaseError) Error() string {

// HasPlatformErrorCode checks if the given error contains a specific error code.
func HasPlatformErrorCode(err error, code ErrorCode) bool {
fe, ok := err.(*FirebaseError)
return ok && fe.ErrorCode == code
var fe *FirebaseError
return errors.As(err, &fe) && fe.ErrorCode == code
}

var httpStatusToErrorCodes = map[int]ErrorCode{
Expand Down
56 changes: 56 additions & 0 deletions internal/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,59 @@ func TestErrorHTTPResponse(t *testing.T) {
t.Errorf("Unmarshal(Response.Body) = %v; want = {key: value}", m)
}
}

func TestHasPlatformErrorCode(t *testing.T) {
type args struct {
err error
code ErrorCode
}
tests := []struct {
name string
args args
want bool
}{
{
name: "nil",
args: args{
err: nil,
code: Aborted,
},
want: false,
},
{
name: "not internal",
args: args{
err: fmt.Errorf("something happened"),
code: Aborted,
},
want: false,
},
{
name: "simple",
args: args{
err: &FirebaseError{
ErrorCode: Aborted,
},
code: Aborted,
},
want: true,
},
{
name: "wrapped",
args: args{
err: fmt.Errorf("[prefix] %w", &FirebaseError{
ErrorCode: Aborted,
}),
code: Aborted,
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := HasPlatformErrorCode(tt.args.err, tt.args.code); got != tt.want {
t.Errorf("HasPlatformErrorCode() = %v, want %v", got, tt.want)
}
})
}
}