Back to godoc.org

Package exc

v0.0.0-...-3cf7f3f
Latest Go to latest

The latest major version is .

Published: May 27, 2020 | License: GPL3 | Module: lab.nexedi.com/kirr/go123

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)
	})()

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

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

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

func Raise(arg interface{})

Raise raise error to upper level.

See Catch which receives raised error.

func Raisef

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

Raisef raises formatted string.

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.

func XFunc

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

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

type Error struct {
	// contains filtered or unexported fields
}

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

func Addcallingcontext

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

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

Addcontext adds "prefix" context to error.

func Aserror

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

func (e *Error) Error() string

Package Files

  • error.go
Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier