try

package
v0.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 17, 2021 License: MIT Imports: 9 Imported by: 6

Documentation

Overview

Example (CopyFile)
copyFile := func(src, dst string) (err Err) {
	defer Annotate(&err, fmt.Sprintf("copy %s %s", src, dst))

	// These helpers are as fast as Check() calls
	r := File(os.Open(src))
	defer Check(r.Close())

	w := File(os.Create(dst))
	defer Handle(&err, func() {
		os.Remove(dst)
	})
	defer Check(w.Close())
	Empty(io.Copy(w, r))
	return nil
}

err := copyFile("/notfound/path/file.go", "/notfound/path/file.bak")
if err != nil {
	fmt.Println(err)
}
//nolint:lll
Output:

copy /notfound/path/file.go /notfound/path/file.bak: open /notfound/path/file.go: no such file or directory

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Annotate

func Annotate(err *Err, msg string)

Annotate adds additional messages to the error.

Example
annotated := func() (err Err) {
	defer Annotate(&err, "annotated")
	Any(throw())
	return err
}
err := annotated()
fmt.Printf("%v", err)
Output:

annotated: this is an ERROR
Example (DeferStack)
annotated := func() (err Err) {
	defer Annotate(&err, "annotated 2nd")
	defer Annotate(&err, "annotated 1st")
	Any(throw())
	return err
}
err := annotated()
fmt.Printf("%v", err)
Output:

annotated 1st: annotated 2nd: this is an ERROR

func Any

func Any(args ...interface{}) []interface{}

Any is as similar as proposed Go2 Try macro, but it's a function and it returns slice of interfaces. It has quite big performance penalty when compared to Check function.

func Bool

func Bool(v bool, err error) bool

Bool is a helper method to handle errors of func() (bool, error) functions.

func Bools

func Bools(v []bool, err error) []bool

Bools is a helper method to handle errors of func() ([]bool, error) functions.

func Byte

func Byte(v byte, err error) byte

Byte is a helper method to handle errors of func() (byte, error) functions.

func Bytes

func Bytes(v []byte, err error) []byte

Bytes is a helper method to handle errors of func() ([]byte, error) functions.

func Catch

func Catch(f func(err Err))

Catch is a convenient helper to those functions that doesn't return errors. Go's main function is a good example. Note! There can be only one deferred Catch function per non error returning function. See Handle for more information.

func CatchAll

func CatchAll(errorHandler func(err Err), panicHandler func(v interface{}))

CatchAll is a helper function to catch and write handlers for all errors and all panics thrown in the current go routine.

func CatchTrace

func CatchTrace(exit int)

CatchTrace catches all errors and prints their call stack. Setting the exit parameter to a value above 0 will make the program exit in case of an error.

func Check

func Check(err error)

Check performs the error check for the given argument. If the err is nil, it does nothing. According the measurements, it's as fast as if err != nil {return err} on happy path.

func Empty

func Empty(_ interface{}, err error)

Empty is a helper method to handle errors of func() (string, error) functions.

func File

func File(v *os.File, err error) *os.File

File is a helper method to handle errors of func() (*os.File, error) functions.

func Float32

func Float32(v float32, err error) float32

Float32 is a helper method to handle errors of func() (float32, error) functions.

func Float32s

func Float32s(v []float32, err error) []float32

Float32s is a helper method to handle errors of func() ([]float32, error) functions.

func Float64

func Float64(v float64, err error) float64

Float64 is a helper method to handle errors of func() (float64, error) functions.

func Float64s

func Float64s(v []float64, err error) []float64

Float64s is a helper method to handle errors of func() ([]float64, error) functions.

func Handle

func Handle(err *Err, f func())

Handle is for adding an error handler to a function by defer. It's for functions returning errors them self. For those functions that doesn't return errors there is a Catch function. Note! The handler function f is called only when err != nil.

Example
doSomething := func(a, b int) (err Err) {
	defer Handle(&err, func() {
		err.Annotate(fmt.Sprintf("error with (%d, %d)", a, b))
	})
	Any(throw())
	return err
}
err := doSomething(1, 2)
fmt.Printf("%v", err)
Output:

error with (1, 2): this is an ERROR

func Int

func Int(v int, err error) int

Int is a helper method to handle errors of func() (int, error) functions.

func Int16

func Int16(v int16, err error) int16

Int16 is a helper method to handle errors of func() (int16, error) functions.

func Int16s

func Int16s(v []int16, err error) []int16

Int16s is a helper method to handle errors of func() ([]int16, error) functions.

func Int32

func Int32(v int32, err error) int32

Int32 is a helper method to handle errors of func() (int32, error) functions.

func Int32s

func Int32s(v []int32, err error) []int32

Int32s is a helper method to handle errors of func() ([]int32, error) functions.

func Int64

func Int64(v int64, err error) int64

Int64 is a helper method to handle errors of func() (int64, error) functions.

func Int64s

func Int64s(v []int64, err error) []int64

Int64s is a helper method to handle errors of func() ([]int64, error) functions.

func Int8

func Int8(v int8, err error) int8

Int8 is a helper method to handle errors of func() (int8, error) functions.

func Int8s

func Int8s(v []int8, err error) []int8

Int8s is a helper method to handle errors of func() ([]int8, error) functions.

func Ints

func Ints(v []int, err error) []int

Ints is a helper method to handle errors of func() ([]int, error) functions.

func Reader

func Reader(v io.Reader, err error) io.Reader

Reader is a helper method to handle errors of func() (io.Reader, error) functions.

func Request

func Request(v *http.Request, err error) *http.Request

Request is a helper method to handle errors of func() (*http.Request, error) functions.

func Response

func Response(v *http.Response, err error) *http.Response

Response is a helper method to handle errors of func() (*http.Response, error) functions.

func Return

func Return(err *Err)

Return is same as Handle but it's for functions which don't wrap or annotate their errors. If you want to annotate errors see Annotate for more information.

Example
var err Err
defer Return(&err)
Any(noThrow())
Output:

func ReturnStd added in v0.3.1

func ReturnStd(err *error)

ReturnStd is like Return, but it returns the Go standard error type.

func Rune

func Rune(v rune, err error) rune

Rune is a helper method to handle errors of func() (rune, error) functions.

func Runes

func Runes(v []rune, err error) []rune

Runes is a helper method to handle errors of func() ([]rune, error) functions.

func StrStr

func StrStr(v string, v1 string, err error) (string, string)

StrStr is a helper method to handle errors of func() (string, string, error) functions.

func String

func String(v string, err error) string

String is a helper method to handle errors of func() (string, error) functions.

func Strings

func Strings(v []string, err error) []string

Strings is a helper method to handle errors of func() ([]string, error) functions.

func Uint

func Uint(v uint, err error) uint

Uint is a helper method to handle errors of func() (uint, error) functions.

func Uint16

func Uint16(v uint16, err error) uint16

Uint16 is a helper method to handle errors of func() (uint16, error) functions.

func Uint16s

func Uint16s(v []uint16, err error) []uint16

Uint16s is a helper method to handle errors of func() ([]uint16, error) functions.

func Uint32

func Uint32(v uint32, err error) uint32

Uint32 is a helper method to handle errors of func() (uint32, error) functions.

func Uint32s

func Uint32s(v []uint32, err error) []uint32

Uint32s is a helper method to handle errors of func() ([]uint32, error) functions.

func Uint64

func Uint64(v uint64, err error) uint64

Uint64 is a helper method to handle errors of func() (uint64, error) functions.

func Uint64s

func Uint64s(v []uint64, err error) []uint64

Uint64s is a helper method to handle errors of func() ([]uint64, error) functions.

func Uint8

func Uint8(v uint8, err error) uint8

Uint8 is a helper method to handle errors of func() (uint8, error) functions.

func Uint8s

func Uint8s(v []uint8, err error) []uint8

Uint8s is a helper method to handle errors of func() ([]uint8, error) functions.

func Uints

func Uints(v []uint, err error) []uint

Uints is a helper method to handle errors of func() ([]uint, error) functions.

func Url

func Url(v *url.URL, err error) *url.URL

Url is a helper method to handle errors of func() (*url.URL, error) functions.

func Writer

func Writer(v io.Writer, err error) io.Writer

Writer is a helper method to handle errors of func() (io.Writer, error) functions.

func X

func X(v interface{}, err error) interface{}

X is a helper method to handle errors of func() (interface{}, error) functions.

Types

type Err added in v0.2.0

type Err interface {
	Error() string
	Unwrap() error
	CallStack() []call
	CallStackString() string
	PrintCallStack()
	GetData() *tryErrData
	Annotate(msg string)
}

Err is an interface for an error with extended stack info.

func FromErr added in v0.3.0

func FromErr(err error) Err

FromErr wraps a standard error into a try-compatible one with extended stack info.

func NewErr added in v0.2.0

func NewErr(msg string) Err

NewErr creates a new try-compatible error with extended stack info.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL