-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle.go
46 lines (44 loc) · 950 Bytes
/
handle.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 e5
import "errors"
// Handle is for error handling
//
// Error raised by Throw will be catched if any.
// If errp point to non-nil error, the error will be joined.
// If the result error is not nil, wrap functions will be applied.
// The result error will be assigned to errp if errp is not nil, otherwise Throw will be raised.
func Handle(errp *error, args ...error) {
var err error
// check throw error
if p := recover(); p != nil {
if e, ok := p.(*throw); ok {
err = e.err
} else {
panic(p)
}
}
// check pointed error
if errp != nil && *errp != nil {
if err == nil {
// no throw error
err = *errp
} else {
// wrap if not the same
if !errors.Is(err, *errp) && !errors.Is(*errp, err) {
err = Join(err, *errp)
}
}
}
if err == nil {
// no error
return
}
// wrap
err = Wrap.With(args...)(err)
if errp != nil {
// set pointed variable
*errp = err
} else {
// throw
Throw(err)
}
}