Documentation ¶
Index ¶
- Variables
- func As(err error, target any) bool
- func Cause(err error) error
- func Errorf(format string, args ...any) error
- func Into[T error](err error) (val T, ok bool)
- func Is(err, target error) bool
- func Join(errs ...error) error
- func New(text string) error
- func Unwrap(err error) error
- func WithMessage(err error, message string) error
- func WithMessagef(err error, format string, args ...any) error
- func Wrap(err error, message string) error
- func Wrapf(err error, format string, args ...any) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrUnsupported = errors.ErrUnsupported
ErrUnsupported indicates that a requested operation cannot be performed, because it is unsupported. requires minimum go 1.21.
Functions ¶
func As ¶
As finds the first error in err's tree that matches target, and if one is found, sets target to that error value and returns true. Otherwise, it returns false.
func Cause ¶
Cause returns the underlying cause of the error, if possible. An error value has a cause if it implements the following interface:
type causer interface { Cause() error }
If the error does not implement Cause, we will call Unwrap in a loop until find the original error. If the error is nil, nil will be returned without further investigation.
Example ¶
package main import ( "fmt" "github.com/peczenyj/errors" ) func fn() error { e1 := errors.New("error") e2 := errors.Wrap(e1, "inner") e3 := errors.Wrap(e2, "middle") return errors.Wrap(e3, "outer") } func main() { err := fn() fmt.Println(err) fmt.Println(errors.Cause(err)) }
Output: outer: middle: inner: error error
func Errorf ¶
Errorf formats according to a format specifier and returns the string as a value that satisfies error.
func Into ¶ added in v0.1.0
Into finds the first error in err's chain that matches target type T, and if so, returns it.
Into is type-safe alternative to As.
this function was ported from https://github.com/go-faster/errors
Example ¶
package main import ( "fmt" "github.com/peczenyj/errors" ) type timeoutError struct { err error } func (te *timeoutError) Error() string { return "timeout: " + te.err.Error() } func (te *timeoutError) Timeout() bool { return true } type TimeoutError interface { Timeout() bool error } func main() { err := errors.New("ops") err = &timeoutError{err} err = errors.Wrap(err, "unexpected") if terr, ok := errors.Into[TimeoutError](err); ok { fmt.Println(terr) } }
Output: timeout: ops
func New ¶
New returns an error that formats as the given text.
Example ¶
package main import ( "fmt" "github.com/peczenyj/errors" ) func main() { err := errors.New("whoops") fmt.Println(err) }
Output: whoops
func Unwrap ¶
Unwrap returns the result of calling the Unwrap method on err, if err's type contains an Unwrap method returning error. Otherwise, Unwrap returns nil.
func WithMessage ¶
WithMessage is an alias to Wrap.
Example ¶
package main import ( "fmt" "github.com/peczenyj/errors" ) func main() { cause := errors.New("whoops") err := errors.WithMessage(cause, "oh noes") fmt.Println(err) }
Output: oh noes: whoops
func WithMessagef ¶
WithMessagef is an alias to Wrapf.
func Wrap ¶
Wrap returns an error annotating err, it is the equivalent to
fmt.Errorf("%s: %w", message, err)
If err is nil, Wrap returns nil.
Example ¶
package main import ( "fmt" "github.com/peczenyj/errors" ) func main() { cause := errors.New("whoops") err := errors.Wrap(cause, "oh noes") fmt.Println(err) }
Output: oh noes: whoops
Types ¶
This section is empty.