Commit 546119d3 authored by Kirill Smelkov's avatar Kirill Smelkov

exc += XFunc(), Funcx() - functional counterparts to XRun and Runx

XRun (added in db941f12) and Runx (added in 486ede30) run a function and
convert error <-> exception back and forth. However sometimes it is
handy to only convert a function but not run it - e.g. for passing into

	x/sync/errgroup.Group.Go

To do so this patch adds XFunc and Funcx - functional counterparts to
XRun and Runx.

No new tests are needed because now XRun and Runx are just tiny wrappers
around new functions and we already have tests for XRun and Runx.
parent c0f14991
......@@ -185,10 +185,23 @@ func Addcallingcontext(topfunc string, e *Error) *Error {
return e
}
// run a function which raises exception, and return exception as regular error, if any.
// Runx runs a function which raises exception, and return exception as regular error, if any.
//
// the error, if non-nil, will be returned with added calling context - see
// Addcallingcontext for details.
//
// See also: Funcx.
func Runx(xf func()) (err error) {
return Funcx(xf)()
}
// Funcx converts a function raising exception, to function returning regular error.
//
// Returned function calls xf and converts exception, if any, to error.
//
// See also: Runx.
func Funcx(xf func()) func() error {
return func() (err error) {
here := my.FuncName()
defer Catch(func(e *Error) {
err = Addcallingcontext(here, e)
......@@ -196,10 +209,24 @@ func Runx(xf func()) (err error) {
xf()
return
}
}
// run a function which returns regular error, and raise exception if error is not nil.
// XRun runs a function which returns regular error, and raise exception if error is not nil.
//
// See also: XFunc.
func XRun(f func() error) {
XFunc(f)()
}
// XFunc converts a function returning regular error, to function raising exception.
//
// Returned function calls f and raises appropriate exception if error is not nil.
//
// See also: XRun.
func XFunc(f func() error) func() {
return func() {
err := f()
Raiseif(err)
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment