]> 127.0.0.1 Git - part/.git/commitdiff
1 v0.28.20240903115607
authorqydysky <qydysky@foxmail.com>
Tue, 3 Sep 2024 11:50:34 +0000 (19:50 +0800)
committerqydysky <qydysky@foxmail.com>
Tue, 3 Sep 2024 11:50:34 +0000 (19:50 +0800)
errors/errors.go
errors/errors_test.go

index a5b0e90909c3575ccd8fe43eb00ad317e74d34cb..44a6efe73dcbf109ef27d4527dbd16184d15f4f1 100644 (file)
@@ -5,7 +5,7 @@ import (
 )
 
 type Error struct {
-       son    interface{}
+       son    error
        Reason string
        action string
 }
@@ -30,16 +30,16 @@ 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 {
+// Grow will append error action for catch
+func Grow(fe Error, e error) Error {
        if v, ok := e.(Error); ok {
-               son.son = v
+               fe.son = v
        } else {
-               son.son = Error{
-                       Reason: v.Error(),
+               fe.son = Error{
+                       Reason: e.Error(),
                }
        }
-       return son
+       return fe
 }
 
 func New(action string, reason ...string) (e Error) {
@@ -85,7 +85,7 @@ func Unwrap(e error) []error {
        return []error{errors.Unwrap(e)}
 }
 
-func ErrorFormat(e error, format ...func(error) string) (s string) {
+func ErrorFormat(e error, format ...ErrFormat) (s string) {
        if e == nil {
                return ""
        }
@@ -109,6 +109,8 @@ func ErrorFormat(e error, format ...func(error) string) (s string) {
        return
 }
 
+type ErrFormat func(e error) string
+
 var (
        ErrSimplifyFunc = func(e error) string {
                return e.Error() + "\n"
index 7dd8222cb6925584030181d021e9c2f0aadbb61d..c7c2325d5d70d199003bbae2dc26af71b0f26eb3 100644 (file)
@@ -19,7 +19,7 @@ func TestXxx(t *testing.T) {
                t.Fail()
        }
 
-       err = Grow(err, New("r1", "a1"))
+       err = Grow(New("r1", "a1"), err)
 
        if !Catch(err, "r0") {
                t.Fail()
@@ -29,6 +29,13 @@ func TestXxx(t *testing.T) {
                t.Fail()
        }
 }
+func TestXxx2(t *testing.T) {
+       err := Grow(New("r1", "a1"), io.EOF)
+       if !Catch(err, "r1") {
+               t.Fatal()
+       }
+       t.Log(err.Error())
+}
 
 func Test2(t *testing.T) {
        e := Join(New("r0", "a0"), New("r1", "a1"))