123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- package errors_test
- import (
- "fmt"
- "github.com/pkg/errors"
- )
- func ExampleNew() {
- err := errors.New("whoops")
- fmt.Println(err)
-
- }
- func ExampleNew_printf() {
- err := errors.New("whoops")
- fmt.Printf("%+v", err)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- func ExampleWithMessage() {
- cause := errors.New("whoops")
- err := errors.WithMessage(cause, "oh noes")
- fmt.Println(err)
-
- }
- func ExampleWithStack() {
- cause := errors.New("whoops")
- err := errors.WithStack(cause)
- fmt.Println(err)
-
- }
- func ExampleWithStack_printf() {
- cause := errors.New("whoops")
- err := errors.WithStack(cause)
- fmt.Printf("%+v", err)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- func ExampleWrap() {
- cause := errors.New("whoops")
- err := errors.Wrap(cause, "oh noes")
- fmt.Println(err)
-
- }
- func fn() error {
- e1 := errors.New("error")
- e2 := errors.Wrap(e1, "inner")
- e3 := errors.Wrap(e2, "middle")
- return errors.Wrap(e3, "outer")
- }
- func ExampleCause() {
- err := fn()
- fmt.Println(err)
- fmt.Println(errors.Cause(err))
-
-
- }
- func ExampleWrap_extended() {
- err := fn()
- fmt.Printf("%+v\n", err)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- func ExampleWrapf() {
- cause := errors.New("whoops")
- err := errors.Wrapf(cause, "oh noes #%d", 2)
- fmt.Println(err)
-
- }
- func ExampleErrorf_extended() {
- err := errors.Errorf("whoops: %s", "foo")
- fmt.Printf("%+v", err)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- func Example_stackTrace() {
- type stackTracer interface {
- StackTrace() errors.StackTrace
- }
- err, ok := errors.Cause(fn()).(stackTracer)
- if !ok {
- panic("oops, err does not implement stackTracer")
- }
- st := err.StackTrace()
- fmt.Printf("%+v", st[0:2])
-
-
-
-
-
- }
- func ExampleCause_printf() {
- err := errors.Wrap(func() error {
- return func() error {
- return errors.Errorf("hello %s", fmt.Sprintf("world"))
- }()
- }(), "failed")
- fmt.Printf("%v", err)
-
- }
|