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 ¶
- func Annotate(err *Err, msg string)
- func Any(args ...interface{}) []interface{}
- func Bool(v bool, err error) bool
- func Bools(v []bool, err error) []bool
- func Byte(v byte, err error) byte
- func Bytes(v []byte, err error) []byte
- func Catch(f func(err Err))
- func CatchAll(errorHandler func(err Err), panicHandler func(v interface{}))
- func CatchTrace(exit int)
- func Check(err error)
- func Empty(_ interface{}, err error)
- func File(v *os.File, err error) *os.File
- func Float32(v float32, err error) float32
- func Float32s(v []float32, err error) []float32
- func Float64(v float64, err error) float64
- func Float64s(v []float64, err error) []float64
- func Handle(err *Err, f func())
- func Int(v int, err error) int
- func Int16(v int16, err error) int16
- func Int16s(v []int16, err error) []int16
- func Int32(v int32, err error) int32
- func Int32s(v []int32, err error) []int32
- func Int64(v int64, err error) int64
- func Int64s(v []int64, err error) []int64
- func Int8(v int8, err error) int8
- func Int8s(v []int8, err error) []int8
- func Ints(v []int, err error) []int
- func Reader(v io.Reader, err error) io.Reader
- func Request(v *http.Request, err error) *http.Request
- func Response(v *http.Response, err error) *http.Response
- func Return(err *Err)
- func ReturnStd(err *error)
- func Rune(v rune, err error) rune
- func Runes(v []rune, err error) []rune
- func StrStr(v string, v1 string, err error) (string, string)
- func String(v string, err error) string
- func Strings(v []string, err error) []string
- func Uint(v uint, err error) uint
- func Uint16(v uint16, err error) uint16
- func Uint16s(v []uint16, err error) []uint16
- func Uint32(v uint32, err error) uint32
- func Uint32s(v []uint32, err error) []uint32
- func Uint64(v uint64, err error) uint64
- func Uint64s(v []uint64, err error) []uint64
- func Uint8(v uint8, err error) uint8
- func Uint8s(v []uint8, err error) []uint8
- func Uints(v []uint, err error) []uint
- func Url(v *url.URL, err error) *url.URL
- func Writer(v io.Writer, err error) io.Writer
- func X(v interface{}, err error) interface{}
- type Err
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Annotate ¶
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 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 Float32s ¶
Float32s is a helper method to handle errors of func() ([]float32, error) functions.
func Float64s ¶
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 Request ¶
Request is a helper method to handle errors of func() (*http.Request, error) functions.
func 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 StrStr ¶
StrStr is a helper method to handle errors of func() (string, string, 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.