@@ -32,7 +32,7 @@ public extension HTTPResponseValidator {
3232 static func statusCode( _ codes: ClosedRange < Int > ) -> Self {
3333 HTTPResponseValidator { response, _, configs in
3434 guard codes. contains ( response. status. code) || configs. ignoreStatusCodeValidator else {
35- throw InvalidStatusCode ( response. status )
35+ throw InvalidStatusCode ( response: response )
3636 }
3737 }
3838 }
@@ -43,25 +43,32 @@ public extension HTTPResponseValidator {
4343 static func statusCode( _ kind: HTTPResponse . Status . Kind ) -> Self {
4444 HTTPResponseValidator { response, _, configs in
4545 guard response. status. kind == kind || configs. ignoreStatusCodeValidator else {
46- throw InvalidStatusCode ( response. status )
46+ throw InvalidStatusCode ( response: response )
4747 }
4848 }
4949 }
5050}
5151
52+ /// An error indicating that an HTTP response has an invalid status code.
5253public struct InvalidStatusCode : Error , LocalizedError , CustomStringConvertible {
53-
54- /// The invalid status code.
55- public let status : HTTPResponse . Status
56-
57- public init ( _ status: HTTPResponse . Status ) {
58- self . status = status
59- }
60-
61- public var errorDescription : String ? { description }
62- public var description : String {
63- " Invalid status code: \( status. code) \( status. reasonPhrase) "
64- }
54+
55+ /// The invalid status code.
56+ public var status : HTTPResponse . Status { response. status }
57+ public let response : HTTPResponse
58+
59+ @available ( * , deprecated, message: " Use init(response: HTTPResponse) instead " )
60+ public init ( _ status: HTTPResponse . Status ) {
61+ self . init ( response: HTTPResponse ( status: status) )
62+ }
63+
64+ public init ( response: HTTPResponse ) {
65+ self . response = response
66+ }
67+
68+ public var errorDescription : String ? { description }
69+ public var description : String {
70+ " Invalid status code: \( status. code) \( status. reasonPhrase) "
71+ }
6572}
6673
6774public extension HTTPResponseValidator {
@@ -107,34 +114,30 @@ public extension APIClient.Configs {
107114 }
108115}
109116
110- func extractStatusCodeEvenFailed < T> ( _ request: ( ) async throws -> ( T , HTTPResponse ) ) async throws -> ( Result < ( T , HTTPResponse ) , Error > , HTTPResponse . Status ) {
111- let status : HTTPResponse . Status
117+ func extractResponseEvenFailed < T> ( _ request: ( ) async throws -> ( T , HTTPResponse ) ) async throws -> ( Result < ( T , HTTPResponse ) , Error > , HTTPResponse ) {
118+ let response : HTTPResponse
112119 let result : Result < ( T , HTTPResponse ) , Error >
113120 do {
114121 let value = try await request ( )
115- status = value. 1 . status
122+ response = value. 1
116123 result = . success( value)
117124 } catch let error as InvalidStatusCode {
118- status = error. status
125+ response = error. response
119126 result = . failure( error)
120127 } catch let error as APIClientError {
121- if let code = error. context. status {
122- status = code
128+ if let httpResponse = error. context. httpResponse {
129+ response = httpResponse
123130 result = . failure( error)
124131 } else {
125132 throw error
126133 }
127134 } catch {
128135 throw error
129136 }
130- return ( result, status )
137+ return ( result, response )
131138}
132139
133- func extractStatusCode( from error: Error ) -> HTTPResponse . Status ? {
134- if let error = error as? InvalidStatusCode {
135- return error. status
136- } else if let error = error as? APIClientError {
137- return error. context. status
138- }
139- return nil
140+ func extractStatusCodeEvenFailed< T> ( _ request: ( ) async throws -> ( T , HTTPResponse ) ) async throws -> ( Result < ( T , HTTPResponse ) , Error > , HTTPResponse . Status ) {
141+ let ( result, response) = try await extractResponseEvenFailed ( request)
142+ return ( result, response. status)
140143}
0 commit comments