go123: lab.nexedi.com/kirr/go123/exc Index | Files

package exc

import "lab.nexedi.com/kirr/go123/exc"

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)

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.


Package Files


func Catch Uses

func Catch(f func(e *Error))

Catch catches error and calls f(e) if it was caught.

Must be called under defer.

func Context Uses

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 Funcx Uses

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 Uses

func Onunwind(f func(e *Error) *Error)

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 Uses

func Raise(arg interface{})

Raise raise error to upper level.

See Catch which receives raised error.

func Raisef Uses

func Raisef(format string, a ...interface{})

Raisef raises formatted string.

func Raiseif Uses

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 Uses

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.

func XFunc Uses

func XFunc(f func() error) func()

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 XRun Uses

func XRun(f func() error)

XRun runs a function which returns regular error, and raise exception if error is not nil.

See also: XFunc.

type Error Uses

type Error struct {
    // contains filtered or unexported fields

Error is the type which is raised by Raise(arg).

func Addcallingcontext Uses

func Addcallingcontext(topfunc string, e *Error) *Error

Addcallingcontext adds calling context to error.

Add calling function frames as error context up-to topfunc not including.

See also: Addcontext()

func Addcontext Uses

func Addcontext(e *Error, arg interface{}) *Error

Addcontext adds "prefix" context to error.

func Aserror Uses

func Aserror(v interface{}) *Error

Aserror turns any value into Error.

if v is already Error - it stays the same, otherwise new Error is created.

func (*Error) Error Uses

func (e *Error) Error() string

Package exc imports 5 packages (graph) and is imported by 3 packages. Updated 2020-07-07. Refresh now. Tools for package owners.