Skip to content

Commit d731e29

Browse files
committed
Add HTTPResponseError
1 parent 23146bc commit d731e29

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

Sources/OpenAPIRuntime/Interface/ErrorHandlingMiddleware.swift

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ public struct ErrorHandlingMiddleware: ServerMiddleware {
7474
///
7575
/// Conform your error type to this protocol to convert it to an `HTTPResponse` and ``HTTPBody``.
7676
///
77-
/// Used by ``ErrorHandlingMiddleware``.
77+
/// You must provide ``ErrorHandlingMiddleware`` to `registerHandlers` in order for
78+
/// this value to get converted to the desired HTTP response.
7879
public protocol HTTPResponseConvertible {
7980

8081
/// An HTTP status to return in the response.
@@ -96,3 +97,36 @@ extension HTTPResponseConvertible {
9697
// swift-format-ignore: AllPublicDeclarationsHaveDocumentation
9798
public var httpBody: OpenAPIRuntime.HTTPBody? { nil }
9899
}
100+
101+
/// An concrete error type that represents a desired HTTP response to be returned by the server.
102+
///
103+
/// Throw this error in your API handler to return a specific undocumented HTTP response.
104+
///
105+
/// You must provide ``ErrorHandlingMiddleware`` to `registerHandlers` in order for
106+
/// this error to get converted to the desired HTTP response.
107+
public struct HTTPResponseError: Error, HTTPResponseConvertible {
108+
109+
/// An HTTP status to return in the response.
110+
public var httpStatus: HTTPResponse.Status
111+
112+
/// The HTTP header fields of the response.
113+
public var httpHeaderFields: HTTPTypes.HTTPFields
114+
115+
/// The body of the HTTP response.
116+
public var httpBody: OpenAPIRuntime.HTTPBody?
117+
118+
/// Creates a new error.
119+
/// - Parameters:
120+
/// - httpStatus: An HTTP status to return in the response.
121+
/// - httpHeaderFields: The HTTP header fields of the response.
122+
/// - httpBody: The body of the HTTP response.
123+
public init(
124+
httpStatus: HTTPResponse.Status,
125+
httpHeaderFields: HTTPTypes.HTTPFields = [:],
126+
httpBody: OpenAPIRuntime.HTTPBody? = nil
127+
) {
128+
self.httpStatus = httpStatus
129+
self.httpHeaderFields = httpHeaderFields
130+
self.httpBody = httpBody
131+
}
132+
}

0 commit comments

Comments
 (0)