Skip to Content

Multi-Error-Handling

Posted on

Several weeks ago, I was asked to implement a “checker” that should check multiple errors (or conditions) at one time, once there is an error returned by one of the check, the whole check process should be terminated and returns the error (the first error).

Then after I wrote it in the project, I refactored it and published in github, https://github.com/wrfly/check

This package is quite simple, https://godoc.org/github.com/wrfly/check, it only got two functions:

NoError(ctx context.Context, checkPoints []CheckErrFunc) error
Passed(ctx context.Context, checkPoints []CheckFunc) bool

NoError tells you that the check points got no error, and Passed tells you all the checkpoints is passed.

And the check point functions are quite simple too:

type CheckErrFunc func() error
type CheckFunc func() bool

You can wrap your functions in these check functions, for example:

func checkSomething() bool{
	return myCheck(a,b,c,d) == nil
}

// or

func checkSomethingError() error{
	return myCheck(a,b,c,d)
}

// myCheck() only returns an error in this
// example, but you can still use more
// complicated functions as you want

This lib use empty channels to notify the caller by close the empty channel (in Passed), and its well tested. (at lease in my opinion.)

comments powered by Disqus