From: qydysky Date: Mon, 20 Jan 2025 12:30:20 +0000 (+0800) Subject: 1 (#10) X-Git-Tag: v0.28.20250120123607 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=dd39fe8d4bcd13c489992b0d0eb068cbccfc78ba;p=part%2F.git 1 (#10) --- diff --git a/errors/errors.go b/errors/errors.go index 550f771..716b049 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -30,7 +30,7 @@ func Catch(e error, action string) bool { return false } -// Grow will append error action for catch +// Grow will append e to fe func Grow(fe Error, e error) Error { if v, ok := e.(Error); ok { fe.son = v @@ -42,12 +42,12 @@ func Grow(fe Error, e error) Error { return fe } -func New(action string, reason ...string) (e Error) { +func New(reason string, action ...string) (e Error) { e = Error{ - action: action, + Reason: reason, } - if len(reason) > 0 { - e.Reason = reason[0] + if len(action) > 0 { + e.action = action[0] } return } @@ -113,20 +113,20 @@ func ErrorFormat(e error, format ...ErrFormat) (s string) { type ErrFormat func(e error) string var ( - ErrSimplifyFunc = func(e error) string { + ErrSimplifyFunc ErrFormat = func(e error) string { return e.Error() + "\n" } - ErrActionSimplifyFunc = func(e error) string { + ErrActionSimplifyFunc ErrFormat = func(e error) string { if err, ok := e.(Error); ok { return err.action + ":" + e.Error() + "\n" } else { return e.Error() + "\n" } } - ErrInLineFunc = func(e error) string { + ErrInLineFunc ErrFormat = func(e error) string { return "> " + e.Error() + " " } - ErrActionInLineFunc = func(e error) string { + ErrActionInLineFunc ErrFormat = func(e error) string { if err, ok := e.(Error); ok { return "> " + err.action + ":" + e.Error() + " " } else { diff --git a/errors/errors_test.go b/errors/errors_test.go index 14882ea..aa743bc 100644 --- a/errors/errors_test.go +++ b/errors/errors_test.go @@ -11,27 +11,28 @@ func TestXxx(t *testing.T) { err = New("r0", "a0") - if !Catch(err, "r0") { + if !Catch(err, "a0") { t.Fail() } - if Catch(err, "r1") { + if Catch(err, "r0") { t.Fail() } err = Grow(New("r1", "a1"), err) - if !Catch(err, "r0") { + if !Catch(err, "a1") { t.Fail() } - if !Catch(err, "r1") { + if !Catch(err, "a0") { t.Fail() } } + func TestXxx2(t *testing.T) { err := Grow(New("r1", "a1"), io.EOF) - if !Catch(err, "r1") { + if !Catch(err, "a1") { t.Fatal() } t.Log(err.Error()) @@ -42,10 +43,10 @@ func Test2(t *testing.T) { t.Log(ErrorFormat(e)) t.Log(ErrorFormat(e, ErrSimplifyFunc)) t.Log(ErrorFormat(e, ErrInLineFunc)) - if ErrorFormat(e, ErrSimplifyFunc) != "a0\na1\n" { + if ErrorFormat(e, ErrSimplifyFunc) != "r0\nr1\n" { t.FailNow() } - if ErrorFormat(e, ErrInLineFunc) != "> a0 > a1 " { + if ErrorFormat(e, ErrInLineFunc) != "> r0 > r1 " { t.FailNow() } } @@ -66,14 +67,14 @@ func Test1(t *testing.T) { func Test3(t *testing.T) { e := New("1") - if e.Error() != "" { - t.FailNow() + if e.Error() != "1" { + t.Fatal() } e1 := e.WithReason("2") - if e.Error() != "" { - t.FailNow() + if e.Error() != "1" { + t.Fatal() } if e1.Error() != "2" { - t.FailNow() + t.Fatal() } }