@@ -74,7 +74,8 @@ public struct ErrorHandlingMiddleware: ServerMiddleware {
74
74
///
75
75
/// Conform your error type to this protocol to convert it to an `HTTPResponse` and ``HTTPBody``.
76
76
///
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.
78
79
public protocol HTTPResponseConvertible {
79
80
80
81
/// An HTTP status to return in the response.
@@ -96,3 +97,36 @@ extension HTTPResponseConvertible {
96
97
// swift-format-ignore: AllPublicDeclarationsHaveDocumentation
97
98
public var httpBody : OpenAPIRuntime . HTTPBody ? { nil }
98
99
}
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