Skip to content

Commit 88e62f8

Browse files
committed
new functions + tests: T, T1, T2, T3
1 parent a858523 commit 88e62f8

File tree

2 files changed

+86
-13
lines changed

2 files changed

+86
-13
lines changed

err2_test.go

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ func TestTry_noError(t *testing.T) {
3333
try.To2(twoStrNoThrow())
3434
try.To2(intStrNoThrow())
3535
try.To3(boolIntStrNoThrow())
36+
37+
_ = try.T1(noThrow())("test")
38+
_, _ = try.T2(twoStrNoThrow())("test %d", 1)
39+
_, _ = try.T2(intStrNoThrow())("test %d", 2)
40+
_, _, _ = try.T3(boolIntStrNoThrow())("test %d",3)
3641
}
3742

3843
func TestDefault_Error(t *testing.T) {
@@ -814,25 +819,12 @@ func BenchmarkTry_ErrVar(b *testing.B) {
814819
}
815820
}
816821

817-
func BenchmarkTryOut_ErrVar(b *testing.B) {
818-
for n := 0; n < b.N; n++ {
819-
_, err := noThrow()
820-
try.Out(err).Handle()
821-
}
822-
}
823-
824822
func BenchmarkTry_StringGenerics(b *testing.B) {
825823
for n := 0; n < b.N; n++ {
826824
_ = try.To1(noThrow())
827825
}
828826
}
829827

830-
func BenchmarkTryOut_StringGenerics(b *testing.B) {
831-
for n := 0; n < b.N; n++ {
832-
_ = try.Out1(noThrow()).Handle()
833-
}
834-
}
835-
836828
func BenchmarkTry_StrStrGenerics(b *testing.B) {
837829
for n := 0; n < b.N; n++ {
838830
_, _ = try.To2(twoStrNoThrow())
@@ -852,6 +844,38 @@ func BenchmarkTryVarCall(b *testing.B) {
852844
}
853845
}
854846

847+
func BenchmarkTryOut_ErrVar(b *testing.B) {
848+
for n := 0; n < b.N; n++ {
849+
_, err := noThrow()
850+
try.Out(err).Handle()
851+
}
852+
}
853+
854+
func BenchmarkTryOut_LogStringGenerics(b *testing.B) {
855+
for n := 0; n < b.N; n++ {
856+
_ = try.Out1(noThrow()).Logf("test").Val1
857+
}
858+
}
859+
860+
func BenchmarkTryOut_StringGenerics(b *testing.B) {
861+
for n := 0; n < b.N; n++ {
862+
_ = try.Out1(noThrow()).Handle().Val1
863+
}
864+
}
865+
866+
func BenchmarkT_ErrVar(b *testing.B) {
867+
for n := 0; n < b.N; n++ {
868+
_, err := noThrow()
869+
try.T(err)("test")
870+
}
871+
}
872+
873+
func BenchmarkT_StringGenerics(b *testing.B) {
874+
for n := 0; n < b.N; n++ {
875+
_ = try.T1(noThrow())("test")
876+
}
877+
}
878+
855879
func BenchmarkRecursionWithOldErrorCheck(b *testing.B) {
856880
var recursionWithErrorCheck func(a int) (int, error)
857881
recursionWithErrorCheck = func(a int) (int, error) {

try/try.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ package try
6565

6666
import (
6767
"errors"
68+
"fmt"
6869
"io"
6970

7071
"github.com/lainio/err2"
72+
"github.com/lainio/err2/internal/handler"
7173
)
7274

7375
// To is a helper function to call functions which returns an error value and
@@ -239,3 +241,50 @@ func IsNotRecoverable(err error) bool {
239241
func IsNotEnabled(err error) bool {
240242
return Is(err, err2.ErrNotEnabled)
241243
}
244+
245+
// T is similar as [To] but it let's you to annotate a possible error at place.
246+
//
247+
// try.T(f.Close)("annotations")
248+
func T(err error) func(fs string, a ...any) {
249+
return func(fs string, a ...any) {
250+
if err != nil {
251+
er := fmt.Errorf(fs+handler.WrapError, append(a[1:], err)...)
252+
panic(er)
253+
}
254+
}
255+
}
256+
257+
// T1 is similar as [To1] but it let's you to annotate a possible error at place.
258+
//
259+
// f := try.T1(os.Open("filename")("cannot open cfg file")
260+
func T1[T any](v T, err error) func(fs string, a ...any) T {
261+
return func(fs string, a ...any) T {
262+
if err != nil {
263+
er := fmt.Errorf(fs+handler.WrapError, append(a[1:], err)...)
264+
panic(er)
265+
}
266+
return v
267+
}
268+
}
269+
270+
// T2 is similar as [To2] but it let's you to annotate a possible error at place.
271+
func T2[T, U any](v T, u U, err error) func(fs string, a ...any) (T, U) {
272+
return func(fs string, a ...any) (T, U) {
273+
if err != nil {
274+
er := fmt.Errorf(fs+handler.WrapError, append(a[1:], err)...)
275+
panic(er)
276+
}
277+
return v, u
278+
}
279+
}
280+
281+
// T3 is similar as [To3] but it let's you to annotate a possible error at place.
282+
func T3[T, U, V any](v1 T, v2 U, v3 V, err error) func(fs string, a ...any) (T, U, V) {
283+
return func(fs string, a ...any) (T, U, V) {
284+
if err != nil {
285+
er := fmt.Errorf(fs+handler.WrapError, append(a[1:], err)...)
286+
panic(er)
287+
}
288+
return v1, v2, v3
289+
}
290+
}

0 commit comments

Comments
 (0)