Skip to content

Commit 962d1d4

Browse files
committed
Add withErrorReporting
It's common to write the following boilerplate when working with code that can throw errors: ```swift do { try work() } catch { reportIssue(error) } ``` Let's extract this pattern to a helper that can save at least some boilerplate: ```swift withErrorReporting { try work() } ``` Or even make it a 1-liner in some cases: ```swift withErrorReporting(catching: work) ```
1 parent d502282 commit 962d1d4

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# ``IssueReporting/withErrorReporting(_:fileID:filePath:line:column:catching:)-3k8o``
2+
3+
## Topics
4+
5+
### Overloads
6+
7+
- ``withErrorReporting(_:fileID:filePath:line:column:catching:)-r4f1``

Sources/IssueReporting/Documentation.docc/IssueReporting.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ that ship in the same target as the library itself.
5353

5454
- ``reportIssue(_:fileID:filePath:line:column:)``
5555
- ``withExpectedIssue(_:isIntermittent:fileID:filePath:line:column:_:)-9pinm``
56+
- ``withErrorReporting(_:fileID:filePath:line:column:catching:)-3k8o``
5657

5758
### Issue reporters
5859

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/// Evaluates a throwing closure and automatically catches and reports any error thrown.
2+
///
3+
/// - Parameters:
4+
/// - reporters: Issue reporters to notify during the operation.
5+
/// - fileID: The source `#fileID` associated with the error reporting.
6+
/// - filePath: The source `#filePath` associated with the error reporting.
7+
/// - line: The source `#line` associated with the error reporting.
8+
/// - column: The source `#column` associated with the error reporting.
9+
/// - body: A synchronous operation.
10+
/// - Returns: The optional result of the operation, or `nil` if an error was thrown.
11+
@_transparent
12+
public func withErrorReporting<R>(
13+
_ reporters: [any IssueReporter]? = nil,
14+
fileID: StaticString = #fileID,
15+
filePath: StaticString = #filePath,
16+
line: UInt = #line,
17+
column: UInt = #column,
18+
catching body: () throws -> R
19+
) -> R? {
20+
if let reporters {
21+
return withIssueReporters(reporters) {
22+
do {
23+
return try body()
24+
} catch {
25+
reportIssue(error, fileID: fileID, filePath: filePath, line: line, column: column)
26+
return nil
27+
}
28+
}
29+
} else {
30+
do {
31+
return try body()
32+
} catch {
33+
reportIssue(error, fileID: fileID, filePath: filePath, line: line, column: column)
34+
return nil
35+
}
36+
}
37+
}
38+
39+
/// Evaluates a throwing closure and automatically catches and reports any error thrown.
40+
///
41+
/// - Parameters:
42+
/// - reporters: Issue reporters to notify during the operation.
43+
/// - fileID: The source `#fileID` associated with the error reporting.
44+
/// - filePath: The source `#filePath` associated with the error reporting.
45+
/// - line: The source `#line` associated with the error reporting.
46+
/// - column: The source `#column` associated with the error reporting.
47+
/// - body: A synchronous operation.
48+
/// - Returns: The optional result of the operation, or `nil` if an error was thrown.
49+
@_transparent
50+
public func withErrorReporting<R>(
51+
_ reporters: [any IssueReporter]? = nil,
52+
fileID: StaticString = #fileID,
53+
filePath: StaticString = #filePath,
54+
line: UInt = #line,
55+
column: UInt = #column,
56+
catching body: () async throws -> R
57+
) async -> R? {
58+
if let reporters {
59+
return await withIssueReporters(reporters) {
60+
do {
61+
return try await body()
62+
} catch {
63+
reportIssue(error, fileID: fileID, filePath: filePath, line: line, column: column)
64+
return nil
65+
}
66+
}
67+
} else {
68+
do {
69+
return try await body()
70+
} catch {
71+
reportIssue(error, fileID: fileID, filePath: filePath, line: line, column: column)
72+
return nil
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)