From b9264301645f64bb6fcbc72eef1c9d69cf472492 Mon Sep 17 00:00:00 2001 From: qydysky Date: Sun, 24 Mar 2024 22:02:04 +0800 Subject: [PATCH] 1 --- errors/errors.go | 11 +++++------ errors/errors_test.go | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/errors/errors.go b/errors/errors.go index ba3aedf..83769b8 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -2,7 +2,6 @@ package errors import ( "errors" - "fmt" ) type Error struct { @@ -26,6 +25,7 @@ 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 { if v, ok := e.(Error); ok { son.son = v @@ -103,10 +103,9 @@ func ErrorFormat(e error, format ...func(error) string) (s string) { var ( ErrSimplifyFunc = func(e error) string { - if es := e.Error(); len(es) > 20 { - return fmt.Sprintf("%.16s...\n", es) - } else { - return es + "\n" - } + return e.Error() + "\n" + } + ErrInLineFunc = func(e error) string { + return " > " + e.Error() } ) diff --git a/errors/errors_test.go b/errors/errors_test.go index 7453611..ec589cc 100644 --- a/errors/errors_test.go +++ b/errors/errors_test.go @@ -30,13 +30,28 @@ func TestXxx(t *testing.T) { } } +func Test2(t *testing.T) { + e := Join(New("r0", "a0"), New("r1", "a1")) + t.Log(ErrorFormat(e, ErrSimplifyFunc)) + t.Log(ErrorFormat(e, ErrInLineFunc)) + if ErrorFormat(e, ErrSimplifyFunc) != "r0\nr1\n" { + t.FailNow() + } + if ErrorFormat(e, ErrInLineFunc) != " > r0 > r1" { + t.FailNow() + } +} + func Test1(t *testing.T) { e := Join(io.EOF, io.ErrClosedPipe) e = Join(io.EOF, e) if !errors.Is(e, io.ErrClosedPipe) { t.FailNow() } - if ErrorFormat(e, ErrSimplifyFunc) != "EOF\nEOF\nio: read/write o...\n" { + if ErrorFormat(e, ErrSimplifyFunc) != "EOF\nEOF\nio: read/write on closed pipe\n" { + t.FailNow() + } + if ErrorFormat(e, ErrInLineFunc) != " > EOF > EOF > io: read/write on closed pipe" { t.FailNow() } } -- 2.39.2