-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
74 lines (61 loc) · 1.47 KB
/
util.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
package try
import (
"errors"
"fmt"
"reflect"
)
var errorType reflect.Type = reflect.TypeOf((*error)(nil)).Elem()
// checkAndExecute runs the try function, and returns the outputs of the function. It'll catch the
// panic error if the CatchPanic mode is enabled.
func checkAndExecute(fn any) (out []any, err error) {
fv := checkFn(fn)
if CatchPanic {
defer func() {
e := recover()
if e == nil {
return
}
switch t := e.(type) {
case error:
err = t
case string:
err = errors.New(t)
default:
err = fmt.Errorf("%v", t)
}
}()
}
out, err = execute(fv)
return
}
// checkFn checks the try function, and it panics if the try parameter is nil or is not a function.
func checkFn(fn any) reflect.Value {
if fn == nil {
panic(ErrNilFunction)
}
fv := reflect.ValueOf(fn)
if fv.Kind() != reflect.Func {
panic(ErrNotFunction)
}
return fv
}
// execute runs the try function and returns the output of the function, it'll also set the return
// value error if the last return value of the function is an error.
func execute(fv reflect.Value) ([]any, error) {
var err error
ret := make([]any, 0, fv.Type().NumOut())
out := fv.Call(nil)
for _, v := range out {
ret = append(ret, v.Interface())
}
if len(out) > 0 {
last := out[len(out)-1]
lastType := last.Type()
if last.Kind() == reflect.Interface &&
lastType.Implements(errorType) &&
errorType.Implements(lastType) {
err = last.Interface().(error)
}
}
return ret, err
}