Skip to content

Commit a6f7323

Browse files
Apply suggestions from code review
Co-authored-by: Honza Dvorsky <[email protected]>
1 parent 8ccb19e commit a6f7323

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

Sources/OpenAPIRuntime/Interface/ErrorHandlingMiddleware.swift

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import HTTPTypes
2020
/// Errors not conforming to ``HTTPResponseConvertible`` are converted to a response with the 500 status code.
2121
///
2222
/// ## Example usage
23+
///
2324
/// 1. Create an error type that conforms to the ``HTTPResponseConvertible`` protocol:
2425
///
2526
/// ```swift
@@ -35,28 +36,19 @@ import HTTPTypes
3536
/// }
3637
/// ```
3738
///
38-
/// 2. Opt in to the ``ErrorHandlingMiddleware`` while registering the handler:
39+
/// 2. Opt into the ``ErrorHandlingMiddleware`` while registering the handler:
3940
///
4041
/// ```swift
4142
/// let handler = RequestHandler()
4243
/// try handler.registerHandlers(on: transport, middlewares: [ErrorHandlingMiddleware()])
4344
/// ```
44-
/// - Note: The placement of ``ErrorHandlingMiddleware`` in the middleware chain is important.
45-
/// It should be determined based on the specific needs of each application.
46-
/// Consider the order of execution and dependencies between middlewares.
45+
/// - Note: The placement of ``ErrorHandlingMiddleware`` in the middleware chain is important. It should be determined based on the specific needs of each application. Consider the order of execution and dependencies between middlewares.
4746
public struct ErrorHandlingMiddleware: ServerMiddleware {
4847

49-
/// Creates an ErrorHandlingMiddleware.
48+
/// Creates a new middleware.
5049
public init() {}
5150

52-
/// Intercepts an outgoing HTTP request and an incoming HTTP Response, and converts errors(if any) that confirms to ``HTTPResponseConvertible`` to an HTTP response.
53-
/// - Parameters:
54-
/// - request: The HTTP request created during the operation.
55-
/// - body: The HTTP request body created during the operation.
56-
/// - metadata: Request metadata
57-
/// - operationID: The OpenAPI operation identifier.
58-
/// - next: A closure that calls the next middleware, or the transport.
59-
/// - Returns: An HTTPResponse and an optional HTTPBody.
51+
// swift-format-ignore: AllPublicDeclarationsHaveDocumentation
6052
public func intercept(
6153
_ request: HTTPTypes.HTTPRequest,
6254
body: OpenAPIRuntime.HTTPBody?,
@@ -78,21 +70,29 @@ public struct ErrorHandlingMiddleware: ServerMiddleware {
7870
}
7971
}
8072

81-
/// A protocol used by ``ErrorHandlingMiddleware`` to map an error to an ``HTTPResponse`` and ``HTTPBody``.
82-
/// Adopters who wish to convert their application error to an `HTTPResponse` and ``HTTPBody`` should conform the error type to this protocol.
73+
/// A value that can be converted to an HTTP response and body.
74+
///
75+
/// Conform your error type to this protocol to convert it to an `HTTPResponse` and ``HTTPBody``.
76+
///
77+
/// Used by ``ErrorHandlingMiddleware``.
8378
public protocol HTTPResponseConvertible {
8479

8580
/// An HTTP status to return in the response.
8681
var httpStatus: HTTPResponse.Status { get }
87-
/// The HTTP headers of the response.
82+
83+
/// The HTTP header fields of the response.
8884
/// This is optional as default values are provided in the extension.
8985
var httpHeaderFields: HTTPTypes.HTTPFields { get }
86+
9087
/// The body of the HTTP response.
9188
var httpBody: OpenAPIRuntime.HTTPBody? { get }
9289
}
9390

94-
/// Extension to HTTPResponseConvertible to provide default values for certain fields.
9591
extension HTTPResponseConvertible {
92+
93+
// swift-format-ignore: AllPublicDeclarationsHaveDocumentation
9694
public var httpHeaderFields: HTTPTypes.HTTPFields { [:] }
95+
96+
// swift-format-ignore: AllPublicDeclarationsHaveDocumentation
9797
public var httpBody: OpenAPIRuntime.HTTPBody? { nil }
9898
}

Tests/OpenAPIRuntimeTests/Interface/Test_ErrorHandlingMiddleware.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ final class Test_ErrorHandlingMiddlewareTests: XCTestCase {
3232
)
3333
XCTAssertEqual(response.0.status, .ok)
3434
}
35-
func testError_confirmingToProtocol_convertedToResponse() async throws {
35+
36+
func testError_conformingToProtocol_convertedToResponse() async throws {
3637
let (response, responseBody) = try await Test_ErrorHandlingMiddlewareTests.errorHandlingMiddleware.intercept(
3738
Test_ErrorHandlingMiddlewareTests.mockRequest,
3839
body: Test_ErrorHandlingMiddlewareTests.mockBody,
@@ -45,7 +46,7 @@ final class Test_ErrorHandlingMiddlewareTests: XCTestCase {
4546
XCTAssertEqual(responseBody, TEST_HTTP_BODY)
4647
}
4748

48-
func testError_confirmingToProtocolWithoutAllValues_convertedToResponse() async throws {
49+
func testError_conformingToProtocolWithoutAllValues_convertedToResponse() async throws {
4950
let (response, responseBody) = try await Test_ErrorHandlingMiddlewareTests.errorHandlingMiddleware.intercept(
5051
Test_ErrorHandlingMiddlewareTests.mockRequest,
5152
body: Test_ErrorHandlingMiddlewareTests.mockBody,
@@ -57,7 +58,8 @@ final class Test_ErrorHandlingMiddlewareTests: XCTestCase {
5758
XCTAssertEqual(response.headerFields, [:])
5859
XCTAssertEqual(responseBody, nil)
5960
}
60-
func testError_notConfirmingToProtocol_returns500() async throws {
61+
62+
func testError_notConformingToProtocol_returns500() async throws {
6163
let (response, responseBody) = try await Test_ErrorHandlingMiddlewareTests.errorHandlingMiddleware.intercept(
6264
Test_ErrorHandlingMiddlewareTests.mockRequest,
6365
body: Test_ErrorHandlingMiddlewareTests.mockBody,
@@ -69,6 +71,7 @@ final class Test_ErrorHandlingMiddlewareTests: XCTestCase {
6971
XCTAssertEqual(response.headerFields, [:])
7072
XCTAssertEqual(responseBody, nil)
7173
}
74+
7275
private func getNextMiddleware(failurePhase: MockErrorMiddleware_Next.FailurePhase) -> @Sendable (
7376
HTTPTypes.HTTPRequest, OpenAPIRuntime.HTTPBody?, OpenAPIRuntime.ServerRequestMetadata
7477
) async throws -> (HTTPTypes.HTTPResponse, OpenAPIRuntime.HTTPBody?) {

0 commit comments

Comments
 (0)