add errx
This commit is contained in:
parent
0a9f6491b4
commit
ff30f6c3d6
6 changed files with 302 additions and 1 deletions
40
example_test.go
Normal file
40
example_test.go
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue