-
Notifications
You must be signed in to change notification settings - Fork 0
/
trycatch.go
102 lines (84 loc) · 1.98 KB
/
trycatch.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
package trycatch
import (
"errors"
"reflect"
)
// IError interface
type IError interface {
GetMessage() string
}
// Error is default IError implementation
type Error struct {
str string
}
// GetMessage method
func (err Error) GetMessage() string {
return err.str
}
type catch struct {
errType reflect.Type
handler reflect.Value
}
// TryCatch the struct.
type TryCatch struct {
f func()
catches []catch
finally func()
}
// Try gets the function with expected error.
func Try(f func()) *TryCatch {
return &TryCatch{f, []catch{}, nil}
}
// Catch method
func (tryCatch *TryCatch) Catch(fn interface{}) *TryCatch {
fnV := reflect.ValueOf(fn)
if fnV.Kind() != reflect.Func {
panic(errors.New(".Catch: expected function"))
}
fnT := fnV.Type()
if fnT.NumIn() != 1 {
panic(errors.New(".Catch: expected function to accept 1 argument"))
}
tryCatch.catches = append(tryCatch.catches, catch{fnT.In(0), fnV})
return tryCatch
}
// Finally method
func (tryCatch *TryCatch) Finally(f func()) *TryCatch {
tryCatch.finally = f
return tryCatch
}
// Do runs the Try func, apply catch and finally
func (tryCatch *TryCatch) Do() {
defer func() {
if r := recover(); r != nil {
_, ok := r.(IError)
if ok {
var tryCatchErrorType = reflect.TypeOf(r)
var catched = false
for _, catcher := range tryCatch.catches {
if tryCatchErrorType.AssignableTo(catcher.errType) {
catcher.handler.Call([]reflect.Value{reflect.ValueOf(r)})
catched = true
break
}
if catcher.errType.Kind() == reflect.Interface && tryCatchErrorType.Implements(catcher.errType) {
catcher.handler.Call([]reflect.Value{reflect.ValueOf(r)})
catched = true
break
}
}
if !catched {
panic(r)
}
}
}
if tryCatch.finally != nil {
tryCatch.finally()
}
}()
tryCatch.f()
}
// RaiseError method
func RaiseError(err *Error) {
panic(err)
}