From 0065df25437c8a3a7a4d1ce8ab6e74be244c0703 Mon Sep 17 00:00:00 2001 From: qydysky Date: Tue, 3 Sep 2024 19:50:34 +0800 Subject: [PATCH] 1 --- errors/errors.go | 18 ++++++++++-------- errors/errors_test.go | 9 ++++++++- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/errors/errors.go b/errors/errors.go index a5b0e90..44a6efe 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -5,7 +5,7 @@ import ( ) type Error struct { - son interface{} + son error Reason string action string } @@ -30,16 +30,16 @@ func Catch(e error, action string) bool { return false } -// Grow will overwrite reason but save action for catch -func Grow(e error, son Error) Error { +// Grow will append error action for catch +func Grow(fe Error, e error) Error { if v, ok := e.(Error); ok { - son.son = v + fe.son = v } else { - son.son = Error{ - Reason: v.Error(), + fe.son = Error{ + Reason: e.Error(), } } - return son + return fe } func New(action string, reason ...string) (e Error) { @@ -85,7 +85,7 @@ func Unwrap(e error) []error { return []error{errors.Unwrap(e)} } -func ErrorFormat(e error, format ...func(error) string) (s string) { +func ErrorFormat(e error, format ...ErrFormat) (s string) { if e == nil { return "" } @@ -109,6 +109,8 @@ func ErrorFormat(e error, format ...func(error) string) (s string) { return } +type ErrFormat func(e error) string + var ( ErrSimplifyFunc = func(e error) string { return e.Error() + "\n" diff --git a/errors/errors_test.go b/errors/errors_test.go index 7dd8222..c7c2325 100644 --- a/errors/errors_test.go +++ b/errors/errors_test.go @@ -19,7 +19,7 @@ func TestXxx(t *testing.T) { t.Fail() } - err = Grow(err, New("r1", "a1")) + err = Grow(New("r1", "a1"), err) if !Catch(err, "r0") { t.Fail() @@ -29,6 +29,13 @@ func TestXxx(t *testing.T) { t.Fail() } } +func TestXxx2(t *testing.T) { + err := Grow(New("r1", "a1"), io.EOF) + if !Catch(err, "r1") { + t.Fatal() + } + t.Log(err.Error()) +} func Test2(t *testing.T) { e := Join(New("r0", "a0"), New("r1", "a1")) -- 2.39.2