From baf26b054edd2965dbba65c4bf79d3191ec99150 Mon Sep 17 00:00:00 2001 From: Chris Leonavicius Date: Fri, 13 Sep 2024 16:29:59 -0700 Subject: [PATCH] fix: improve logging in httpclient --- Sources/Amplitude/Utilities/HttpClient.swift | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Sources/Amplitude/Utilities/HttpClient.swift b/Sources/Amplitude/Utilities/HttpClient.swift index 72964e2..ae90f12 100644 --- a/Sources/Amplitude/Utilities/HttpClient.swift +++ b/Sources/Amplitude/Utilities/HttpClient.swift @@ -10,6 +10,7 @@ import Foundation class HttpClient { let configuration: Configuration let session: URLSession + let logger: (any Logger)? let diagnostics: Diagnostics let callbackQueue: DispatchQueue @@ -22,6 +23,7 @@ class HttpClient { init(configuration: Configuration, diagnostics: Diagnostics, callbackQueue: DispatchQueue? = nil) { self.configuration = configuration self.diagnostics = diagnostics + self.logger = configuration.loggerProvider self.callbackQueue = callbackQueue ?? .global() let sessionConfiguration = URLSessionConfiguration.default @@ -37,15 +39,17 @@ class HttpClient { let request = try getRequest() let requestData = getRequestData(events: events) - sessionTask = session.uploadTask(with: request, from: requestData) { [callbackQueue, configuration] data, response, error in + sessionTask = session.uploadTask(with: request, from: requestData) { [callbackQueue, configuration, logger] data, response, error in callbackQueue.async { if let error = error { let nsError = error as NSError if nsError.domain == NSURLErrorDomain { switch nsError.code { case NSURLErrorCannotConnectToHost, NSURLErrorNetworkConnectionLost, NSURLErrorCannotFindHost, NSURLErrorAppTransportSecurityRequiresSecureConnection: + logger?.error(message: "Conection failed with error: \(error.localizedDescription), marking offline") configuration.offline = true default: + logger?.error(message: "Request failed with error: \(error.localizedDescription)") break } } @@ -54,7 +58,10 @@ class HttpClient { switch httpResponse.statusCode { case 1..<300: completion(.success(httpResponse.statusCode)) + logger?.debug(message: "Successfully completed request") default: + let response = String(data: data ?? Data(), encoding: .utf8) ?? "" + logger?.error(message: "Response failed \(httpResponse.statusCode): \(response)") completion(.failure(Exception.httpError(code: httpResponse.statusCode, data: data))) } } @@ -63,6 +70,7 @@ class HttpClient { } sessionTask!.resume() } catch { + logger?.debug(message: "Failed to construct request") completion(.failure(Exception.httpError(code: 500, data: nil))) backgroundTaskCompletion?() }