From 3c3c4cdc2ed4e8738084da1d32cb55f9ad20b940 Mon Sep 17 00:00:00 2001 From: qydysky Date: Tue, 26 Mar 2024 01:01:28 +0800 Subject: [PATCH] 1 --- errors/errors.go | 5 +++++ errors/errors_test.go | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/errors/errors.go b/errors/errors.go index 1d02ffc..5e4330e 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -14,6 +14,11 @@ func (t Error) Error() string { return t.Reason } +func (t Error) WithReason(reason string) Error { + t.Reason = reason + return t +} + func Catch(e error, action string) bool { if v, ok := e.(Error); ok { if v.action == action { diff --git a/errors/errors_test.go b/errors/errors_test.go index ec589cc..83c7a47 100644 --- a/errors/errors_test.go +++ b/errors/errors_test.go @@ -55,3 +55,17 @@ func Test1(t *testing.T) { t.FailNow() } } + +func Test3(t *testing.T) { + e := New("1") + if e.Error() != "" { + t.FailNow() + } + e1 := e.WithReason("2") + if e.Error() != "" { + t.FailNow() + } + if e1.Error() != "2" { + t.FailNow() + } +} -- 2.39.2