]> 127.0.0.1 Git - part/.git/commitdiff
errors v0.20.0
authorqydysky <32743305+qydysky@users.noreply.github.com>
Mon, 28 Nov 2022 13:22:23 +0000 (21:22 +0800)
committerqydysky <32743305+qydysky@users.noreply.github.com>
Mon, 28 Nov 2022 13:22:23 +0000 (21:22 +0800)
errors/errors.go [new file with mode: 0644]
errors/errors_test.go [new file with mode: 0644]

diff --git a/errors/errors.go b/errors/errors.go
new file mode 100644 (file)
index 0000000..86045de
--- /dev/null
@@ -0,0 +1,40 @@
+package errors
+
+type Error struct {
+       son    interface{}
+       reason string
+       action string
+}
+
+func (t Error) Error() string {
+       return t.reason
+}
+
+func Catch(e error, action string) bool {
+       if v, ok := e.(Error); ok {
+               if v.action == action {
+                       return true
+               } else if v.son != nil {
+                       return Catch((v.son).(Error), action)
+               }
+       }
+       return false
+}
+
+func Grow(e error, son Error) Error {
+       if v, ok := e.(Error); ok {
+               son.son = v
+       } else {
+               son.son = Error{
+                       reason: v.Error(),
+               }
+       }
+       return son
+}
+
+func New(reason string, action string) Error {
+       return Error{
+               reason: reason,
+               action: action,
+       }
+}
diff --git a/errors/errors_test.go b/errors/errors_test.go
new file mode 100644 (file)
index 0000000..885baa4
--- /dev/null
@@ -0,0 +1,27 @@
+package errors
+
+import "testing"
+
+func TestXxx(t *testing.T) {
+       var err error
+
+       err = New("r0", "a0")
+
+       if !Catch(err, "a0") {
+               t.Fail()
+       }
+
+       if Catch(err, "a1") {
+               t.Fail()
+       }
+
+       err = Grow(err, New("r1", "a1"))
+
+       if !Catch(err, "a0") {
+               t.Fail()
+       }
+
+       if !Catch(err, "a1") {
+               t.Fail()
+       }
+}