-
Notifications
You must be signed in to change notification settings - Fork 19
/
errprop.go
31 lines (26 loc) · 893 Bytes
/
errprop.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
package errors
import (
"github.com/mandelsoft/logging"
)
type ErrorFunction func() error
// PropagateError propagates a deferred error to the named return value
// whose address has to be passed as argument.
func PropagateError(errp *error, f ErrorFunction) {
PropagateErrorf(errp, f, "")
}
// PropagateErrorf propagates an optional deferred error to the named return value
// whose address has to be passed as argument.
// All errors, including the original one, are wrapped by the given context.
func PropagateErrorf(errp *error, f ErrorFunction, msg string, args ...interface{}) {
if f == nil {
*errp = ErrListf(msg, args...).Add(*errp).Result()
} else {
*errp = ErrListf(msg, args...).Add(*errp, f()).Result()
}
}
func LogError(log logging.Logger, f ErrorFunction, msg string, keypair ...interface{}) {
err := f()
if err != nil {
log.LogError(err, msg, keypair...)
}
}