Skip to content

Commit a96e318

Browse files
committed
examples
1 parent bacaae7 commit a96e318

File tree

2 files changed

+182
-1
lines changed

2 files changed

+182
-1
lines changed

error.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,12 @@ func (e *Error) Cause() error {
142142
}
143143

144144
// Format implements the Formatter interface.
145-
// Supports %v or %s for simple message output, and %+v for full output complete with a stack trace.
145+
// Supported verbs:
146+
//
147+
// %s simple message output
148+
// %v same as %s
149+
// %+v full output complete with a stack trace
150+
//
146151
// In is nearly always preferable to use %+v format.
147152
// If a stack trace is not required, it should be omitted at the moment of creation rather in formatting.
148153
func (e *Error) Format(s fmt.State, verb rune) {

example_test.go

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,182 @@ func ExampleType_Wrap() {
6262
// common.assertion_failed: wrapped, cause: common.illegal_argument
6363
}
6464

65+
func ExampleError_Format() {
66+
err := nestedCall()
67+
68+
simpleOutput := fmt.Sprintf("Error short: %v\n", err)
69+
verboseOutput := fmt.Sprintf("Error full: %+v", err)
70+
71+
fmt.Println(simpleOutput)
72+
fmt.Println(verboseOutput)
73+
74+
// Example output:
75+
//Error short: common.assertion_failed: example
76+
//
77+
//Error full: common.assertion_failed: example
78+
// at github.com/joomcode/errorx_test.someFunc()
79+
// /Users/p.ivanov/go/src/github.com/joomcode/errorx/example_test.go:102
80+
// at github.com/joomcode/errorx_test.nestedCall()
81+
// /Users/p.ivanov/go/src/github.com/joomcode/errorx/example_test.go:98
82+
// at github.com/joomcode/errorx_test.ExampleError_Format()
83+
// /Users/p.ivanov/go/src/github.com/joomcode/errorx/example_test.go:66
84+
// <...> more
85+
}
86+
87+
func ExampleEnhanceStackTrace() {
88+
errCh := make(chan error)
89+
go func() {
90+
errCh <- nestedCall()
91+
}()
92+
93+
err := <- errCh
94+
verboseOutput := fmt.Sprintf("Error full: %+v", errorx.EnhanceStackTrace(err, "another goroutine"))
95+
fmt.Println(verboseOutput)
96+
97+
// Example output:
98+
//Error full: another goroutine, cause: common.assertion_failed: example
99+
// at github.com/joomcode/errorx_test.ExampleEnhanceStackTrace()
100+
// /Users/p.ivanov/go/src/github.com/joomcode/errorx/example_test.go:94
101+
// at testing.runExample()
102+
// /usr/local/Cellar/go/1.10.3/libexec/src/testing/example.go:122
103+
// at testing.runExamples()
104+
// /usr/local/Cellar/go/1.10.3/libexec/src/testing/example.go:46
105+
// at testing.(*M).Run()
106+
// /usr/local/Cellar/go/1.10.3/libexec/src/testing/testing.go:979
107+
// at main.main()
108+
// _testmain.go:146
109+
// ...
110+
// (1 duplicated frames)
111+
// ----------------------------------
112+
// at github.com/joomcode/errorx_test.someFunc()
113+
// /Users/p.ivanov/go/src/github.com/joomcode/errorx/example_test.go:106
114+
// at github.com/joomcode/errorx_test.nestedCall()
115+
// /Users/p.ivanov/go/src/github.com/joomcode/errorx/example_test.go:102
116+
// at github.com/joomcode/errorx_test.ExampleEnhanceStackTrace.func1()
117+
// /Users/p.ivanov/go/src/github.com/joomcode/errorx/example_test.go:90
118+
// at runtime.goexit()
119+
// /usr/local/Cellar/go/1.10.3/libexec/src/runtime/asm_amd64.s:2361
120+
}
121+
122+
func ExampleIgnore() {
123+
err := errorx.IllegalArgument.NewWithNoMessage()
124+
err = errorx.Decorate(err, "more info")
125+
126+
fmt.Println(err)
127+
fmt.Println(errorx.Ignore(err, errorx.IllegalArgument))
128+
fmt.Println(errorx.Ignore(err, errorx.AssertionFailed))
129+
130+
// Output:
131+
// more info, cause: common.illegal_argument
132+
// <nil>
133+
// more info, cause: common.illegal_argument
134+
}
135+
136+
func ExampleIgnoreWithTrait() {
137+
err := errorx.TimeoutElapsed.NewWithNoMessage()
138+
err = errorx.Decorate(err, "more info")
139+
140+
fmt.Println(err)
141+
fmt.Println(errorx.IgnoreWithTrait(err, errorx.Timeout()))
142+
fmt.Println(errorx.IgnoreWithTrait(err, errorx.NotFound()))
143+
144+
// Output:
145+
// more info, cause: common.timeout
146+
// <nil>
147+
// more info, cause: common.timeout
148+
}
149+
150+
func ExampleIsOfType() {
151+
err0 := errorx.DataUnavailable.NewWithNoMessage()
152+
err1 := errorx.Decorate(err0, "decorated")
153+
err2 := errorx.RejectedOperation.Wrap(err0, "wrapped")
154+
155+
fmt.Println(errorx.IsOfType(err0, errorx.DataUnavailable))
156+
fmt.Println(errorx.IsOfType(err1, errorx.DataUnavailable))
157+
fmt.Println(errorx.IsOfType(err2, errorx.DataUnavailable))
158+
159+
// Output:
160+
// true
161+
// true
162+
// false
163+
}
164+
165+
func ExampleTypeSwitch() {
166+
err := errorx.DataUnavailable.NewWithNoMessage()
167+
168+
switch errorx.TypeSwitch(err, errorx.DataUnavailable) {
169+
case errorx.DataUnavailable:
170+
fmt.Println("good")
171+
case nil:
172+
fmt.Println("bad")
173+
default:
174+
fmt.Println("bad")
175+
}
176+
177+
switch errorx.TypeSwitch(nil, errorx.DataUnavailable) {
178+
case errorx.DataUnavailable:
179+
fmt.Println("bad")
180+
case nil:
181+
fmt.Println("good")
182+
default:
183+
fmt.Println("bad")
184+
}
185+
186+
switch errorx.TypeSwitch(err, errorx.TimeoutElapsed) {
187+
case errorx.TimeoutElapsed:
188+
fmt.Println("bad")
189+
case nil:
190+
fmt.Println("bad")
191+
default:
192+
fmt.Println("good")
193+
}
194+
195+
// Output:
196+
// good
197+
// good
198+
// good
199+
}
200+
201+
func ExampleTraitSwitch() {
202+
err := errorx.TimeoutElapsed.NewWithNoMessage()
203+
204+
switch errorx.TraitSwitch(err, errorx.Timeout()) {
205+
case errorx.Timeout():
206+
fmt.Println("good")
207+
case errorx.CaseNoError():
208+
fmt.Println("bad")
209+
default:
210+
fmt.Println("bad")
211+
}
212+
213+
switch errorx.TraitSwitch(nil, errorx.Timeout()) {
214+
case errorx.Timeout():
215+
fmt.Println("bad")
216+
case errorx.CaseNoError():
217+
fmt.Println("good")
218+
default:
219+
fmt.Println("bad")
220+
}
221+
222+
switch errorx.TraitSwitch(err, errorx.NotFound()) {
223+
case errorx.NotFound():
224+
fmt.Println("bad")
225+
case errorx.CaseNoError():
226+
fmt.Println("bad")
227+
default:
228+
fmt.Println("good")
229+
}
230+
231+
// Output:
232+
// good
233+
// good
234+
// good
235+
}
236+
237+
func nestedCall() error {
238+
return someFunc()
239+
}
240+
65241
func someFunc() error {
66242
return errorx.AssertionFailed.New("example")
67243
}

0 commit comments

Comments
 (0)