-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
61 lines (49 loc) · 1.1 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
package errorx_test
import (
"fmt"
"os"
"time"
"github.com/cristalhq/errorx"
)
func Example() {
func() error {
err := errorx.Newf("this is the error")
if err != nil {
return errorx.Wrapf(err, "something happened")
}
errAt := errorx.Newf("happened at: %s", time.Now())
if errAt != nil {
return errorx.Trace(err)
}
if errorx.Tracing() {
println("error tracing is enabled")
}
return nil
}()
}
func ExampleNewf() {
err := errorx.Newf("this is the error")
err2 := errorx.Newf("this is the error, with code: %d", 123)
err3 := errorx.Newf("this is the error, with code: %d", 123)
errFull := errorx.Newf("caused by: %w", err3)
if err2 == err3 {
panic("should be different")
}
fmt.Println(err)
fmt.Println(err2)
fmt.Println(err3)
fmt.Println(errFull)
// Output:
// this is the error
// this is the error, with code: 123
// this is the error, with code: 123
// caused by: this is the error, with code: 123
}
func ExampleIsAny() {
err := os.ErrPermission
if errorx.IsAny(err, os.ErrNotExist, os.ErrPermission) {
fmt.Println("it's not DNS")
}
// Output:
// it's not DNS
}