Documentation
¶
Index ¶
- func And(err1 error, err2 error) error
- func As(err error, target interface{}) bool
- func Is(err, target error) bool
- func New(msg string) error
- func Newf(format string, a ...interface{}) error
- func Wrap(err error, msg string) error
- func Wrapf(err error, format string, a ...interface{}) error
- type List
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func And ¶
And creates a new combined error. Avoid `err = errors.And(err1, err)` and use `err = errors.And(err, err1)` instead when collecting numerous errors. This will save some memory allocations.
Example ¶
package main
import (
"fmt"
"io"
"github.com/sirkon/errors"
)
func main() {
fmt.Println(errors.And(io.EOF, io.EOF))
fmt.Println(errors.And(nil, io.EOF))
fmt.Println(errors.And(io.EOF, nil))
fmt.Println(errors.List{io.EOF})
}
Output: EOF; EOF EOF EOF EOF
func As ¶
As finds the first error in err's chain that matches target, and if so, sets target to that error value and returns true.
The chain consists of err itself followed by the sequence of errors obtained by repeatedly calling Unwrap.
An error matches target if the error's concrete value is assignable to the value pointed to by target, or if the error has a method As(interface{}) bool such that As(target) returns true. In the latter case, the As method is responsible for setting target.
As will panic if target is not a non-nil pointer to either a type that implements error, or to any interface type. As returns false if err is nil.
Example ¶
package main
import (
"fmt"
"io"
"github.com/sirkon/errors"
)
func main() {
var ce customError
err := errors.Wrap(customError("message"), "message")
err = customWrapper{err: err}
if errors.As(err, &ce) {
fmt.Println(ce)
}
if !errors.As(err, &fakeError{}) {
fmt.Println(err)
}
errs := errors.And(io.EOF, err)
errs = errors.And(errs, io.EOF)
if errors.As(errs, &ce) {
fmt.Println(ce)
}
fmt.Println(errors.As(errs, &fakeError{}))
}
type customWrapper struct {
err error
}
func (cw customWrapper) Error() string { return "custom wrap: " + cw.err.Error() }
func (cw customWrapper) Unwrap() error { return cw.err }
type customError string
func (ce customError) Error() string { return "error " + string(ce) }
type fakeError struct{}
func (ce fakeError) Error() string { return "fake error" }
Output: error message custom wrap: message: error message error message false
func Is ¶
Is reports whether any error in err's chain matches target.
The chain consists of err itself followed by the sequence of errors obtained by repeatedly calling Unwrap.
An error is considered to match a target if it is equal to that target or if it implements a method Is(error) bool such that Is(target) returns true.
Example ¶
package main
import (
"fmt"
"io"
"github.com/sirkon/errors"
)
func main() {
ce := customError("message")
err := errors.Wrap(customError("message"), "message")
err = customWrapper{err: err}
fmt.Println(errors.Is(err, ce))
fmt.Println(errors.Is(err, fakeError{}))
errs := errors.And(io.EOF, err)
errs = errors.And(errs, io.EOF)
fmt.Println(errors.Is(errs, ce))
fmt.Println(errors.Is(errs, fakeError{}))
fmt.Println("checking nil cases")
fmt.Println(errors.Is(nil, nil))
fmt.Println(errors.Is(io.EOF, nil))
fmt.Println(errors.Is(nil, io.EOF))
}
type customWrapper struct {
err error
}
func (cw customWrapper) Error() string { return "custom wrap: " + cw.err.Error() }
func (cw customWrapper) Unwrap() error { return cw.err }
type customError string
func (ce customError) Error() string { return "error " + string(ce) }
type fakeError struct{}
func (ce fakeError) Error() string { return "fake error" }
Output: true false true false checking nil cases true false false
func New ¶
New just an alias for stdlib's New
Example ¶
package main
import (
"fmt"
"github.com/sirkon/errors"
)
func main() {
fmt.Println(errors.New("error"))
}
Output: error
func Newf ¶
Newf an alias for fmt.Errorf
Example ¶
package main
import (
"fmt"
"github.com/sirkon/errors"
)
func main() {
fmt.Println(errors.Newf("error %s", "message"))
}
Output: error message
func Wrap ¶
Wrap consturcts a new error by wrapping given message into an error
Example ¶
package main
import (
"fmt"
"io"
"github.com/sirkon/errors"
)
func main() {
fmt.Println(errors.Wrap(io.EOF, "error"))
fmt.Println(errors.Wrap(errors.Wrapf(io.EOF, "msg %d", 1), "error"))
}
Output: error: EOF error: msg 1: EOF