diff --git a/Sources/BSWFoundation/APIClient/APIClient+Logging.swift b/Sources/BSWFoundation/APIClient/APIClient+Logging.swift index 0878cbf..ade7a4b 100644 --- a/Sources/BSWFoundation/APIClient/APIClient+Logging.swift +++ b/Sources/BSWFoundation/APIClient/APIClient+Logging.swift @@ -6,17 +6,15 @@ import OSLog extension APIClient { func logRequest(request: URLRequest) { + guard loggingConfiguration.requestBehaviour == .all else { + return + } let logger = Logger(subsystem: submoduleName("APIClient"), category: "APIClient.Request") - switch loggingConfiguration.requestBehaviour { - case .all: - let httpMethod = request.httpMethod ?? "GET" - let path = request.url?.path ?? "" - logger.debug("Method: \(httpMethod) Path: \(path)") - if let data = request.httpBody, let prettyString = String(data: data, encoding: .utf8) { - logger.debug("Body: \(prettyString)") - } - default: - break + let httpMethod = request.httpMethod ?? "GET" + let path = request.url?.path ?? "" + logger.debug("Sending URLRequest → \(httpMethod) \(path)") + if let data = request.httpBody, let prettyString = String(data: data, encoding: .utf8) { + logger.debug("Body: \(prettyString)") } } @@ -36,9 +34,19 @@ extension APIClient { guard shouldLogThis else { return } let logType: OSLogType = isError ? .error : .debug let path = response.httpResponse.url?.path ?? "" - logger.log(level: logType, "StatusCode: \(response.httpResponse.statusCode) Path: \(path)") - if isError, let errorString = String(data: response.data, encoding: .utf8) { + logger.log(level: logType, "Receiving Response → Path: \(path) HTTPStatusCode: \(response.httpResponse.statusCode) ") + if isError, let errorString = String(data: response.data, encoding: .utf8), !errorString.isEmpty { logger.log(level: logType, "Error Message: \(errorString)") } } + + func logNetworkError(_ networkError: Swift.Error, forRequest request: URLRequest) { + guard loggingConfiguration.responseBehaviour != .none else { + return + } + let logger = Logger(subsystem: submoduleName("APIClient"), category: "APIClient.Network") + let httpMethod = request.httpMethod ?? "GET" + let path = request.url?.path ?? "" + logger.error("Error Received for URLRequest → \(httpMethod) \(path). Error: \(networkError)") + } } diff --git a/Sources/BSWFoundation/APIClient/APIClient+URLSession.swift b/Sources/BSWFoundation/APIClient/APIClient+URLSession.swift index 55c2d03..5d37793 100644 --- a/Sources/BSWFoundation/APIClient/APIClient+URLSession.swift +++ b/Sources/BSWFoundation/APIClient/APIClient+URLSession.swift @@ -40,7 +40,7 @@ public struct VoidResponse: Decodable, Hashable, Sendable {} // MARK: UIApplicationWrapper /// This is here just to make sure that on non-UIKit /// platforms we have a nice API to call to. -#if canImport(UIKit) +#if canImport(UIKit.UIApplication) import UIKit private extension APIClient { class ApplicationWrapper { diff --git a/Sources/BSWFoundation/APIClient/APIClient.swift b/Sources/BSWFoundation/APIClient/APIClient.swift index fd3a070..ef37136 100644 --- a/Sources/BSWFoundation/APIClient/APIClient.swift +++ b/Sources/BSWFoundation/APIClient/APIClient.swift @@ -181,7 +181,12 @@ private extension APIClient { func sendNetworkRequest(_ urlRequest: URLRequest) async throws -> APIClient.Response { try Task.checkCancellation() logRequest(request: urlRequest) - return try await networkFetcher.fetchData(with: urlRequest) + do { + return try await networkFetcher.fetchData(with: urlRequest) + } catch { + logNetworkError(error, forRequest: urlRequest) + throw error + } } func validateResponse(_ response: Response) async throws -> Data {