-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_example_test.go
More file actions
260 lines (233 loc) · 8.62 KB
/
error_example_test.go
File metadata and controls
260 lines (233 loc) · 8.62 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package core_test
import (
. "dappco.re/go"
)
// ExampleErrorSink declares an error sink through `ErrorSink` for dAppCore error handling.
// Operations, codes, roots, and crash reports remain inspectable through core errors.
func ExampleErrorSink() {
var sink ErrorSink = NewLog(LogOptions{Output: NewBuffer()})
sink.Warn("example")
}
// ExampleErr_Error writes or renders an error through `Err.Error` for dAppCore error
// handling. Operations, codes, roots, and crash reports remain inspectable through core
// errors.
func ExampleErr_Error() {
err := &Err{Operation: "cache.Get", Message: "missing", Code: "NOT_FOUND"}
Println(err.Error())
// Output: cache.Get: missing [NOT_FOUND]
}
// ExampleErr_Unwrap unwraps an error through `Err.Unwrap` for dAppCore error handling.
// Operations, codes, roots, and crash reports remain inspectable through core errors.
func ExampleErr_Unwrap() {
cause := NewError("root")
err := &Err{Operation: "cache.Get", Message: "missing", Cause: cause}
Println(err.Unwrap() == cause)
// Output: true
}
// ExampleE builds a scoped error through `E` for dAppCore error handling. Operations,
// codes, roots, and crash reports remain inspectable through core errors.
func ExampleE() {
err := E("cache.Get", "key not found", nil)
Println(Operation(err))
Println(ErrorMessage(err))
// Output:
// cache.Get
// key not found
}
// ExampleWrap wraps an error through `Wrap` for dAppCore error handling. Operations,
// codes, roots, and crash reports remain inspectable through core errors.
func ExampleWrap() {
cause := NewError("connection refused")
err := Wrap(cause, "database.Connect", "failed to reach host")
Println(Operation(err))
Println(Is(err, cause))
// Output:
// database.Connect
// true
}
// ExampleWrapCode wraps an error with a code through `WrapCode` for dAppCore error
// handling. Operations, codes, roots, and crash reports remain inspectable through core
// errors.
func ExampleWrapCode() {
err := WrapCode(NewError("bad input"), "VALIDATION", "user.Save", "invalid user")
Println(ErrorCode(err))
Println(Operation(err))
// Output:
// VALIDATION
// user.Save
}
// ExampleNewCode creates a coded error through `NewCode` for dAppCore error handling.
// Operations, codes, roots, and crash reports remain inspectable through core errors.
func ExampleNewCode() {
err := NewCode("NOT_FOUND", "resource missing")
Println(ErrorCode(err))
Println(ErrorMessage(err))
// Output:
// NOT_FOUND
// resource missing
}
// ExampleIs matches an error through `Is` for dAppCore error handling. Operations, codes,
// roots, and crash reports remain inspectable through core errors.
func ExampleIs() {
cause := NewError("root")
err := Wrap(cause, "worker.Run", "failed")
Println(Is(err, cause))
// Output: true
}
// ExampleAs extracts a typed error through `As` for dAppCore error handling. Operations,
// codes, roots, and crash reports remain inspectable through core errors.
func ExampleAs() {
err := E("worker.Run", "failed", nil)
var target *Err
Println(As(err, &target))
Println(target.Operation)
// Output:
// true
// worker.Run
}
// ExampleNewError creates a simple error through `NewError` for dAppCore error handling.
// Operations, codes, roots, and crash reports remain inspectable through core errors.
func ExampleNewError() {
err := NewError("plain error")
Println(err.Error())
// Output: plain error
}
// ExampleErrorJoin joins errors through `ErrorJoin` for dAppCore error handling.
// Operations, codes, roots, and crash reports remain inspectable through core errors.
func ExampleErrorJoin() {
err := ErrorJoin(NewError("first"), NewError("second"))
Println(Contains(err.Error(), "first"))
Println(Contains(err.Error(), "second"))
// Output:
// true
// true
}
// ExampleOperation reads the operation from an error through `Operation` for dAppCore
// error handling. Operations, codes, roots, and crash reports remain inspectable through
// core errors.
func ExampleOperation() {
err := E("cache.Get", "missing", nil)
Println(Operation(err))
// Output: cache.Get
}
// ExampleErrorCode reads the code from an error through `ErrorCode` for dAppCore error
// handling. Operations, codes, roots, and crash reports remain inspectable through core
// errors.
func ExampleErrorCode() {
err := NewCode("NOT_FOUND", "missing")
Println(ErrorCode(err))
// Output: NOT_FOUND
}
// ExampleErrorMessage reads the message from an error through `ErrorMessage` for dAppCore
// error handling. Operations, codes, roots, and crash reports remain inspectable through
// core errors.
func ExampleErrorMessage() {
err := E("cache.Get", "missing", nil)
Println(ErrorMessage(err))
// Output: missing
}
// ExampleRoot reports the root value through `Root` for dAppCore error handling.
// Operations, codes, roots, and crash reports remain inspectable through core errors.
func ExampleRoot() {
cause := NewError("original")
wrapped := Wrap(cause, "op1", "first wrap")
double := Wrap(wrapped, "op2", "second wrap")
Println(Root(double))
// Output: original
}
// ExampleAllOperations lists wrapped operations through `AllOperations` for dAppCore error
// handling. Operations, codes, roots, and crash reports remain inspectable through core
// errors.
func ExampleAllOperations() {
err := Wrap(Wrap(NewError("root"), "db.Query", "failed"), "api.Get", "failed")
var ops []string
for op := range AllOperations(err) {
ops = append(ops, op)
}
Println(ops)
// Output: [api.Get db.Query]
}
// ExampleStackTrace captures a stack trace through `StackTrace` for dAppCore error
// handling. Operations, codes, roots, and crash reports remain inspectable through core
// errors.
func ExampleStackTrace() {
err := Wrap(Wrap(NewError("root"), "db.Query", "failed"), "api.Get", "failed")
Println(StackTrace(err))
// Output: [api.Get db.Query]
}
// ExampleFormatStackTrace formats a stack trace through `FormatStackTrace` for dAppCore
// error handling. Operations, codes, roots, and crash reports remain inspectable through
// core errors.
func ExampleFormatStackTrace() {
err := Wrap(Wrap(NewError("root"), "db.Query", "failed"), "api.Get", "failed")
Println(FormatStackTrace(err))
// Output: api.Get -> db.Query
}
// ExampleErrorLog_Error writes or renders an error through `ErrorLog.Error` for dAppCore
// error handling. Operations, codes, roots, and crash reports remain inspectable through
// core errors.
func ExampleErrorLog_Error() {
var log ErrorLog
Println(log.Error(nil, "example", "ok").OK)
// Output: true
}
// ExampleErrorLog_Warn writes a warning event through `ErrorLog.Warn` for dAppCore error
// handling. Operations, codes, roots, and crash reports remain inspectable through core
// errors.
func ExampleErrorLog_Warn() {
var log ErrorLog
Println(log.Warn(nil, "example", "ok").OK)
// Output: true
}
// ExampleErrorLog_Must unwraps a successful Result through `ErrorLog.Must` for dAppCore
// error handling. Operations, codes, roots, and crash reports remain inspectable through
// core errors.
func ExampleErrorLog_Must() {
var log ErrorLog
log.Must(nil, "example", "ok")
Println("ok")
// Output: ok
}
// ExampleCrashReport builds a crash report through `CrashReport` for dAppCore error
// handling. Operations, codes, roots, and crash reports remain inspectable through core
// errors.
func ExampleCrashReport() {
report := CrashReport{Error: "panic: boom", System: CrashSystem{OperatingSystem: "darwin"}}
Println(report.Error)
Println(report.System.OperatingSystem)
// Output:
// panic: boom
// darwin
}
// ExampleCrashSystem collects crash reports through `CrashSystem` for dAppCore error
// handling. Operations, codes, roots, and crash reports remain inspectable through core
// errors.
func ExampleCrashSystem() {
system := CrashSystem{OperatingSystem: "darwin", Architecture: "arm64", Version: "go1.26"}
Println(system.OperatingSystem)
Println(system.Architecture)
// Output:
// darwin
// arm64
}
// ExampleErrorPanic_Recover documents panic recovery when no panic is active. Operations,
// codes, roots, and crash reports remain inspectable through core errors.
func ExampleErrorPanic_Recover() {
h := &ErrorPanic{}
defer h.Recover()
}
// ExampleErrorPanic_SafeGo launches protected work that reports panics instead of crashing
// the caller. Operations, codes, roots, and crash reports remain inspectable through core
// errors.
func ExampleErrorPanic_SafeGo() {
h := &ErrorPanic{}
h.SafeGo(func() { /* no-op goroutine body demonstrates safe launch */ })
}
// ExampleErrorPanic_Reports lists captured reports through `ErrorPanic.Reports` for
// dAppCore error handling. Operations, codes, roots, and crash reports remain inspectable
// through core errors.
func ExampleErrorPanic_Reports() {
h := &ErrorPanic{}
Println(h.Reports(1).OK)
// Output: false
}