runner icon indicating copy to clipboard operation
runner copied to clipboard

thoughts on supporting functions that return errors and handling pancis

Open dvic opened this issue 6 years ago • 1 comments

What are your thoughts on adding support for getting errors back and thereby also handling panics, e.g.,

// RunErr runs a function of type func() error in a new goroutine.
// The returned channel is closed when f returns.
func (r *Runner) RunErr(f func() error) <-chan error {
	done := make(chan error, 1)
	r.wg.Add(1)
	go func() {
		var ret error
		defer func() {
			rec := recover()
			if recErr, ok := rec.(error); ok {
				ret = recErr
			} else if rec != nil {
				ret = errors.New(fmt.Sprintf("panic: %+v", rec))
			}
			done <- ret
			close(done)
			r.wg.Done()
		}()
		ret = f()
	}()
	return done
}

(and also RunErrContext and RunErrOtherContext)

dvic avatar Mar 01 '18 21:03 dvic