Documentation
¶
Overview ¶
Package exception provides a simple exception mechanism to reduce boilerplate for trivial error forwarding in a function. Example:
func f0() error { return nil } func f1() (int, error) { return 1, nil } func MyFunc() (err error) { defer PropagateException(&err) Mustf(f0(),"f0 failed") i:=Must1f(R1(f1()), "f1 failed") fmt.Printf("got %d\n", i) return nil }
Index ¶
- Variables
- func Catch(funcs ...func()) (err error)
- func CatchError(f func(err error), matchers ...Matcher)
- func Exception(r interface{}) error
- func FilterException(r interface{}, matchers ...Matcher) error
- func ForwardExceptionf(msg string, args ...interface{})
- func Must(err error)
- func Must1[A any](r1 A, err error) A
- func Must1f[A any](r result1[A], msg string, args ...interface{}) A
- func Must2[A, B any](r1 A, r2 B, err error) (A, B)
- func Must2f[A, B any](r result2[A, B], msg string, args ...interface{}) (A, B)
- func Must3[A, B, C any](r1 A, r2 B, r3 C, err error) (A, B, C)
- func Must3f[A, B, C any](r result3[A, B, C], msg string, args ...interface{}) (A, B, C)
- func Must4[A, B, C, D any](r1 A, r2 B, r3 C, r4 D, err error) (A, B, C, D)
- func Must4f[A, B, C, D any](r result4[A, B, C, D], msg string, args ...interface{}) (A, B, C, D)
- func Mustf(err error, msg string, args ...interface{})
- func PropagateException(errp *error, matchers ...Matcher)
- func PropagateExceptionf(errp *error, msg string, args ...interface{})
- func PropagateMatchedExceptionf(errp *error, m Matcher, msg string, args ...interface{})
- func R1[A any](r1 A, err error) result1[A]
- func R2[A, B any](r1 A, r2 B, err error) result2[A, B]
- func R3[A, B, C any](r1 A, r2 B, r3 C, err error) result3[A, B, C]
- func R4[A, B, C, D any](r1 A, r2 B, r3 C, r4 D, err error) result4[A, B, C, D]
- func Throw(err error)
- func Throwf(err error, msg string, args ...interface{})
- type Matcher
Constants ¶
This section is empty.
Variables ¶
var All = func(_ error) bool { return true }
var None = func(_ error) bool { return false }
Functions ¶
func CatchError ¶
CatchError calls the given function with the error of a catched exception, if it is deferred.
func Exception ¶
func Exception(r interface{}) error
Exception provie the error object from an exception object.
func FilterException ¶
FilterException can be used in an own defer function to handle exceptions. In Go it is not possible to provide the complete catch in a function, because recover works on top-level functions, only. Therefore. the recover call has to be placed directly in the deferred wrapper function, which can then use this function to catch an exception and convert it to an error code.
func ForwardExceptionf ¶
func ForwardExceptionf(msg string, args ...interface{})
ForwardExceptionf add an error context to a forwarded exception. Usage: defer ForwardExceptionf("error context").
func Must ¶
func Must(err error)
Must converts an error (e.g. provided by a nested function call) into an exception. It provides no regular result.
func Must1 ¶
Must1 converts an error into an exception. It provides one regular result.
Usage: Must1(ErrorFunctionWithOneRegularResult()).
func Must1f ¶
Must1f converts an error into an exception. It provides one regular result, which has to be provided by method R1. Optionally an error context can be given.
Usage: Must1f(R1(ErrorFunctionWithOneRegularResult()), "context").
The intermediate function R1 is required , because GO does not allow to compose arguments provided by a function with multiple return values with additional arguments.
func Must2f ¶
Must2f like Must1f, but for two regular results, which has to be provided by method R2. The error is wrapped by the given error context.
func Must3f ¶
Must3f like Must1f, but for three regular results, which has to be provided by method R3.
func Must4f ¶
Must4f like Must1f, but for four regular results, which has to be provided by method R4.
func Mustf ¶
Must converts an error (e.g. provided by a nested function call) into an exception. It provides no regular result. The given error context is used to wrap the error.
func PropagateException ¶
PropagateException catches an exception provided by variations of MustX functions and forwards the error to its argument. It must be called by defer. Cannot reuse PropagateExceptionf, because recover MUST be called at TOP level defer function to recover the panic.
func PropagateExceptionf ¶
PropagateExceptionf catches an exception provided by variations of MustX functions and forwards the error to its argument. It must be called by defer. If an error context is given (msg!=""), it is added to the propagated error.
func R1 ¶
R1 bundles the result of an error function with one additional argument to be passed to Must1f.
func R2 ¶
R2 bundles the result of an error function with two additional arguments to be passed to Must1.
func R3 ¶
R3 bundles the result of an error function with three additional arguments to be passed to Must3.