Skip to content

Commit 8c9509a

Browse files
committed
wip
1 parent e58fb44 commit 8c9509a

File tree

2 files changed

+105
-52
lines changed

2 files changed

+105
-52
lines changed

Sources/IssueReporting/ErrorReporting.swift

Lines changed: 80 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -52,50 +52,92 @@ public func withErrorReporting<R>(
5252
}
5353
}
5454

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-
isolation: isolated (any Actor)? = #isolation,
75-
catching body: () async throws -> sending R
76-
) async -> R? {
77-
if let reporters {
78-
return await withIssueReporters(reporters) {
55+
#if compiler(>=6)
56+
/// Evaluates a throwing closure and automatically catches and reports any error thrown.
57+
///
58+
/// - Parameters:
59+
/// - message: A message describing the expectation.
60+
/// - reporters: Issue reporters to notify during the operation.
61+
/// - fileID: The source `#fileID` associated with the error reporting.
62+
/// - filePath: The source `#filePath` associated with the error reporting.
63+
/// - line: The source `#line` associated with the error reporting.
64+
/// - column: The source `#column` associated with the error reporting.
65+
/// - isolation: The isolation associated with the error reporting.
66+
/// - body: An asynchronous operation.
67+
/// - Returns: The optional result of the operation, or `nil` if an error was thrown.
68+
@_transparent
69+
public func withErrorReporting<R>(
70+
_ message: @autoclosure () -> String? = nil,
71+
to reporters: [any IssueReporter]? = nil,
72+
fileID: StaticString = #fileID,
73+
filePath: StaticString = #filePath,
74+
line: UInt = #line,
75+
column: UInt = #column,
76+
isolation: isolated (any Actor)? = #isolation,
77+
catching body: () async throws -> sending R
78+
) async -> R? {
79+
if let reporters {
80+
return await withIssueReporters(reporters) {
81+
do {
82+
return try await body()
83+
} catch {
84+
reportIssue(
85+
error,
86+
message(),
87+
fileID: fileID,
88+
filePath: filePath,
89+
line: line,
90+
column: column
91+
)
92+
return nil
93+
}
94+
}
95+
} else {
7996
do {
8097
return try await body()
8198
} catch {
8299
reportIssue(
83-
error,
84-
message(),
85-
fileID: fileID,
86-
filePath: filePath,
87-
line: line,
88-
column: column
89-
)
100+
error, message(), fileID: fileID, filePath: filePath, line: line, column: column)
90101
return nil
91102
}
92103
}
93-
} else {
94-
do {
95-
return try await body()
96-
} catch {
97-
reportIssue(error, message(), fileID: fileID, filePath: filePath, line: line, column: column)
98-
return nil
104+
}
105+
#else
106+
@_transparent
107+
@_unsafeInheritExecutor
108+
public func withErrorReporting<R>(
109+
_ message: @autoclosure () -> String? = nil,
110+
to reporters: [any IssueReporter]? = nil,
111+
fileID: StaticString = #fileID,
112+
filePath: StaticString = #filePath,
113+
line: UInt = #line,
114+
column: UInt = #column,
115+
catching body: () async throws -> R
116+
) async -> R? {
117+
if let reporters {
118+
return await withIssueReporters(reporters) {
119+
do {
120+
return try await body()
121+
} catch {
122+
reportIssue(
123+
error,
124+
message(),
125+
fileID: fileID,
126+
filePath: filePath,
127+
line: line,
128+
column: column
129+
)
130+
return nil
131+
}
132+
}
133+
} else {
134+
do {
135+
return try await body()
136+
} catch {
137+
reportIssue(
138+
error, message(), fileID: fileID, filePath: filePath, line: line, column: column)
139+
return nil
140+
}
99141
}
100142
}
101-
}
143+
#endif

Sources/IssueReporting/IssueReporter.swift

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -183,17 +183,28 @@ public func withIssueReporters<R>(
183183
try IssueReporters.$_current.withValue(LockIsolated(reporters), operation: operation)
184184
}
185185

186-
/// Overrides the task's issue reporters for the duration of the asynchronous operation.
187-
///
188-
/// An asynchronous version of ``withIssueReporters(_:operation:)-91179``.
189-
///
190-
/// - Parameters:
191-
/// - reporters: Issue reporters to notify during the operation.
192-
/// - operation: An asynchronous operation.
193-
public func withIssueReporters<R>(
194-
_ reporters: [any IssueReporter],
195-
isolation: isolated (any Actor)? = #isolation,
196-
operation: () async throws -> R
197-
) async rethrows -> R {
198-
try await IssueReporters.$_current.withValue(LockIsolated(reporters), operation: operation)
199-
}
186+
#if compiler(>=6)
187+
/// Overrides the task's issue reporters for the duration of the asynchronous operation.
188+
///
189+
/// An asynchronous version of ``withIssueReporters(_:operation:)-91179``.
190+
///
191+
/// - Parameters:
192+
/// - reporters: Issue reporters to notify during the operation.
193+
/// - isolation: The isolation associated with the operation.
194+
/// - operation: An asynchronous operation.
195+
public func withIssueReporters<R>(
196+
_ reporters: [any IssueReporter],
197+
isolation: isolated (any Actor)? = #isolation,
198+
operation: () async throws -> R
199+
) async rethrows -> R {
200+
try await IssueReporters.$_current.withValue(LockIsolated(reporters), operation: operation)
201+
}
202+
#else
203+
@_unsafeInheritExecutor
204+
public func withIssueReporters<R>(
205+
_ reporters: [any IssueReporter],
206+
operation: () async throws -> R
207+
) async rethrows -> R {
208+
try await IssueReporters.$_current.withValue(LockIsolated(reporters), operation: operation)
209+
}
210+
#endif

0 commit comments

Comments
 (0)