-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add `withErrorReporting` It's common to write the following boilerplate when working with code that can throw errors: ```swift do { try work() } catch { reportIssue(error) } ``` Let's extract this pattern to a helper that can save at least some boilerplate: ```swift withErrorReporting { try work() } ``` Or even make it a 1-liner in some cases: ```swift withErrorReporting(catching: work) ``` * Bump Wasm CI * wip * add test * wip * Disable Wasm tests for now * wip --------- Co-authored-by: Brandon Williams <[email protected]>
- Loading branch information
1 parent
d502282
commit adadb00
Showing
5 changed files
with
140 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
Sources/IssueReporting/Documentation.docc/Extensions/withErrorReporting.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# ``IssueReporting/withErrorReporting(_:to:fileID:filePath:line:column:catching:)-89omf`` | ||
|
||
## Topics | ||
|
||
### Overloads | ||
|
||
- ``withErrorReporting(_:to:fileID:filePath:line:column:catching:)-3dh1h`` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/// Evaluates a throwing closure and automatically catches and reports any error thrown. | ||
/// | ||
/// - Parameters: | ||
/// - message: A message describing the expectation. | ||
/// - reporters: Issue reporters to notify during the operation. | ||
/// - fileID: The source `#fileID` associated with the error reporting. | ||
/// - filePath: The source `#filePath` associated with the error reporting. | ||
/// - line: The source `#line` associated with the error reporting. | ||
/// - column: The source `#column` associated with the error reporting. | ||
/// - body: A synchronous operation. | ||
/// - Returns: The optional result of the operation, or `nil` if an error was thrown. | ||
@_transparent | ||
public func withErrorReporting<R>( | ||
_ message: @autoclosure () -> String? = nil, | ||
to reporters: [any IssueReporter]? = nil, | ||
fileID: StaticString = #fileID, | ||
filePath: StaticString = #filePath, | ||
line: UInt = #line, | ||
column: UInt = #column, | ||
catching body: () throws -> R | ||
) -> R? { | ||
if let reporters { | ||
return withIssueReporters(reporters) { | ||
do { | ||
return try body() | ||
} catch { | ||
reportIssue( | ||
error, | ||
message(), | ||
fileID: fileID, | ||
filePath: filePath, | ||
line: line, | ||
column: column | ||
) | ||
return nil | ||
} | ||
} | ||
} else { | ||
do { | ||
return try body() | ||
} catch { | ||
reportIssue( | ||
error, | ||
message(), | ||
fileID: fileID, | ||
filePath: filePath, | ||
line: line, | ||
column: column | ||
) | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
/// Evaluates a throwing closure and automatically catches and reports any error thrown. | ||
/// | ||
/// - Parameters: | ||
/// - message: A message describing the expectation. | ||
/// - reporters: Issue reporters to notify during the operation. | ||
/// - fileID: The source `#fileID` associated with the error reporting. | ||
/// - filePath: The source `#filePath` associated with the error reporting. | ||
/// - line: The source `#line` associated with the error reporting. | ||
/// - column: The source `#column` associated with the error reporting. | ||
/// - body: An asynchronous operation. | ||
/// - Returns: The optional result of the operation, or `nil` if an error was thrown. | ||
@_transparent | ||
public func withErrorReporting<R>( | ||
_ message: @autoclosure () -> String? = nil, | ||
to reporters: [any IssueReporter]? = nil, | ||
fileID: StaticString = #fileID, | ||
filePath: StaticString = #filePath, | ||
line: UInt = #line, | ||
column: UInt = #column, | ||
catching body: () async throws -> R | ||
) async -> R? { | ||
if let reporters { | ||
return await withIssueReporters(reporters) { | ||
do { | ||
return try await body() | ||
} catch { | ||
reportIssue(error, fileID: fileID, filePath: filePath, line: line, column: column) | ||
return nil | ||
} | ||
} | ||
} else { | ||
do { | ||
return try await body() | ||
} catch { | ||
reportIssue(error, fileID: fileID, filePath: filePath, line: line, column: column) | ||
return nil | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#if canImport(Testing) | ||
import Testing | ||
import IssueReporting | ||
|
||
@Suite | ||
struct WithErrorReportingTests { | ||
@Test func basics() { | ||
withKnownIssue { | ||
withErrorReporting { | ||
throw SomeError() | ||
} | ||
} matching: { issue in | ||
issue.description == "Caught error: SomeError()" | ||
} | ||
|
||
withKnownIssue { | ||
withErrorReporting("Failed") { | ||
throw SomeError() | ||
} | ||
} matching: { issue in | ||
issue.description == "Caught error: SomeError(): Failed" | ||
} | ||
} | ||
} | ||
|
||
private struct SomeError: Error {} | ||
#endif |