Fix RequestError.Is

This commit is contained in:
Tulir Asokan 2023-09-04 14:31:59 +03:00
parent fece6ec681
commit e863120b51
2 changed files with 5 additions and 3 deletions

View file

@ -45,9 +45,9 @@ func (re RequestError) Error() string {
} }
func (re RequestError) Is(other error) bool { func (re RequestError) Is(other error) bool {
otherRe, ok := other.(RequestError) var otherRe RequestError
if !ok { if !errors.As(other, &otherRe) {
return errors.Is(*re.HTTP, other) return re.HTTP != nil && errors.Is(*re.HTTP, other)
} }
return otherRe.Data.GetType() == re.Data.GetType() && return otherRe.Data.GetType() == re.Data.GetType() &&
otherRe.Data.GetMessage() == re.Data.GetMessage() otherRe.Data.GetMessage() == re.Data.GetMessage()

View file

@ -2,6 +2,7 @@ package events_test
import ( import (
"encoding/base64" "encoding/base64"
"fmt"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -18,6 +19,7 @@ func TestRequestError_Is(t *testing.T) {
err := pblite.Unmarshal(dat, &errResp) err := pblite.Unmarshal(dat, &errResp)
require.NoError(t, err) require.NoError(t, err)
assert.ErrorIs(t, events.RequestError{Data: &errResp}, events.ErrRequestedEntityNotFound) assert.ErrorIs(t, events.RequestError{Data: &errResp}, events.ErrRequestedEntityNotFound)
assert.ErrorIs(t, events.RequestError{Data: &errResp}, fmt.Errorf("meow: %w", events.ErrRequestedEntityNotFound))
assert.NotErrorIs(t, events.RequestError{Data: &errResp}, events.RequestError{ assert.NotErrorIs(t, events.RequestError{Data: &errResp}, events.RequestError{
Data: &gmproto.ErrorResponse{ Data: &gmproto.ErrorResponse{
Type: 5, Type: 5,