-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample_test.go
122 lines (99 loc) · 3.23 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package errors_test
import (
"database/sql"
"fmt"
"net/http"
"github.com/sudo-suhas/xgo/errors"
)
func ExampleE() {
// Simple error
e1 := errors.E(
errors.WithOp("xgo_test.SimpleError"),
errors.Internal,
errors.WithText("fail"),
)
fmt.Println("\nSimple error:")
fmt.Println(e1)
// Nested error.
e2 := errors.E(errors.WithOp("xgo_test.NestedError"), errors.WithErr(e1))
fmt.Println("\nNested error:")
fmt.Println(e2)
// Output:
// Simple error:
// xgo_test.SimpleError: internal error: fail
//
// Nested error:
// xgo_test.NestedError: internal error: xgo_test.SimpleError: fail
}
func ExampleWhatKind() {
// Somewhere in the application, return the error with Kind: NotFound.
err := errors.E(errors.WithOp("Get"), errors.NotFound, errors.WithErr(sql.ErrNoRows))
// Use WhatKind to extract the Kind associated with the error.
fmt.Println("Kind:", errors.WhatKind(err))
// If required, define and use a custom Kind
k := errors.Kind{Code: "CONFLICT"}
err = errors.E(errors.WithOp("UpdateEntity"), k)
fmt.Println("Custom kind:", errors.WhatKind(err))
// Output:
// Kind: not found
// Custom kind: conflict
}
func ExampleStatusCode() {
// Somewhere in the application, return the error with Kind: NotFound.
err := errors.E(errors.WithOp("Get"), errors.NotFound, errors.WithErr(sql.ErrNoRows))
// Use StatusCode to extract the status code associated with the error Kind.
fmt.Println("Status code:", errors.StatusCode(err))
// If required, define and use a custom Kind
k := errors.Kind{Code: "CONFLICT", Status: http.StatusConflict}
err = errors.E(errors.WithOp("UpdateEntity"), k)
fmt.Println("Status code with custom kind:", errors.StatusCode(err))
// Output:
// Status code: 404
// Status code with custom kind: 409
}
func ExampleUserMsg() {
// Along with the error Kind, associate the appropriate message for the
// specific error scenario.
msg := "Username is required"
e1 := errors.E(errors.WithOp("svc.CreateUser"), errors.InvalidInput, errors.WithUserMsg(msg))
// Use UserMsg to extract the message to be shown to the user.
fmt.Println("User message:", errors.UserMsg(e1))
// Override the message by wrapping it in a new error with a different
// message.
e2 := errors.E(errors.WithErr(e1), errors.WithUserMsg("Deal with it!"))
fmt.Println("Overidden user message:", errors.UserMsg(e2))
// Output:
// User message: Username is required
// Overidden user message: Deal with it!
}
func ExampleMatch() {
msg := "Oops! Something went wrong. Please try again after some time."
err := errors.New("network unreachable")
// Construct an error, one we pretend to have received from a test.
got := errors.E(
errors.WithOp("Get"),
errors.WithUserMsg(msg),
errors.Unavailable,
errors.WithErr(err),
)
// Now construct a reference error, which might not have all
// the fields of the error from the test.
expect := errors.E(
errors.WithOp("Get"),
errors.Unavailable,
errors.WithErr(err),
)
fmt.Println("Match:", errors.Match(expect, got))
// Now one that's incorrect - wrong Kind.
got = errors.E(
errors.WithOp("Get"),
errors.WithUserMsg(msg),
errors.PermissionDenied,
errors.WithErr(err),
)
fmt.Println("Mismatch:", errors.Match(expect, got))
// Output:
//
// Match: true
// Mismatch: false
}