Skip to content

Commit fcfd214

Browse files
committed
Simplfiy and added tests
1 parent ff4c8cd commit fcfd214

File tree

2 files changed

+40
-74
lines changed

2 files changed

+40
-74
lines changed

Sources/IssueReporting/ErrorReporting.swift

Lines changed: 18 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -77,45 +77,17 @@ public func withErrorReporting<R>(
7777
column: UInt = #column,
7878
catching body: () throws -> R?
7979
) -> R? {
80-
if let reporters {
81-
return withIssueReporters(reporters) {
82-
do {
83-
if let body = try body() {
84-
return body
85-
} else {
86-
return nil
87-
}
88-
} catch {
89-
reportIssue(
90-
error,
91-
message(),
92-
fileID: fileID,
93-
filePath: filePath,
94-
line: line,
95-
column: column
96-
)
97-
return nil
98-
}
99-
}
100-
} else {
101-
do {
102-
if let body = try body() {
103-
return body
104-
} else {
105-
return nil
106-
}
107-
} catch {
108-
reportIssue(
109-
error,
110-
message(),
111-
fileID: fileID,
112-
filePath: filePath,
113-
line: line,
114-
column: column
115-
)
116-
return nil
117-
}
118-
}
80+
(
81+
withErrorReporting(
82+
message(),
83+
to: reporters,
84+
fileID: fileID,
85+
filePath: filePath,
86+
line: line,
87+
column: column,
88+
catching: body
89+
) as R??
90+
) ?? nil
11991
}
12092

12193
#if compiler(>=6)
@@ -200,46 +172,18 @@ public func withErrorReporting<R>(
200172
// DO NOT FIX THE WHITESPACE IN THE NEXT LINE UNTIL 5.10 IS UNSUPPORTED
201173
// https://github.com/swiftlang/swift/issues/79285
202174
catching body: () async throws -> sending R?) async -> R? {
203-
if let reporters {
204-
return await withIssueReporters(reporters) {
205-
do {
206-
if let body = try body() {
207-
return body
208-
} else {
209-
return nil
210-
}
211-
} catch {
212-
reportIssue(
213-
error,
214-
message(),
215-
fileID: fileID,
216-
filePath: filePath,
217-
line: line,
218-
column: column
219-
)
220-
return nil
221-
}
222-
}
223-
} else {
224-
do {
225-
if let body = try body() {
226-
return body
227-
} else {
228-
return nil
229-
}
230-
} catch {
231-
reportIssue(
232-
error,
175+
(
176+
await withErrorReporting(
233177
message(),
178+
to: reporters,
234179
fileID: fileID,
235180
filePath: filePath,
236181
line: line,
237-
column: column
238-
)
239-
return nil
240-
}
182+
column: column,
183+
catching: body
184+
) as R??
185+
) ?? nil
241186
}
242-
}
243187
#else
244188
@_transparent
245189
@_unsafeInheritExecutor

Tests/IssueReportingTests/WithErrorReportingTests.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,28 @@
4040
}
4141
}
4242

43+
@Test func squashOptionalSync() async {
44+
withKnownIssue {
45+
let _: Int? = withErrorReporting { () -> Int? in
46+
throw SomeError()
47+
}
48+
} matching: { issue in
49+
issue.description == "Caught error: SomeError()\(issueDescriptionSuffix)"
50+
}
51+
}
52+
53+
54+
@Test func squashOptionalAsync() async {
55+
await withKnownIssue {
56+
let _: Int? = await withErrorReporting { () -> Int? in
57+
await Task.yield()
58+
throw SomeError()
59+
}
60+
} matching: { issue in
61+
issue.description == "Caught error: SomeError()\(issueDescriptionSuffix)"
62+
}
63+
}
64+
4365
#if compiler(<6.2)
4466
@MainActor
4567
@Test func isolation() async {

0 commit comments

Comments
 (0)