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

index 716b0494c173ddf01926a57691c9557680d74287..3e8bad03d2f3599adc37fa5567fb48b29d81282a 100644 (file)
@@ -4,10 +4,12 @@ import (
        "errors"
 )
 
+type Action string
+
 type Error struct {
        son    error
        Reason string
-       action string
+       action Action
 }
 
 func (t Error) Error() string {
@@ -19,7 +21,7 @@ func (t Error) WithReason(reason string) Error {
        return t
 }
 
-func Catch(e error, action string) bool {
+func Catch(e error, action Action) bool {
        if v, ok := e.(Error); ok {
                if v.action == action {
                        return true
@@ -42,7 +44,7 @@ func Grow(fe Error, e error) Error {
        return fe
 }
 
-func New(reason string, action ...string) (e Error) {
+func New(reason string, action ...Action) (e Error) {
        e = Error{
                Reason: reason,
        }
@@ -118,7 +120,7 @@ var (
        }
        ErrActionSimplifyFunc ErrFormat = func(e error) string {
                if err, ok := e.(Error); ok {
-                       return err.action + ":" + e.Error() + "\n"
+                       return string(err.action) + ":" + e.Error() + "\n"
                } else {
                        return e.Error() + "\n"
                }
@@ -128,7 +130,7 @@ var (
        }
        ErrActionInLineFunc ErrFormat = func(e error) string {
                if err, ok := e.(Error); ok {
-                       return "> " + err.action + ":" + e.Error() + " "
+                       return "> " + string(err.action) + ":" + e.Error() + " "
                } else {
                        return "> " + e.Error() + " "
                }
index aa743bca4f58aa0065a00801aa7adb1399befe6d..da0d1c6f88da29021c616e1711d0e444e5e0908c 100644 (file)
@@ -6,12 +6,15 @@ import (
        "testing"
 )
 
+var a0 = Action("a0")
+var a1 = Action("a1")
+
 func TestXxx(t *testing.T) {
        var err error
 
-       err = New("r0", "a0")
+       err = New("r0", a0)
 
-       if !Catch(err, "a0") {
+       if !Catch(err, a0) {
                t.Fail()
        }
 
@@ -19,27 +22,27 @@ func TestXxx(t *testing.T) {
                t.Fail()
        }
 
-       err = Grow(New("r1", "a1"), err)
+       err = Grow(New("r1", a1), err)
 
-       if !Catch(err, "a1") {
+       if !Catch(err, a1) {
                t.Fail()
        }
 
-       if !Catch(err, "a0") {
+       if !Catch(err, a0) {
                t.Fail()
        }
 }
 
 func TestXxx2(t *testing.T) {
-       err := Grow(New("r1", "a1"), io.EOF)
-       if !Catch(err, "a1") {
+       err := Grow(New("r1", a1), io.EOF)
+       if !Catch(err, a1) {
                t.Fatal()
        }
        t.Log(err.Error())
 }
 
 func Test2(t *testing.T) {
-       e := Join(New("r0", "a0"), New("r1", "a1"))
+       e := Join(New("r0", a0), New("r1", a1))
        t.Log(ErrorFormat(e))
        t.Log(ErrorFormat(e, ErrSimplifyFunc))
        t.Log(ErrorFormat(e, ErrInLineFunc))