Documentation ¶
Overview ¶
Package exc provides exception-style error handling for Go.
Raise and Catch allow to raise and catch exceptions.
By default the error caught is the same error that was raised. However with Context* functions can arrange for context related to what they are doing to be added to raised error as prefix, for example
func doSomething(path string) { defer exc.Context(func() interface{} { return fmt.Sprintf("doing something %s", path) })()
or
func doSomething(path string) { defer exc.Contextf("doing something %s", path) ...
Lacking such Context annotations Addcallingcontext allows to add function names up to the exception point as the calling context. However this way only actions without corresponding arguments (path in the above example) can be shown, and there have to be direct 1-1 relation between program and operational structures.
Runx allows to run a function which raises exception, and return exception as regular error, if any. Similarly XRun allows to run a function which returns regular error, and raise exception if error is not nil.
Last but not least it has to be taken into account that exceptions complicate control flow and are directly applicable only to serial programs. Their use is thus justified in only limited number of cases and by default one should always first strongly consider using explicit error returns programming style which is canonical in Go.
Index ¶
- func Catch(f func(e *Error))
- func Context(f func() interface{})
- func Contextf(format string, argv ...interface{})
- func Funcx(xf func()) func() error
- func Onunwind(f func(e *Error) *Error)
- func Raise(arg interface{})
- func Raisef(format string, a ...interface{})
- func Raiseif(err error)
- func Runx(xf func()) (err error)
- func XFunc(f func() error) func()
- func XRun(f func() error)
- type Error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Catch ¶
func Catch(f func(e *Error))
Catch catches error and calls f(e) if it was caught.
Must be called under defer.
func Context ¶
func Context(f func() interface{})
Context provides error context to be added on unwinding.
f is called if error unwinding is happening and its result is added to raised error as "prefix" context.
Must be called under defer.
func Contextf ¶
func Contextf(format string, argv ...interface{})
Contextf provides formatted string to be added to error as context on unwinding.
It is shorthand for Context wrapping fmt.Sprintf.
Must be called under defer.
func Funcx ¶
func Funcx(xf func()) func() error
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 Onunwind ¶
Onunwind installs error filter to be applied on error unwinding.
It hooks into unwinding process with f() call. Returned error is reraised. see also: Context()
Must be called under defer.
func Raise ¶
func Raise(arg interface{})
Raise raise error to upper level.
See Catch which receives raised error.
func Raiseif ¶
func Raiseif(err error)
Raiseif raises if err != nil.
NOTE err can be != nil even if typed obj = nil:
var obj *T; err = obj err != nil is true
func Runx ¶
func Runx(xf func()) (err error)
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.
Types ¶
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error is the type which is raised by Raise(arg).
func Addcallingcontext ¶
Addcallingcontext adds calling context to error.
Add calling function frames as error context up-to topfunc not including.
See also: Addcontext()
func Addcontext ¶
Addcontext adds "prefix" context to error.