github.com/mailgun/holster/v4@v4.20.0/errors/example_test.go (about)

     1  package errors_test
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/mailgun/holster/v4/errors"
     7  	pkgerrors "github.com/pkg/errors" //nolint:gomodguard // Legacy code requires deprecated package.
     8  )
     9  
    10  func ExampleNew() {
    11  	err := errors.New("whoops")
    12  	fmt.Println(err)
    13  
    14  	// Output: whoops
    15  }
    16  
    17  func ExampleNew_printf() {
    18  	err := errors.New("whoops")
    19  	fmt.Printf("%+v", err)
    20  
    21  	// Example output:
    22  	// whoops
    23  	// github.com/pkg/errors_test.ExampleNew_printf
    24  	//         /home/dfc/src/github.com/pkg/errors/example_test.go:17
    25  	// testing.runExample
    26  	//         /home/dfc/go/src/testing/example.go:114
    27  	// testing.RunExamples
    28  	//         /home/dfc/go/src/testing/example.go:38
    29  	// testing.(*M).Run
    30  	//         /home/dfc/go/src/testing/testing.go:744
    31  	// main.main
    32  	//         /github.com/pkg/errors/_test/_testmain.go:106
    33  	// runtime.main
    34  	//         /home/dfc/go/src/runtime/proc.go:183
    35  	// runtime.goexit
    36  	//         /home/dfc/go/src/runtime/asm_amd64.s:2059
    37  }
    38  
    39  func ExampleWithMessage() {
    40  	cause := errors.New("whoops")
    41  	err := errors.WithMessage(cause, "oh noes")
    42  	fmt.Println(err)
    43  
    44  	// Output: oh noes: whoops
    45  }
    46  
    47  func ExampleWithStack() {
    48  	cause := errors.New("whoops")
    49  	err := errors.WithStack(cause)
    50  	fmt.Println(err)
    51  
    52  	// Output: whoops
    53  }
    54  
    55  func ExampleWithStack_printf() {
    56  	cause := errors.New("whoops")
    57  	err := errors.WithStack(cause)
    58  	fmt.Printf("%+v", err)
    59  
    60  	// Example Output:
    61  	// whoops
    62  	// github.com/pkg/errors_test.ExampleWithStack_printf
    63  	//         /home/fabstu/go/src/github.com/pkg/errors/example_test.go:55
    64  	// testing.runExample
    65  	//         /usr/lib/go/src/testing/example.go:114
    66  	// testing.RunExamples
    67  	//         /usr/lib/go/src/testing/example.go:38
    68  	// testing.(*M).Run
    69  	//         /usr/lib/go/src/testing/testing.go:744
    70  	// main.main
    71  	//         github.com/pkg/errors/_test/_testmain.go:106
    72  	// runtime.main
    73  	//         /usr/lib/go/src/runtime/proc.go:183
    74  	// runtime.goexit
    75  	//         /usr/lib/go/src/runtime/asm_amd64.s:2086
    76  	// github.com/pkg/errors_test.ExampleWithStack_printf
    77  	//         /home/fabstu/go/src/github.com/pkg/errors/example_test.go:56
    78  	// testing.runExample
    79  	//         /usr/lib/go/src/testing/example.go:114
    80  	// testing.RunExamples
    81  	//         /usr/lib/go/src/testing/example.go:38
    82  	// testing.(*M).Run
    83  	//         /usr/lib/go/src/testing/testing.go:744
    84  	// main.main
    85  	//         github.com/pkg/errors/_test/_testmain.go:106
    86  	// runtime.main
    87  	//         /usr/lib/go/src/runtime/proc.go:183
    88  	// runtime.goexit
    89  	//         /usr/lib/go/src/runtime/asm_amd64.s:2086
    90  }
    91  
    92  func ExampleWrap() {
    93  	cause := errors.New("whoops")
    94  	err := errors.Wrap(cause, "oh noes")
    95  	fmt.Println(err)
    96  
    97  	// Output: oh noes: whoops
    98  }
    99  
   100  func fn() error {
   101  	e1 := errors.New("error")
   102  	e2 := errors.Wrap(e1, "inner")
   103  	e3 := errors.Wrap(e2, "middle")
   104  	return errors.Wrap(e3, "outer")
   105  }
   106  
   107  func ExampleCause() {
   108  	err := fn()
   109  	fmt.Println(err)
   110  	fmt.Println(errors.Cause(err))
   111  
   112  	// Output: outer: middle: inner: error
   113  	// error
   114  }
   115  
   116  func ExampleWrap_extended() {
   117  	err := fn()
   118  	fmt.Printf("%+v\n", err)
   119  
   120  	// Example output:
   121  	// error
   122  	// github.com/pkg/errors_test.fn
   123  	//         /home/dfc/src/github.com/pkg/errors/example_test.go:47
   124  	// github.com/pkg/errors_test.ExampleCause_printf
   125  	//         /home/dfc/src/github.com/pkg/errors/example_test.go:63
   126  	// testing.runExample
   127  	//         /home/dfc/go/src/testing/example.go:114
   128  	// testing.RunExamples
   129  	//         /home/dfc/go/src/testing/example.go:38
   130  	// testing.(*M).Run
   131  	//         /home/dfc/go/src/testing/testing.go:744
   132  	// main.main
   133  	//         /github.com/pkg/errors/_test/_testmain.go:104
   134  	// runtime.main
   135  	//         /home/dfc/go/src/runtime/proc.go:183
   136  	// runtime.goexit
   137  	//         /home/dfc/go/src/runtime/asm_amd64.s:2059
   138  	// github.com/pkg/errors_test.fn
   139  	// 	  /home/dfc/src/github.com/pkg/errors/example_test.go:48: inner
   140  	// github.com/pkg/errors_test.fn
   141  	//        /home/dfc/src/github.com/pkg/errors/example_test.go:49: middle
   142  	// github.com/pkg/errors_test.fn
   143  	//      /home/dfc/src/github.com/pkg/errors/example_test.go:50: outer
   144  }
   145  
   146  func ExampleWrapf() {
   147  	cause := errors.New("whoops")
   148  	err := errors.Wrapf(cause, "oh noes #%d", 2)
   149  	fmt.Println(err)
   150  
   151  	// Output: oh noes #2: whoops
   152  }
   153  
   154  func ExampleErrorf_extended() {
   155  	err := errors.Errorf("whoops: %s", "foo")
   156  	fmt.Printf("%+v", err)
   157  
   158  	// Example output:
   159  	// whoops: foo
   160  	// github.com/pkg/errors_test.ExampleErrorf
   161  	//         /home/dfc/src/github.com/pkg/errors/example_test.go:101
   162  	// testing.runExample
   163  	//         /home/dfc/go/src/testing/example.go:114
   164  	// testing.RunExamples
   165  	//         /home/dfc/go/src/testing/example.go:38
   166  	// testing.(*M).Run
   167  	//         /home/dfc/go/src/testing/testing.go:744
   168  	// main.main
   169  	//         /github.com/pkg/errors/_test/_testmain.go:102
   170  	// runtime.main
   171  	//         /home/dfc/go/src/runtime/proc.go:183
   172  	// runtime.goexit
   173  	//         /home/dfc/go/src/runtime/asm_amd64.s:2059
   174  }
   175  
   176  func Example_stackTrace() {
   177  	type stackTracer interface {
   178  		StackTrace() pkgerrors.StackTrace
   179  	}
   180  
   181  	err, ok := errors.Cause(fn()).(stackTracer)
   182  	if !ok {
   183  		panic("oops, err does not implement stackTracer")
   184  	}
   185  
   186  	st := err.StackTrace()
   187  	fmt.Printf("%+v", st[0:2]) // top two frames
   188  
   189  	// Example output:
   190  	// github.com/pkg/errors_test.fn
   191  	//	/home/dfc/src/github.com/pkg/errors/example_test.go:47
   192  	// github.com/pkg/errors_test.Example_stackTrace
   193  	//	/home/dfc/src/github.com/pkg/errors/example_test.go:127
   194  }
   195  
   196  func ExampleCause_printf() {
   197  	err := errors.Wrap(func() error {
   198  		return func() error {
   199  			return errors.Errorf("hello %s", "world")
   200  		}()
   201  	}(), "failed")
   202  
   203  	fmt.Printf("%v", err)
   204  
   205  	// Output: failed: hello world
   206  }