40 lines
774 B
Go
40 lines
774 B
Go
package errx_test
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
|
|
"git.juancwu.dev/juancwu/errx"
|
|
)
|
|
|
|
func ExampleNew() {
|
|
err := errx.New("users.Get", "user not found")
|
|
fmt.Println(err)
|
|
// Output: users.Get: user not found
|
|
}
|
|
|
|
func ExampleWrap() {
|
|
const op = "users.Get"
|
|
err := errx.Wrap(op, io.EOF)
|
|
fmt.Println(err)
|
|
// Output: users.Get: EOF
|
|
}
|
|
|
|
func ExampleWrapf() {
|
|
err := errx.Wrapf("users.Get", io.EOF, "user=%d", 42)
|
|
fmt.Println(err)
|
|
// Output: users.Get: user=42: EOF
|
|
}
|
|
|
|
func ExampleError_chain() {
|
|
fetch := func() error { return errx.Wrap("db.Query", io.EOF) }
|
|
get := func() error { return errx.Wrapf("users.Get", fetch(), "user=%d", 42) }
|
|
|
|
err := get()
|
|
fmt.Println(err)
|
|
fmt.Println(errors.Is(err, io.EOF))
|
|
// Output:
|
|
// users.Get: user=42: db.Query: EOF
|
|
// true
|
|
}
|