Skip to content

Commit ff4c8cd

Browse files
committed
Prevent double optional return value
1 parent b2ed9ea commit ff4c8cd

File tree

1 file changed

+179
-0
lines changed

1 file changed

+179
-0
lines changed

Sources/IssueReporting/ErrorReporting.swift

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,68 @@ public func withErrorReporting<R>(
5656
}
5757
}
5858

59+
/// Evaluates a throwing closure and automatically catches and reports any error thrown.
60+
///
61+
/// - Parameters:
62+
/// - message: A message describing the expectation.
63+
/// - reporters: Issue reporters to notify during the operation.
64+
/// - fileID: The source `#fileID` associated with the error reporting.
65+
/// - filePath: The source `#filePath` associated with the error reporting.
66+
/// - line: The source `#line` associated with the error reporting.
67+
/// - column: The source `#column` associated with the error reporting.
68+
/// - body: A synchronous operation.
69+
/// - Returns: The optional result of the operation, or `nil` if an error was thrown.
70+
@_transparent
71+
public func withErrorReporting<R>(
72+
_ message: @autoclosure () -> String? = nil,
73+
to reporters: [any IssueReporter]? = nil,
74+
fileID: StaticString = #fileID,
75+
filePath: StaticString = #filePath,
76+
line: UInt = #line,
77+
column: UInt = #column,
78+
catching body: () throws -> R?
79+
) -> 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+
}
119+
}
120+
59121
#if compiler(>=6)
60122
/// Evaluates a throwing closure and automatically catches and reports any error thrown.
61123
///
@@ -113,6 +175,71 @@ public func withErrorReporting<R>(
113175
}
114176
}
115177
}
178+
179+
/// Evaluates a throwing closure and automatically catches and reports any error thrown.
180+
///
181+
/// - Parameters:
182+
/// - message: A message describing the expectation.
183+
/// - reporters: Issue reporters to notify during the operation.
184+
/// - fileID: The source `#fileID` associated with the error reporting.
185+
/// - filePath: The source `#filePath` associated with the error reporting.
186+
/// - line: The source `#line` associated with the error reporting.
187+
/// - column: The source `#column` associated with the error reporting.
188+
/// - isolation: The isolation associated with the error reporting.
189+
/// - body: An asynchronous operation.
190+
/// - Returns: The optional result of the operation, or `nil` if an error was thrown.
191+
@_transparent
192+
public func withErrorReporting<R>(
193+
_ message: @autoclosure () -> String? = nil,
194+
to reporters: [any IssueReporter]? = nil,
195+
fileID: StaticString = #fileID,
196+
filePath: StaticString = #filePath,
197+
line: UInt = #line,
198+
column: UInt = #column,
199+
isolation: isolated (any Actor)? = #isolation,
200+
// DO NOT FIX THE WHITESPACE IN THE NEXT LINE UNTIL 5.10 IS UNSUPPORTED
201+
// https://github.com/swiftlang/swift/issues/79285
202+
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,
233+
message(),
234+
fileID: fileID,
235+
filePath: filePath,
236+
line: line,
237+
column: column
238+
)
239+
return nil
240+
}
241+
}
242+
}
116243
#else
117244
@_transparent
118245
@_unsafeInheritExecutor
@@ -157,4 +284,56 @@ public func withErrorReporting<R>(
157284
}
158285
}
159286
}
287+
288+
@_transparent
289+
@_unsafeInheritExecutor
290+
public func withErrorReporting<R>(
291+
_ message: @autoclosure () -> String? = nil,
292+
to reporters: [any IssueReporter]? = nil,
293+
fileID: StaticString = #fileID,
294+
filePath: StaticString = #filePath,
295+
line: UInt = #line,
296+
column: UInt = #column,
297+
catching body: () async throws -> R?
298+
) async -> R? {
299+
if let reporters {
300+
return await withIssueReporters(reporters) {
301+
do {
302+
if let body = try body() {
303+
return body
304+
} else {
305+
return nil
306+
}
307+
} catch {
308+
reportIssue(
309+
error,
310+
message(),
311+
fileID: fileID,
312+
filePath: filePath,
313+
line: line,
314+
column: column
315+
)
316+
return nil
317+
}
318+
}
319+
} else {
320+
do {
321+
if let body = try body() {
322+
return body
323+
} else {
324+
return nil
325+
}
326+
} catch {
327+
reportIssue(
328+
error,
329+
message(),
330+
fileID: fileID,
331+
filePath: filePath,
332+
line: line,
333+
column: column
334+
)
335+
return nil
336+
}
337+
}
338+
}
160339
#endif

0 commit comments

Comments
 (0)