diff --git a/result/example_test.go b/result/example_test.go index 3865996..097ba9f 100644 --- a/result/example_test.go +++ b/result/example_test.go @@ -3,8 +3,10 @@ package result_test import ( "fmt" "strconv" + "testing" "github.com/ayonli/goext/result" + "github.com/stretchr/testify/assert" ) func ExampleWrap() { @@ -44,3 +46,17 @@ func ExampleUnwrap() { // Output: // 30 } + +func TestWrap(t *testing.T) { + mathAdd := func(input1 string, input2 string) (int, error) { + return result.Wrap(func() (int, error) { + num1 := result.Unwrap(strconv.Atoi(input1)) + num2 := result.Unwrap(strconv.Atoi(input2)) + return num1 + num2, nil + }) + } + + res, err := mathAdd("10", "b") + assert.Equal(t, 0, res) + assert.Equal(t, "strconv.Atoi: parsing \"b\": invalid syntax", err.Error()) +} diff --git a/result/result.go b/result/result.go index f37d1c5..39dfaa7 100644 --- a/result/result.go +++ b/result/result.go @@ -5,7 +5,7 @@ import ( "fmt" ) -func Wrap[R any](fn func() (value R, err error)) (value R, err error) { +func Wrap[T any](fn func() (value T, err error)) (value T, err error) { defer func() { if re := recover(); re != nil { if _err, ok := re.(error); ok {