-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
86 lines (61 loc) · 1.92 KB
/
example_test.go
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package errors_test
import (
"fmt"
"github.com/primalskill/errors"
)
func ExampleE() {
err := errors.E("this is an error")
fmt.Println(err.Error())
// Output: this is an error
}
func ExampleM() {
err1 := errors.E("this is an error", errors.WithMeta("key1", "val1"))
err2 := errors.M(err1, errors.WithMeta("key2", "val2"))
fmt.Printf("%+v", errors.PrettyPrint(err2))
}
func ExampleE_meta() {
// err will carry a Meta map.
err := errors.E("this is an error with meta", errors.WithMeta("myKey", "value_testing"))
var e *errors.Error
errors.As(err, &e)
fmt.Printf("%+v", e.Meta)
// Output: [myKey:value_testing]
}
func ExampleUnwrap() {
err1 := errors.E("error 1")
err2 := errors.E("error 2", err1)
e := errors.Unwrap(err2)
fmt.Printf("%+v", e)
// Output: error 1
}
func ExamplePrettyPrint() {
err := errors.E("my error", errors.WithMeta("key", "value"))
fmt.Printf("%s", errors.PrettyPrint(err))
}
func ExampleError_PrettyPrint() {
err := errors.E("my error", errors.WithMeta("key", "value"))
var e *errors.Error
errors.As(err, &e)
fmt.Printf("%s", e.PrettyPrint())
}
func ExampleWithMeta() {
// Valid
mValid := errors.WithMeta("key1", 158, "key2", "some value", "anotherKey", true)
fmt.Printf("%#v", mValid)
// no value defined it will add !BADVALUE to noValueKey
mBadValue := errors.WithMeta("noValueKey")
fmt.Printf("\n%#v", mBadValue)
// 16 is not a string for a key it will replace it with !BADKEY2
mBadKey := errors.WithMeta("key1", "val1", 16, "val2", "key3", "val3")
fmt.Printf("\n%#v", mBadKey)
// 10 is not a string for a key it will replace it with !BADKEY2
// key3 doesn't have a value it will add !BADVALUE to key3
mBadKeyValue := errors.WithMeta("key1", "val1", 10, "val2", "key3")
fmt.Printf("\n%#v", mBadKeyValue)
}
func ExampleMeta_Merge() {
m := errors.WithMeta("key1", "val1")
m = m.Merge("key2", "val2")
fmt.Printf("%#v", m)
// Output: errors.Meta{"key1":"val1", "key2":"val2"}
}