/
ErrorTests.swift
325 lines (280 loc) · 8.41 KB
/
ErrorTests.swift
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import XCTest
import Vapor
final class ErrorTests: XCTestCase {
func testPrintable() throws {
print(FooError.noFoo.debugDescription)
let expectedPrintable = """
Foo Error: You do not have a `foo`.
- id: FooError.noFoo
Here are some possible causes:
- You did not set the flongwaffle.
- The session ended before a `Foo` could be made.
- The universe conspires against us all.
- Computers are hard.
These suggestions could address the issue:
- You really want to use a `Bar` here.
- Take up the guitar and move to the beach.
Vapor's documentation talks about this:
- http://documentation.com/Foo
- http://documentation.com/foo/noFoo
"""
XCTAssertEqual(FooError.noFoo.debugDescription, expectedPrintable)
}
func testOmitEmptyFields() {
XCTAssertTrue(FooError.noFoo.stackOverflowQuestions.isEmpty)
XCTAssertFalse(
FooError.noFoo.debugDescription.contains("Stack Overflow")
)
}
func testReadableName() {
XCTAssertEqual(FooError.readableName, "Foo Error")
}
func testIdentifier() {
XCTAssertEqual(FooError.noFoo.identifier, "noFoo")
}
func testCausesAndSuggestions() {
XCTAssertEqual(FooError.noFoo.possibleCauses, [
"You did not set the flongwaffle.",
"The session ended before a `Foo` could be made.",
"The universe conspires against us all.",
"Computers are hard."
])
XCTAssertEqual(FooError.noFoo.suggestedFixes, [
"You really want to use a `Bar` here.",
"Take up the guitar and move to the beach."
])
XCTAssertEqual(FooError.noFoo.documentationLinks, [
"http://documentation.com/Foo",
"http://documentation.com/foo/noFoo"
])
}
func testMinimumConformance() {
let minimum = MinimumError.alpha
let description = minimum.debugDescription
let expectation = """
MinimumError: Not enabled
- id: MinimumError.alpha
"""
XCTAssertEqual(description, expectation)
}
func testErrorLogging() {
let logger = Logger(label: "codes.vapor.test")
logger.report(error: FooError.noFoo)
}
func testErrorLogging_stacktrace() {
let logger = Logger(label: "codes.vapor.test")
func foo() throws {
try bar()
}
func bar() throws {
try baz()
}
func baz() throws {
throw TestError(kind: .foo, reason: "Oops")
}
do {
try foo()
} catch {
logger.report(error: error)
}
}
func testStackTrace() {
StackTrace.isCaptureEnabled = false
XCTAssertNil(StackTrace.capture())
StackTrace.isCaptureEnabled = true
print(StackTrace.capture()!.description)
}
func testAbortError() throws {
let app = Application(.testing)
defer { app.shutdown() }
app.get("foo") { req -> String in
throw Abort(.internalServerError, reason: "Foo")
}
app.post("foo") { req -> Foo in
try req.content.decode(Foo.self)
}
struct AbortResponse: Content {
var reason: String
}
try app.test(.GET, "foo") { res in
XCTAssertEqual(res.status, .internalServerError)
let abort = try res.content.decode(AbortResponse.self)
XCTAssertEqual(abort.reason, "Foo")
}.test(.POST, "foo", beforeRequest: { req in
try req.content.encode(Foo(bar: 42))
}, afterResponse: { res in
XCTAssertEqual(res.status, .internalServerError)
let abort = try res.content.decode(AbortResponse.self)
XCTAssertEqual(abort.reason, "After decode")
})
}
func testAbortDebuggable() throws {
func foo() throws {
try bar()
}
func bar() throws {
try baz()
}
func baz() throws {
throw Abort(.internalServerError, reason: "Oops")
}
do {
try foo()
} catch let error as DebuggableError {
XCTAssertContains(error.stackTrace?.frames[0].function, "baz")
XCTAssertContains(error.stackTrace?.frames[1].function, "bar")
XCTAssertContains(error.stackTrace?.frames[2].function, "foo")
}
}
}
func XCTAssertContains(
_ haystack: String?,
_ needle: String,
file: StaticString = #file,
line: UInt = #line
) {
let file = (file)
guard let haystack = haystack else {
XCTFail("\(needle) not found in: nil", file: file, line: line)
return
}
if !haystack.contains(needle) {
XCTFail("\(needle) not found in: \(haystack)", file: file, line: line)
}
}
private struct Foo: Content {
let bar: Int
func afterDecode() throws {
throw Abort(.internalServerError, reason: "After decode")
}
}
private enum MinimumError: String, DebuggableError {
case alpha, bravo, charlie
/// The reason for the error.
/// Typical implementations will switch over `self`
/// and return a friendly `String` describing the error.
/// - note: It is most convenient that `self` be a `Swift.Error`.
///
/// Here is one way to do this:
///
/// switch self {
/// case someError:
/// return "A `String` describing what went wrong including the actual error: `Error.someError`."
/// // other cases
/// }
var reason: String {
switch self {
case .alpha:
return "Not enabled"
case .bravo:
return "Enabled, but I'm not configured"
case .charlie:
return "Broken beyond repair"
}
}
var identifier: String {
return rawValue
}
/// A `String` array describing the possible causes of the error.
/// - note: Defaults to an empty array.
/// Provide a custom implementation to give more context.
var possibleCauses: [String] {
return []
}
var suggestedFixes: [String] {
return []
}
}
private enum FooError: String, DebuggableError {
case noFoo
static var readableName: String {
return "Foo Error"
}
var identifier: String {
return rawValue
}
var reason: String {
switch self {
case .noFoo:
return "You do not have a `foo`."
}
}
var possibleCauses: [String] {
switch self {
case .noFoo:
return [
"You did not set the flongwaffle.",
"The session ended before a `Foo` could be made.",
"The universe conspires against us all.",
"Computers are hard."
]
}
}
var suggestedFixes: [String] {
switch self {
case .noFoo:
return [
"You really want to use a `Bar` here.",
"Take up the guitar and move to the beach."
]
}
}
var documentationLinks: [String] {
switch self {
case .noFoo:
return [
"http://documentation.com/Foo",
"http://documentation.com/foo/noFoo"
]
}
}
}
private struct TestError: DebuggableError {
enum Kind: String {
case foo
case bar
case baz
}
var kind: Kind
var reason: String
var source: ErrorSource?
var stackTrace: StackTrace?
init(
kind: Kind,
reason: String,
file: String = #file,
function: String = #function,
line: UInt = #line,
column: UInt = #column,
stackTrace: StackTrace? = .capture()
) {
self.kind = kind
self.reason = reason
self.source = .init(
file: file,
function: function,
line: line,
column: column
)
self.stackTrace = stackTrace
}
var identifier: String {
return kind.rawValue
}
var possibleCauses: [String] {
switch kind {
case .foo:
return ["What do you expect, you're testing errors."]
default:
return []
}
}
var suggestedFixes: [String] {
switch kind {
case .foo:
return ["Get a better keyboard to chair interface."]
default:
return []
}
}
}