@@ -9,24 +9,30 @@ import SimpleHTTPCore
99
1010/// Validates that the HTTP response status code falls within a given range.
1111public struct HTTPStatusValidatingHandler : HTTPHandler {
12+ public typealias ErrorMapper = @Sendable ( Response ) -> Error ?
13+
1214 /// The next handler in the chain.
1315 public var next : AnyHandler ?
1416
1517 /// The default acceptable status code range: 200–299.
1618 public static let defaultValidRange = 200 ..< 300
1719
1820 private let validStatusRange : Range < Int >
21+ private let mapError : ErrorMapper ?
1922
2023 /// Creates a status-validator handler.
2124 ///
2225 /// - Parameters:
2326 /// - validStatusRange: The acceptable HTTP status codes.
27+ /// - mapError: Optional mapper that produces an underlying error when the status code is invalid.
2428 /// - nextHandler: The next handler to invoke.
2529 public init (
2630 validStatusRange: Range < Int > = Self . defaultValidRange,
31+ mapError: ErrorMapper ? = nil ,
2732 nextHandler: AnyHandler ? = nil
2833 ) {
2934 self . validStatusRange = validStatusRange
35+ self . mapError = mapError
3036 self . next = nextHandler
3137 }
3238
@@ -38,7 +44,8 @@ public struct HTTPStatusValidatingHandler: HTTPHandler {
3844 throw . init(
3945 code: . invalidStatus( result. status) ,
4046 request: result. request,
41- response: result
47+ response: result,
48+ underlyingError: mapError ? ( result)
4249 )
4350 }
4451
@@ -54,8 +61,16 @@ extension HTTPHandler where Self == HTTPStatusValidatingHandler {
5461
5562 /// Factory for a status-validating handler.
5663 ///
57- /// - Parameter range: The acceptable status codes.
58- public static func httpStatusValidator( in range: Range < Int > = Self . defaultValidRange) -> Self {
59- HTTPStatusValidatingHandler ( validStatusRange: range)
64+ /// - Parameters:
65+ /// - range: The acceptable status codes.
66+ /// - mapError: Optional mapper that produces an underlying error when the status code is invalid.
67+ public static func httpStatusValidator(
68+ in range: Range < Int > = Self . defaultValidRange,
69+ mapError: Self . ErrorMapper ? = nil
70+ ) -> Self {
71+ HTTPStatusValidatingHandler (
72+ validStatusRange: range,
73+ mapError: mapError
74+ )
6075 }
6176}
0 commit comments