-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrorkit.go
109 lines (96 loc) · 2.75 KB
/
errorkit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package errorkit
import (
"fmt"
"runtime/debug"
)
// Validate creates an error when a condition is not met.
func Validate(condition bool, format string, args ...any) error {
if !condition {
return fmt.Errorf(fmt.Sprintf(format, args...))
}
return nil
}
// Try executes a function and returns the error value if it returns an error, nil otherwise.
// It also recovers from panics and converts them into errors.
func Try(fn func() error) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic occurred: %v\nStack trace:\n%s", r, debug.Stack())
}
}()
return fn()
}
// Try0 has the same behavior as Try, but fn returns no value.
func Try0(fn func()) error {
return Try(func() error {
fn()
return nil
})
}
// Try1 executes a function and returns a result and an error.
func Try1[T any](fn func() (T, error)) (result T, err error) {
err = Try(func() error {
var fnErr error
result, fnErr = fn()
return fnErr
})
return
}
// Try2 executes a function and returns two results and an error.
func Try2[T1, T2 any](fn func() (T1, T2, error)) (result1 T1, result2 T2, err error) {
err = Try(func() error {
var fnErr error
result1, result2, fnErr = fn()
return fnErr
})
return
}
// Try3 executes a function and returns three results and an error.
func Try3[T1, T2, T3 any](fn func() (T1, T2, T3, error)) (result1 T1, result2 T2, result3 T3, err error) {
err = Try(func() error {
var fnErr error
result1, result2, result3, fnErr = fn()
return fnErr
})
return
}
// TryCatch executes a function and calls the catch function if an error occurs.
func TryCatch(fn func() error, catch func(error)) {
err := Try(fn)
if err != nil {
catch(err)
}
}
// Try0Catch executes a function with no return value and calls the catch function if an error occurs.
func Try0Catch(fn func(), catch func(error)) {
if err := Try0(fn); err != nil {
catch(err)
}
}
// Try1Catch executes a function returning a result and an error,
// and calls the catch function if an error occurs.
func Try1Catch[T any](fn func() (T, error), catch func(error)) (result T) {
result, err := Try1(fn)
if err != nil {
catch(err)
}
return
}
// Try2Catch executes a function returning two results and an error,
// and calls the catch function if an error occurs.
func Try2Catch[T1, T2 any](fn func() (T1, T2, error), catch func(error)) (result1 T1, result2 T2) {
result1, result2, err := Try2(fn)
if err != nil {
catch(err)
}
return
}
// Try3Catch executes a function returning three results and an error,
// and calls the catch function if an error occurs.
func Try3Catch[T1, T2, T3 any](fn func() (T1, T2, T3, error), catch func(error)) (result1 T1, result2 T2, result3 T3) {
result1, result2, result3, err := Try3(fn)
if err != nil {
catch(err)
}
return
}