From: qydysky Date: Mon, 20 Jan 2025 13:16:16 +0000 (+0800) Subject: 1 (#11) X-Git-Tag: v0.28.20250120132246 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=f299ed5022282a2a76761a659554d5e858bb0437;p=part%2F.git 1 (#11) --- diff --git a/errors/errors.go b/errors/errors.go index 716b049..3e8bad0 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -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() + " " } diff --git a/errors/errors_test.go b/errors/errors_test.go index aa743bc..da0d1c6 100644 --- a/errors/errors_test.go +++ b/errors/errors_test.go @@ -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))