Documentation ¶
Overview ¶
Package errors.
This package can be used as drop-in replacement for standard errors package.
This package provide StackTrace function to get the stack trace.
Stack trace can be attached to any error by passing it to Trace function.
Example ¶
package main import ( "bytes" "fmt" "os" "strings" "github.com/payfazz/go-errors/v2" ) func readFile() (string, error) { data, err := os.ReadFile("InvalidFile.txt") if err != nil { return "", errors.Trace(err) } return string(data), nil } func doSomething() error { data, err := readFile() if err != nil { return errors.Trace(err) } fmt.Println(data) return nil } func main() { var output bytes.Buffer if err := errors.Catch(doSomething); err != nil { for _, loc := range errors.StackTrace(err) { fmt.Fprintln(&output, loc.String()) } } fmt.Println( strings.Contains(output.String(), "readFile"), strings.Contains(output.String(), "doSomething"), ) }
Output: true true
Index ¶
- func As(err error, target any) bool
- func Assert(fact bool)
- func AssertFact(fact bool, message string)
- func Catch(f func() error) (err error)
- func Catch2[A any](f func() (A, error)) (A, error)
- func Catch3[A, B any](f func() (A, B, error)) (A, B, error)
- func Catch4[A, B, C any](f func() (A, B, C, error)) (A, B, C, error)
- func Check(err error)
- func Errorf(format string, a ...any) error
- func Format(err error) string
- func FormatWithFilter(err error, filter func(trace.Location) bool) string
- func FormatWithFilterPkgs(err error, pkgs ...string) string
- func Is(err, target error) bool
- func New(text string) error
- func StackTrace(err error) []trace.Location
- func Trace(err error) error
- func Trace2[A any](a A, err error) (A, error)
- func Trace3[A, B any](a A, b B, err error) (A, B, error)
- func Trace4[A, B, C any](a A, b B, c C, err error) (A, B, C, error)
- func Unwrap(err error) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertFact ¶ added in v2.8.2
AssertFact will panic with message if fact is false
func Catch ¶ added in v2.0.2
run f, if f panic or returned, that value will be returned by this function
func Format ¶
Format representation of the Error, including stack trace.
Use err.Error() if you want to get just the error string.
the returned string is not stable, future version maybe returned different format.
func FormatWithFilter ¶ added in v2.0.8
like Format, but you can filter what location to include in the formated string
func FormatWithFilterPkgs ¶ added in v2.0.11
like Format, but you can filter pkg location to include in the formated string
func Trace ¶ added in v2.0.4
Trace will return new error that have stack trace
will return same err if err already have stack trace
use Is function to compare the returned error with others, because equality (==) operator will fail
Types ¶
This section is empty.