import "go.felesatra.moe/go2/errors"
Package errors implements the error values proposal for Go 2.
https://go.googlesource.com/proposal/+/master/design/go2draft-error-values-overview.md https://go.googlesource.com/proposal/+/master/design/go2draft-error-inspection.md https://go.googlesource.com/proposal/+/master/design/go2draft-error-printing.md
AsValue checks whether err or any of the errors in its chain is a value of the type t points to. If so, it returns true, with t set to the discovered value. If not, it returns false.
Code:
package main
import (
"fmt"
"go.felesatra.moe/go2/errors"
)
type Frobber interface {
Frob() string
}
type FrobErr struct {
data string
}
func (FrobErr) Error() string {
return "frob"
}
func (f FrobErr) Frob() string {
return f.data
}
func main() {
err := errors.Wrap(FrobErr{data: "ayanami"}, "text")
var e FrobErr
if errors.AsValue(&e, err) {
fmt.Printf("data: %s\n", e.data)
}
var e2 Frobber
if errors.AsValue(&e2, err) {
fmt.Printf("interface data: %s\n", e2.Frob())
}
err = errors.New("test")
if errors.AsValue(&e, err) {
fmt.Printf("plain data: %s\n", e.data)
}
if errors.AsValue(&e2, err) {
fmt.Printf("plain interface data: %s\n", e2.Frob())
}
}
Format formats the error using the proposed style.
Code:
package main
import (
"fmt"
"go.felesatra.moe/go2/errors"
)
type Wrapper struct {
Title string
Detail string
Err error
}
func (w Wrapper) Error() string {
return w.Title
}
func (w Wrapper) Format(p errors.Printer) error {
p.Printf("%s:\n", w.Title)
if p.Detail() {
p.Printf(" %s\n", w.Detail)
}
return w.Err
}
type RootError struct {
Title string
Detail string
}
func (e RootError) Error() string {
return e.Title
}
func (e RootError) Format(p errors.Printer) error {
p.Printf("%s:\n", e.Title)
if p.Detail() {
p.Printf(" %s\n", e.Detail)
}
return nil
}
func main() {
var err error
err = Wrapper{
Title: "foo",
Detail: "some context",
Err: Wrapper{
Title: "spam",
Detail: "stuff",
Err: RootError{
Title: "egg",
Detail: "stuff",
},
},
}
fmt.Print(errors.Format(err, false))
fmt.Println()
fmt.Print(errors.Format(err, true))
fmt.Println()
err = errors.Wrap(errors.Wrap(errors.New("baz"), "bar"), "foo")
fmt.Print(errors.Format(err, true))
}
Is reports whether err or any of the errors in its chain is equal to target.
Code:
package main
import (
"fmt"
"go.felesatra.moe/go2/errors"
)
type FooError struct{}
func (FooError) Error() string {
return "foo"
}
func main() {
fmt.Printf("Plain error is FooError: %t\n", errors.Is(errors.New("text"), FooError{}))
fmt.Printf("Wrapped plain error is FooError: %t\n",
errors.Is(errors.Wrap(errors.New("text"), "text"), FooError{}))
fmt.Printf("FooError is FooError: %t\n", errors.Is(FooError{}, FooError{}))
fmt.Printf("Wrapped FooError is FooError: %t\n",
errors.Is(errors.Wrap(FooError{}, "text"), FooError{}))
}
Wrap wraps the error in a basic implementation of Wrapper.
Wrapf is like Wrap, but formats the text.
type Formatter interface {
// Format is implemented by errors to print a single error message.
// It should return the next error in the error chain, if any.
Format(p Printer) (next error)
}A Formatter formats error messages.
type Printer interface {
// Print appends args to the message output.
// String arguments are not localized, even within a localized context.
Print(args ...interface{})
// Printf writes a formatted string.
Printf(format string, args ...interface{})
// Detail reports whether error detail is requested.
// After the first call to Detail, all text written to the Printer
// is formatted as additional detail, or ignored when
// detail has not been requested.
// If Detail returns false, the caller can avoid printing the detail at all.
Detail() bool
}A Printer creates formatted error messages. It enforces that detailed information is written last.
Printer is implemented by fmt. Localization packages may provide their own implementation to support localized error messages (see for instance golang.org/x/text/message).
Wrapper defines the interface for errors wrapping another error.
Package errors imports 4 packages (graph). Updated 2018-10-07. Refresh now. Tools for package owners.