|
| 1 | +/// Evaluates a throwing closure and automatically catches and reports any error thrown. |
| 2 | +/// |
| 3 | +/// - Parameters: |
| 4 | +/// - reporters: Issue reporters to notify during the operation. |
| 5 | +/// - fileID: The source `#fileID` associated with the error reporting. |
| 6 | +/// - filePath: The source `#filePath` associated with the error reporting. |
| 7 | +/// - line: The source `#line` associated with the error reporting. |
| 8 | +/// - column: The source `#column` associated with the error reporting. |
| 9 | +/// - body: A synchronous operation. |
| 10 | +/// - Returns: The optional result of the operation, or `nil` if an error was thrown. |
| 11 | +@_transparent |
| 12 | +public func withErrorReporting<R>( |
| 13 | + _ reporters: [any IssueReporter]? = nil, |
| 14 | + fileID: StaticString = #fileID, |
| 15 | + filePath: StaticString = #filePath, |
| 16 | + line: UInt = #line, |
| 17 | + column: UInt = #column, |
| 18 | + catching body: () throws -> R |
| 19 | +) -> R? { |
| 20 | + if let reporters { |
| 21 | + return withIssueReporters(reporters) { |
| 22 | + do { |
| 23 | + return try body() |
| 24 | + } catch { |
| 25 | + reportIssue(error, fileID: fileID, filePath: filePath, line: line, column: column) |
| 26 | + return nil |
| 27 | + } |
| 28 | + } |
| 29 | + } else { |
| 30 | + do { |
| 31 | + return try body() |
| 32 | + } catch { |
| 33 | + reportIssue(error, fileID: fileID, filePath: filePath, line: line, column: column) |
| 34 | + return nil |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +/// Evaluates a throwing closure and automatically catches and reports any error thrown. |
| 40 | +/// |
| 41 | +/// - Parameters: |
| 42 | +/// - reporters: Issue reporters to notify during the operation. |
| 43 | +/// - fileID: The source `#fileID` associated with the error reporting. |
| 44 | +/// - filePath: The source `#filePath` associated with the error reporting. |
| 45 | +/// - line: The source `#line` associated with the error reporting. |
| 46 | +/// - column: The source `#column` associated with the error reporting. |
| 47 | +/// - body: A synchronous operation. |
| 48 | +/// - Returns: The optional result of the operation, or `nil` if an error was thrown. |
| 49 | +@_transparent |
| 50 | +public func withErrorReporting<R>( |
| 51 | + _ reporters: [any IssueReporter]? = nil, |
| 52 | + fileID: StaticString = #fileID, |
| 53 | + filePath: StaticString = #filePath, |
| 54 | + line: UInt = #line, |
| 55 | + column: UInt = #column, |
| 56 | + catching body: () async throws -> R |
| 57 | +) async -> R? { |
| 58 | + if let reporters { |
| 59 | + return await withIssueReporters(reporters) { |
| 60 | + do { |
| 61 | + return try await body() |
| 62 | + } catch { |
| 63 | + reportIssue(error, fileID: fileID, filePath: filePath, line: line, column: column) |
| 64 | + return nil |
| 65 | + } |
| 66 | + } |
| 67 | + } else { |
| 68 | + do { |
| 69 | + return try await body() |
| 70 | + } catch { |
| 71 | + reportIssue(error, fileID: fileID, filePath: filePath, line: line, column: column) |
| 72 | + return nil |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments