Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve logging in httpclient #228

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Sources/Amplitude/Utilities/HttpClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Foundation
class HttpClient {
let configuration: Configuration
let session: URLSession
let logger: (any Logger)?
let diagnostics: Diagnostics
let callbackQueue: DispatchQueue

Expand All @@ -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
Expand All @@ -37,24 +39,28 @@ 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:
break
logger?.error(message: "Request failed with error: \(error.localizedDescription)")
}
}
completion(.failure(error))
} else if let httpResponse = response as? HTTPURLResponse {
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 with code\(httpResponse.statusCode): \(response)")
completion(.failure(Exception.httpError(code: httpResponse.statusCode, data: data)))
}
}
Expand All @@ -63,6 +69,7 @@ class HttpClient {
}
sessionTask!.resume()
} catch {
logger?.debug(message: "Failed to construct request")
completion(.failure(Exception.httpError(code: 500, data: nil)))
backgroundTaskCompletion?()
}
Expand Down
Loading