From c7c06efd005679b6a863f8ff5406a4fd8fc58fcc Mon Sep 17 00:00:00 2001 From: Sebastien Stormacq Date: Mon, 5 Sep 2022 19:54:29 +0200 Subject: [PATCH] improved error handling --- Sources/xcodeinstall/API/AuthenticationMFA.swift | 12 +++++++++--- Sources/xcodeinstall/API/NetworkAgent.swift | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Sources/xcodeinstall/API/AuthenticationMFA.swift b/Sources/xcodeinstall/API/AuthenticationMFA.swift index b5689a8..b282555 100644 --- a/Sources/xcodeinstall/API/AuthenticationMFA.swift +++ b/Sources/xcodeinstall/API/AuthenticationMFA.swift @@ -145,9 +145,15 @@ extension AppleAuthenticator { method: .POST, body: try JSONEncoder().encode(body), headers: requestHeader, - validResponse: .range(200..<400)) - - try await self.saveSession(response: response, session: session) + validResponse: .range(200..<404)) + + if response.statusCode == 400 { + throw AuthenticationError.invalidPinCode + } else if response.statusCode == 403 { + throw AuthenticationError.unexpectedHTTPReturnCode(code: 403) + } else { + try await self.saveSession(response: response, session: session) + } // should we save additional cookies ? // return (try await getDESCookie(), session) diff --git a/Sources/xcodeinstall/API/NetworkAgent.swift b/Sources/xcodeinstall/API/NetworkAgent.swift index 5afac45..989d8aa 100644 --- a/Sources/xcodeinstall/API/NetworkAgent.swift +++ b/Sources/xcodeinstall/API/NetworkAgent.swift @@ -130,7 +130,7 @@ class NetworkAgent { let (data, response) = try await httpClient.data(for: request) guard let httpResponse = response as? HTTPURLResponse, validResponse.isValid(response: httpResponse.statusCode) else { - logger.error("=== HTTP ERROR. Status code not in range \(validResponse) ===") + logger.error("=== HTTP ERROR. Status code \((response as? HTTPURLResponse)!.statusCode) not in range \(validResponse) ===") logger.debug("URLResponse : \(response)") throw URLError(.badServerResponse) }