1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-11 08:12:26 +08:00

feat: add try-catch-finally implementation

This commit is contained in:
dudaodong
2024-11-04 14:46:25 +08:00
parent 840ea8f3c5
commit 8bbae69175
7 changed files with 475 additions and 70 deletions

View File

@@ -1,6 +1,7 @@
package xerror
import (
"context"
"errors"
"fmt"
"reflect"
@@ -61,7 +62,7 @@ func ExampleXError_StackTrace() {
// Output:
// github.com/duke-git/lancet/v2/xerror.ExampleXError_StackTrace
// 52
// 53
// true
}
@@ -154,3 +155,27 @@ func ExampleTryUnwrap() {
// 42
// true
}
func ExampleTryCatch() {
calledFinally := false
calledCatch := false
tc := NewTryCatch(context.Background())
tc.Try(func(ctx context.Context) error {
return errors.New("error message")
}).Catch(func(ctx context.Context, err error) {
calledCatch = true
// Error in try block at /path/xxx.go:174 - Cause: error message
// fmt.Println(err.Error())
}).Finally(func(ctx context.Context) {
calledFinally = true
}).Do()
fmt.Println(calledCatch)
fmt.Println(calledFinally)
// Output:
// true
// true
}