From ab50136d8476782d0653b869021dc7e0fc18649c Mon Sep 17 00:00:00 2001 From: Johan Lindh Date: Tue, 16 Jan 2024 10:34:04 +0100 Subject: [PATCH] ErrIllegalTagType prints tag type --- tag.go | 14 ++++++++++---- tag_test.go | 6 +++++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/tag.go b/tag.go index 945123f..d8196d5 100644 --- a/tag.go +++ b/tag.go @@ -27,10 +27,16 @@ func (errTooManyTags) Error() string { var ErrTooManyTags = errTooManyTags{} -type errIllegalTagType struct{} +type errIllegalTagType struct { + tag any +} + +func (e errIllegalTagType) Error() string { + return fmt.Sprintf("illegal tag type %T", e.tag) +} -func (errIllegalTagType) Error() string { - return "illegal tag type" +func (errIllegalTagType) Is(other error) bool { + return other == ErrIllegalTagType } var ErrIllegalTagType = errIllegalTagType{} @@ -82,7 +88,7 @@ func tagExpand(l int, rq *Request, tag interface{}, result []interface{}) ([]int default: return append(result, data), nil } - return result, ErrIllegalTagType + return result, errIllegalTagType{tag: tag} } func TagExpand(rq *Request, tag interface{}) ([]interface{}, error) { diff --git a/tag_test.go b/tag_test.go index d321812..7b09db0 100644 --- a/tag_test.go +++ b/tag_test.go @@ -5,6 +5,7 @@ import ( "fmt" "html/template" "reflect" + "strings" "sync/atomic" "testing" ) @@ -80,7 +81,10 @@ func TestTagExpand_IllegalTypesPanic(t *testing.T) { if !ok { t.Fail() } - if e.Error() != ErrIllegalTagType.Error() { + if !errors.Is(e, ErrIllegalTagType) { + t.Fail() + } + if !strings.Contains(e.Error(), fmt.Sprintf("%T", tag)) { t.Fail() } }()