diff --git a/zerror/tag.go b/zerror/tag.go index 1331996..dccdc96 100644 --- a/zerror/tag.go +++ b/zerror/tag.go @@ -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 diff --git a/zerror/tag_test.go b/zerror/tag_test.go index 9c04139..22c9769 100644 --- a/zerror/tag_test.go +++ b/zerror/tag_test.go @@ -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) }