-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_cause_test.go
More file actions
59 lines (49 loc) · 1.29 KB
/
example_cause_test.go
File metadata and controls
59 lines (49 loc) · 1.29 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package errors_test
import (
"fmt"
"io"
"net"
"os"
"github.com/upfluence/errors"
)
func ExampleCause() {
rootErr := errors.New("root cause")
wrapped := errors.Wrap(rootErr, "additional context")
root := errors.Cause(wrapped)
fmt.Println(root)
// Output: root cause
}
func ExampleUnwrap() {
rootErr := errors.New("root")
wrapped := errors.Wrap(rootErr, "wrapper")
unwrapped := errors.Unwrap(wrapped)
fmt.Println(unwrapped)
// Output: wrapper: root
}
func ExampleAs() {
// Create a PathError
pathErr := &os.PathError{Op: "open", Path: "/tmp/file.txt", Err: os.ErrNotExist}
wrapped := errors.Wrap(pathErr, "failed to process file")
// Use As to extract the PathError
var targetErr *os.PathError
if errors.As(wrapped, &targetErr) {
fmt.Println("Failed at path:", targetErr.Path)
}
// Output: Failed at path: /tmp/file.txt
}
func ExampleIs() {
err := errors.Wrap(io.EOF, "read operation failed")
if errors.Is(err, io.EOF) {
fmt.Println("End of file reached")
}
// Output: End of file reached
}
func ExampleIsTimeout() {
// Create a timeout error
timeoutErr := &net.DNSError{IsTimeout: true}
wrapped := errors.Wrap(timeoutErr, "network operation failed")
if errors.IsTimeout(wrapped) {
fmt.Println("Operation timed out, retrying...")
}
// Output: Operation timed out, retrying...
}