Skip to content

Commit f1c0bb0

Browse files
authored
Don't report CancellationError from withErrorReporting (#173)
* Don't report `CancellationError` from `withErrorReporting` It's not super helpful and can cause a lot of noise in code that can be terminated from cancellation. * test
1 parent fa72d0a commit f1c0bb0

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

.github/workflows/format.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ concurrency:
1212
jobs:
1313
swift_format:
1414
name: swift-format
15-
runs-on: macos-14
15+
runs-on: macos-26
1616
steps:
17-
- uses: actions/checkout@v4
17+
- uses: actions/checkout@v5
1818
- name: Xcode Select
19-
run: sudo xcode-select -s /Applications/Xcode_15.4.app
19+
run: sudo xcode-select -s /Applications/Xcode_26.1.app
2020
- name: Install
2121
run: brew install swift-format
2222
- name: Format

Sources/IssueReporting/ErrorReporting.swift

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
// swift-format-ignore-file
2-
// Note: Whitespace changes are used to workaround compiler bug
3-
// https://github.com/swiftlang/swift/issues/79285
4-
51
/// Evaluates a throwing closure and automatically catches and reports any error thrown.
62
///
73
/// - Parameters:
@@ -27,6 +23,7 @@ public func withErrorReporting<R>(
2723
return withIssueReporters(reporters) {
2824
do {
2925
return try body()
26+
} catch is CancellationError {
3027
} catch {
3128
reportIssue(
3229
error,
@@ -36,12 +33,13 @@ public func withErrorReporting<R>(
3633
line: line,
3734
column: column
3835
)
39-
return nil
4036
}
37+
return nil
4138
}
4239
} else {
4340
do {
4441
return try body()
42+
} catch is CancellationError {
4543
} catch {
4644
reportIssue(
4745
error,
@@ -51,8 +49,8 @@ public func withErrorReporting<R>(
5149
line: line,
5250
column: column
5351
)
54-
return nil
5552
}
53+
return nil
5654
}
5755
}
5856

@@ -110,13 +108,13 @@ public func withErrorReporting<R>(
110108
line: UInt = #line,
111109
column: UInt = #column,
112110
isolation: isolated (any Actor)? = #isolation,
113-
// DO NOT FIX THE WHITESPACE IN THE NEXT LINE UNTIL 5.10 IS UNSUPPORTED
114-
// https://github.com/swiftlang/swift/issues/79285
115-
catching body: () async throws -> sending R) async -> R? {
111+
catching body: () async throws -> sending R
112+
) async -> R? {
116113
if let reporters {
117114
return await withIssueReporters(reporters) {
118115
do {
119116
return try await body()
117+
} catch is CancellationError {
120118
} catch {
121119
reportIssue(
122120
error,
@@ -126,12 +124,13 @@ public func withErrorReporting<R>(
126124
line: line,
127125
column: column
128126
)
129-
return nil
130127
}
128+
return nil
131129
}
132130
} else {
133131
do {
134132
return try await body()
133+
} catch is CancellationError {
135134
} catch {
136135
reportIssue(
137136
error,
@@ -141,8 +140,8 @@ public func withErrorReporting<R>(
141140
line: line,
142141
column: column
143142
)
144-
return nil
145143
}
144+
return nil
146145
}
147146
}
148147

@@ -167,8 +166,6 @@ public func withErrorReporting<R>(
167166
line: UInt = #line,
168167
column: UInt = #column,
169168
isolation: isolated (any Actor)? = #isolation,
170-
// DO NOT FIX THE WHITESPACE IN THE NEXT LINE UNTIL 5.10 IS UNSUPPORTED
171-
// https://github.com/swiftlang/swift/issues/79285
172169
catching body: () async throws -> sending R?
173170
) async -> R? {
174171
(await withErrorReporting(
@@ -197,6 +194,7 @@ public func withErrorReporting<R>(
197194
return await withIssueReporters(reporters) {
198195
do {
199196
return try await body()
197+
} catch is CancellationError {
200198
} catch {
201199
reportIssue(
202200
error,
@@ -206,12 +204,13 @@ public func withErrorReporting<R>(
206204
line: line,
207205
column: column
208206
)
209-
return nil
210207
}
208+
return nil
211209
}
212210
} else {
213211
do {
214212
return try await body()
213+
} catch is CancellationError {
215214
} catch {
216215
reportIssue(
217216
error,
@@ -221,8 +220,8 @@ public func withErrorReporting<R>(
221220
line: line,
222221
column: column
223222
)
224-
return nil
225223
}
224+
return nil
226225
}
227226
}
228227

Tests/IssueReportingTests/WithErrorReportingTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@
6161
}
6262
}
6363

64+
@Test func cancellation() async {
65+
await withErrorReporting {
66+
await Task.yield()
67+
throw CancellationError()
68+
}
69+
}
70+
6471
#if compiler(<6.2)
6572
@MainActor
6673
@Test func isolation() async {

0 commit comments

Comments
 (0)