-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecovery.go
46 lines (41 loc) · 1.07 KB
/
recovery.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
package m3lsh
import (
"reflect"
)
type tryFn func()
type catchFn func(interface{})
// Try catch all errors and exceptions and return them without failing
func Try(function tryFn) (err exception) {
defer func() {
if r := recover(); r != nil {
err = recoveredException(r)
}
}()
function()
return nil
}
// TryCatch catch errors specified in catchers, if an *m3lsh.BaseException is given it takes any kind of exception, otherwise, exception Type must exactly match given type
func TryCatch(function tryFn, catchers ...*CatcherWrapper) (err exception) {
defer func() {
if r := recover(); r != nil {
err = recoveredException(r)
errType := reflect.TypeOf(err)
// Find if any catchers fit
for _, CatcherWrapper := range catchers {
if isBaseException(CatcherWrapper.typ) || CatcherWrapper.typ == errType {
CatcherWrapper.fn(err)
return
}
}
panic(err)
}
}()
function()
return nil
}
// Throw Format and throw error message with type and message
func Throw(ex exception, message string) {
ex.setMessage(message)
formatException(ex)
panic(ex)
}