-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrap_test.go
More file actions
42 lines (36 loc) · 1.15 KB
/
wrap_test.go
File metadata and controls
42 lines (36 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package panicgroup_test
import (
"fmt"
"github.com/getkalido/panicgroup"
)
// BareWrapping illustrates how WrapReturn can be used to wrap functions
// in error recovery code. Note how the safe function acts normally and
// the panicking function returns an error.
//
// The same principles apply to the other wrapping functions.
func ExampleWrapReturn_bareWrapping() {
// An example function, which has a chance to panic.
panicFunc := panicgroup.Wrap1Return1(func(input string) (string, error) {
panic(ErrSomethingTerrible)
})
result, err := panicFunc("this")
if result != "" {
fmt.Println("Panicked function returned a result.")
}
if err == nil {
fmt.Println("No error was captured.")
} else {
fmt.Println("The panic was successfully converted into an error.")
}
safeFunc := panicgroup.Wrap1Return1(func(input string) (string, error) {
return input + "!", nil
})
result, err = safeFunc("this")
fmt.Printf("Safe function returned: %q.\n", result)
if err != nil {
fmt.Printf("The safe function returned an error: %q.\n", err.Error())
}
// Output:
// The panic was successfully converted into an error.
// Safe function returned: "this!".
}