Skip to content

Commit adadb00

Browse files
Add withErrorReporting (#143)
* 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) ``` * Bump Wasm CI * wip * add test * wip * Disable Wasm tests for now * wip --------- Co-authored-by: Brandon Williams <mbrandonw@hey.com>
1 parent d502282 commit adadb00

File tree

5 files changed

+140
-18
lines changed

5 files changed

+140
-18
lines changed

.github/workflows/ci.yml

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -95,28 +95,22 @@ jobs:
9595
run: make CONFIG=${{ matrix.config }} build-for-static-stdlib
9696

9797
wasm:
98-
name: SwiftWasm
98+
name: Wasm
9999
runs-on: ubuntu-latest
100-
strategy:
101-
matrix:
102-
toolchain:
103-
- wasm-5.9.2-RELEASE
104-
- wasm-5.10.0-RELEASE
105100
steps:
106-
- name: Cache toolchains
107-
uses: actions/cache@v3
108-
with:
109-
path: ~/Library/Developer/Toolchains
110-
key: ${{ matrix.toolchain }}
111101
- uses: actions/checkout@v4
112102
- uses: bytecodealliance/actions/wasmtime/setup@v1
113-
- uses: swiftwasm/setup-swiftwasm@v1
114-
with:
115-
swift-version: ${{ matrix.toolchain }}
116-
- name: Build tests
117-
run: swift build --triple wasm32-unknown-wasi --build-tests -Xlinker -z -Xlinker stack-size=$((1024 * 1024))
118-
- name: Run tests
119-
run: wasmtime .build/debug/xctest-dynamic-overlayPackageTests.wasm
103+
- name: Install Swift and Swift SDK for WebAssembly
104+
run: |
105+
PREFIX=/opt/swift
106+
set -ex
107+
curl -f -o /tmp/swift.tar.gz "https://download.swift.org/swift-6.0.2-release/ubuntu2204/swift-6.0.2-RELEASE/swift-6.0.2-RELEASE-ubuntu22.04.tar.gz"
108+
sudo mkdir -p $PREFIX; sudo tar -xzf /tmp/swift.tar.gz -C $PREFIX --strip-component 1
109+
$PREFIX/usr/bin/swift sdk install https://github.com/swiftwasm/swift/releases/download/swift-wasm-6.0.2-RELEASE/swift-wasm-6.0.2-RELEASE-wasm32-unknown-wasi.artifactbundle.zip --checksum 6ffedb055cb9956395d9f435d03d53ebe9f6a8d45106b979d1b7f53358e1dcb4
110+
echo "$PREFIX/usr/bin" >> $GITHUB_PATH
111+
112+
- name: Build
113+
run: swift build --swift-sdk wasm32-unknown-wasi --build-tests -Xlinker -z -Xlinker stack-size=$((1024 * 1024))
120114

121115
windows:
122116
name: Windows
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# ``IssueReporting/withErrorReporting(_:to:fileID:filePath:line:column:catching:)-89omf``
2+
3+
## Topics
4+
5+
### Overloads
6+
7+
- ``withErrorReporting(_:to:fileID:filePath:line:column:catching:)-3dh1h``

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(_:to:fileID:filePath:line:column:catching:)-89omf``
5657

5758
### Issue reporters
5859

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/// Evaluates a throwing closure and automatically catches and reports any error thrown.
2+
///
3+
/// - Parameters:
4+
/// - message: A message describing the expectation.
5+
/// - reporters: Issue reporters to notify during the operation.
6+
/// - fileID: The source `#fileID` associated with the error reporting.
7+
/// - filePath: The source `#filePath` associated with the error reporting.
8+
/// - line: The source `#line` associated with the error reporting.
9+
/// - column: The source `#column` associated with the error reporting.
10+
/// - body: A synchronous operation.
11+
/// - Returns: The optional result of the operation, or `nil` if an error was thrown.
12+
@_transparent
13+
public func withErrorReporting<R>(
14+
_ message: @autoclosure () -> String? = nil,
15+
to reporters: [any IssueReporter]? = nil,
16+
fileID: StaticString = #fileID,
17+
filePath: StaticString = #filePath,
18+
line: UInt = #line,
19+
column: UInt = #column,
20+
catching body: () throws -> R
21+
) -> R? {
22+
if let reporters {
23+
return withIssueReporters(reporters) {
24+
do {
25+
return try body()
26+
} catch {
27+
reportIssue(
28+
error,
29+
message(),
30+
fileID: fileID,
31+
filePath: filePath,
32+
line: line,
33+
column: column
34+
)
35+
return nil
36+
}
37+
}
38+
} else {
39+
do {
40+
return try body()
41+
} catch {
42+
reportIssue(
43+
error,
44+
message(),
45+
fileID: fileID,
46+
filePath: filePath,
47+
line: line,
48+
column: column
49+
)
50+
return nil
51+
}
52+
}
53+
}
54+
55+
/// Evaluates a throwing closure and automatically catches and reports any error thrown.
56+
///
57+
/// - Parameters:
58+
/// - message: A message describing the expectation.
59+
/// - reporters: Issue reporters to notify during the operation.
60+
/// - fileID: The source `#fileID` associated with the error reporting.
61+
/// - filePath: The source `#filePath` associated with the error reporting.
62+
/// - line: The source `#line` associated with the error reporting.
63+
/// - column: The source `#column` associated with the error reporting.
64+
/// - body: An asynchronous operation.
65+
/// - Returns: The optional result of the operation, or `nil` if an error was thrown.
66+
@_transparent
67+
public func withErrorReporting<R>(
68+
_ message: @autoclosure () -> String? = nil,
69+
to reporters: [any IssueReporter]? = nil,
70+
fileID: StaticString = #fileID,
71+
filePath: StaticString = #filePath,
72+
line: UInt = #line,
73+
column: UInt = #column,
74+
catching body: () async throws -> R
75+
) async -> R? {
76+
if let reporters {
77+
return await withIssueReporters(reporters) {
78+
do {
79+
return try await body()
80+
} catch {
81+
reportIssue(error, fileID: fileID, filePath: filePath, line: line, column: column)
82+
return nil
83+
}
84+
}
85+
} else {
86+
do {
87+
return try await body()
88+
} catch {
89+
reportIssue(error, fileID: fileID, filePath: filePath, line: line, column: column)
90+
return nil
91+
}
92+
}
93+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#if canImport(Testing)
2+
import Testing
3+
import IssueReporting
4+
5+
@Suite
6+
struct WithErrorReportingTests {
7+
@Test func basics() {
8+
withKnownIssue {
9+
withErrorReporting {
10+
throw SomeError()
11+
}
12+
} matching: { issue in
13+
issue.description == "Caught error: SomeError()"
14+
}
15+
16+
withKnownIssue {
17+
withErrorReporting("Failed") {
18+
throw SomeError()
19+
}
20+
} matching: { issue in
21+
issue.description == "Caught error: SomeError(): Failed"
22+
}
23+
}
24+
}
25+
26+
private struct SomeError: Error {}
27+
#endif

0 commit comments

Comments
 (0)