-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
82 lines (70 loc) · 1.96 KB
/
example_test.go
File metadata and controls
82 lines (70 loc) · 1.96 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package errors_test
import (
stderrors "errors"
"fmt"
"io"
"github.com/go-coldbrew/errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func ExampleNew() {
err := errors.New("something went wrong")
fmt.Println(err)
// Output: something went wrong
}
func ExampleWrap() {
original := io.EOF
wrapped := errors.Wrap(original, "failed to read config")
fmt.Println(wrapped)
fmt.Println("cause:", wrapped.Cause())
// Output:
// failed to read config: EOF
// cause: EOF
}
func ExampleNewf() {
err := errors.Newf("user %s not found", "alice")
fmt.Println(err)
// Output: user alice not found
}
func Example_stackFrame() {
err := errors.New("something failed")
frames := err.StackFrame()
// Stack frames are captured automatically
fmt.Println(len(frames) > 0)
// Output: true
}
func ExampleWrapf() {
err := fmt.Errorf("connection refused")
wrapped := errors.Wrapf(err, "failed to connect to port %d", 5432)
fmt.Println(wrapped)
// Output: failed to connect to port 5432: connection refused
}
// WrapWithStatus attaches a gRPC status code to a wrapped error.
func ExampleWrapWithStatus() {
original := fmt.Errorf("record not found")
s := status.New(codes.NotFound, "user not found")
wrapped := errors.WrapWithStatus(original, "lookup failed", s)
fmt.Println(wrapped)
fmt.Println("gRPC code:", wrapped.GRPCStatus().Code())
// Output:
// lookup failed: record not found
// gRPC code: NotFound
}
// Cause returns the root cause of a wrapped error chain.
func Example_cause() {
root := io.EOF
first := errors.Wrap(root, "read body")
second := errors.Wrap(first, "handle request")
fmt.Println("error:", second)
fmt.Println("cause:", second.Cause())
// Output:
// error: handle request: read body: EOF
// cause: EOF
}
// Wrapped errors are compatible with stdlib errors.Is for unwrapping.
func ExampleWrap_errorsIs() {
original := io.EOF
wrapped := errors.Wrap(original, "read failed")
fmt.Println(stderrors.Is(wrapped, io.EOF))
// Output: true
}