Skip to content

Commit 313e26d

Browse files
committed
Handle examples for multi-handler and annotation
1 parent 45fa921 commit 313e26d

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

err2_test.go

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,9 +655,22 @@ func ExampleHandle_errThrow() {
655655
// Output: testing: run example: our error
656656
}
657657

658+
func ExampleHandle_annotatedErrReturn() {
659+
normalReturn := func() (err error) {
660+
defer err2.Handle(&err) // automatic annotation
661+
return fmt.Errorf("our error")
662+
}
663+
err := normalReturn()
664+
fmt.Printf("%v", err)
665+
666+
// ------- automatic in Go example/test
667+
// ------- v ------------------ v --------
668+
// Output: testing: run example: our error
669+
}
670+
658671
func ExampleHandle_errReturn() {
659672
normalReturn := func() (err error) {
660-
defer err2.Handle(&err, "")
673+
defer err2.Handle(&err, nil) // nil disables automatic annotation
661674
return fmt.Errorf("our error")
662675
}
663676
err := normalReturn()
@@ -729,7 +742,9 @@ func ExampleHandle_handlerFn() {
729742
doSomething := func(a, b int) (err error) {
730743
defer err2.Handle(&err, func(err error) error {
731744
// Example for just annotating current err. Normally Handle is
732-
// used for cleanup. See CopyFile example for more information.
745+
// used for e.g. cleanup, not annotation that can be left for
746+
// err2 automatic annotation. See CopyFile example for more
747+
// information.
733748
return fmt.Errorf("error with (%d, %d): %v", a, b, err)
734749
})
735750
try.To1(throw())
@@ -740,6 +755,26 @@ func ExampleHandle_handlerFn() {
740755
// Output: error with (1, 2): this is an ERROR
741756
}
742757

758+
func ExampleHandle_multipleHandlerFns() {
759+
doSomething := func(a, b int) (err error) {
760+
defer err2.Handle(&err,
761+
// cause automatic annotation <== 2 error handlers do the trick
762+
err2.Noop,
763+
func(err error) error {
764+
// Example for just annotating current err. Normally Handle
765+
// is used for e.g. cleanup, not annotation that can be left
766+
// for err2 automatic annotation. See CopyFile example for
767+
// more information.
768+
return fmt.Errorf("%w error with (%d, %d)", err, a, b)
769+
})
770+
try.To1(throw())
771+
return err
772+
}
773+
err := doSomething(1, 2)
774+
fmt.Printf("%v", err)
775+
// Output: testing: run example: this is an ERROR error with (1, 2)
776+
}
777+
743778
func ExampleHandle_noThrow() {
744779
doSomething := func(a, b int) (err error) {
745780
defer err2.Handle(&err, func(err error) error {

0 commit comments

Comments
 (0)