]> 127.0.0.1 Git - part/.git/commitdiff
1 (#10) v0.28.20250120123607
authorqydysky <qydysky@foxmail.com>
Mon, 20 Jan 2025 12:30:20 +0000 (20:30 +0800)
committerGitHub <noreply@github.com>
Mon, 20 Jan 2025 12:30:20 +0000 (20:30 +0800)
errors/errors.go
errors/errors_test.go

index 550f7718432d0bac013ecbb00cbe52a124a007ca..716b0494c173ddf01926a57691c9557680d74287 100644 (file)
@@ -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 {
index 14882ea0083e44e59eb291ff5c58ba4bb6189476..aa743bca4f58aa0065a00801aa7adb1399befe6d 100644 (file)
@@ -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()
        }
 }