]> 127.0.0.1 Git - part/.git/commitdiff
1 v0.28.20240324140642
authorqydysky <qydysky@foxmail.com>
Sun, 24 Mar 2024 14:02:04 +0000 (22:02 +0800)
committerqydysky <qydysky@foxmail.com>
Sun, 24 Mar 2024 14:02:04 +0000 (22:02 +0800)
errors/errors.go
errors/errors_test.go

index ba3aedf0a4be8a64b8140c9621b4b30324e86efe..83769b850142d33b12233467ad2d8093ee5eade9 100644 (file)
@@ -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()
        }
 )
index 7453611760863429466e58e131383559f8c480f1..ec589cc50bddf2c58f83645fb6cc0a7b3b986a2a 100644 (file)
@@ -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()
        }
 }