Skip to content

Commit

Permalink
✨ feat: Add Text method for TagKind to set error message text
Browse files Browse the repository at this point in the history
  • Loading branch information
sohaha committed Mar 16, 2024
1 parent 858bc69 commit 60832eb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions zerror/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ const (
func (t TagKind) Wrap(err error, text string) error {
return With(err, text, WrapTag(t))
}
func (t TagKind) Text(text string) error {
return &withTag{
wrapErr: &Error{errText: &text},
tag: t,
}
}

type withTag struct {
wrapErr error
Expand Down
8 changes: 8 additions & 0 deletions zerror/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@ func TestTag(t *testing.T) {
zerr = zerror.With(zerr, "最终错误提示", zerror.WrapTag(zerror.Unauthorized))

tt.Equal(zerror.Unauthorized, zerror.GetTag(zerr))

e := zerror.InvalidInput.Wrap(err, "输入无效")
e2 := zerror.InvalidInput.Text("输入无效")
tt.Equal(zerror.GetTag(e), zerror.GetTag(e2))
tt.Equal(zerror.InvalidInput, zerror.GetTag(e2))

tt.Logf("%v\n", e)
tt.Logf("%v\n", e2)
}

0 comments on commit 60832eb

Please sign in to comment.