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

feat: add AnalyticsError type for setting fetch failed. #366

Merged
merged 3 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
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
15 changes: 9 additions & 6 deletions Sources/Segment/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

public enum AnalyticsError: Error {
public indirect enum AnalyticsError: Error {
case storageUnableToCreate(String)
case storageUnableToWrite(String)
case storageUnableToRename(String)
Expand All @@ -16,19 +16,22 @@ public enum AnalyticsError: Error {
case storageInvalid(String)
case storageUnknown(Error)

case networkUnexpectedHTTPCode(Int)
case networkServerLimited(Int)
case networkServerRejected(Int)
case networkUnknown(Error)
case networkUnexpectedHTTPCode(URL?, Int)
case networkServerLimited(URL?, Int)
case networkServerRejected(URL?, Int)
case networkUnknown(URL?, Error)
case networkInvalidData

case jsonUnableToSerialize(Error)
case jsonUnableToDeserialize(Error)
case jsonUnknown(Error)

case pluginError(Error)

case enrichmentError(String)

case settingsFail(AnalyticsError)
case batchUploadFail(AnalyticsError)
}

extension Analytics {
Expand Down
22 changes: 11 additions & 11 deletions Sources/Segment/Utilities/Networking/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class HTTPClient {

let dataTask = session.uploadTask(with: urlRequest, fromFile: batch) { [weak self] (data, response, error) in
guard let self else { return }
handleResponse(data: data, response: response, error: error, completion: completion)
handleResponse(data: data, response: response, error: error, url: uploadURL, completion: completion)
}

dataTask.resume()
Expand All @@ -88,31 +88,31 @@ public class HTTPClient {

let dataTask = session.uploadTask(with: urlRequest, from: data) { [weak self] (data, response, error) in
guard let self else { return }
handleResponse(data: data, response: response, error: error, completion: completion)
handleResponse(data: data, response: response, error: error, url: uploadURL, completion: completion)
}

dataTask.resume()
return dataTask
}

private func handleResponse(data: Data?, response: URLResponse?, error: Error?, completion: @escaping (_ result: Result<Bool, Error>) -> Void) {
private func handleResponse(data: Data?, response: URLResponse?, error: Error?, url: URL?, completion: @escaping (_ result: Result<Bool, Error>) -> Void) {
if let error = error {
analytics?.log(message: "Error uploading request \(error.localizedDescription).")
analytics?.reportInternalError(AnalyticsError.networkUnknown(error))
analytics?.reportInternalError(AnalyticsError.networkUnknown(url, error))
completion(.failure(HTTPClientErrors.unknown(error: error)))
} else if let httpResponse = response as? HTTPURLResponse {
switch (httpResponse.statusCode) {
case 1..<300:
completion(.success(true))
return
case 300..<400:
analytics?.reportInternalError(AnalyticsError.networkUnexpectedHTTPCode(httpResponse.statusCode))
analytics?.reportInternalError(AnalyticsError.networkUnexpectedHTTPCode(url, httpResponse.statusCode))
completion(.failure(HTTPClientErrors.statusCode(code: httpResponse.statusCode)))
case 429:
analytics?.reportInternalError(AnalyticsError.networkServerLimited(httpResponse.statusCode))
analytics?.reportInternalError(AnalyticsError.networkServerLimited(url, httpResponse.statusCode))
completion(.failure(HTTPClientErrors.statusCode(code: httpResponse.statusCode)))
default:
analytics?.reportInternalError(AnalyticsError.networkServerRejected(httpResponse.statusCode))
analytics?.reportInternalError(AnalyticsError.networkServerRejected(url, httpResponse.statusCode))
completion(.failure(HTTPClientErrors.statusCode(code: httpResponse.statusCode)))
}
}
Expand All @@ -128,21 +128,21 @@ public class HTTPClient {

let dataTask = session.dataTask(with: urlRequest) { [weak self] (data, response, error) in
if let error = error {
self?.analytics?.reportInternalError(AnalyticsError.networkUnknown(error))
self?.analytics?.reportInternalError(AnalyticsError.settingsFail(AnalyticsError.networkUnknown(settingsURL, error)))
completion(false, nil)
return
}

if let httpResponse = response as? HTTPURLResponse {
if httpResponse.statusCode > 300 {
self?.analytics?.reportInternalError(AnalyticsError.networkUnexpectedHTTPCode(httpResponse.statusCode))
self?.analytics?.reportInternalError(AnalyticsError.settingsFail(AnalyticsError.networkUnexpectedHTTPCode(settingsURL, httpResponse.statusCode)))
completion(false, nil)
return
}
}

guard let data = data else {
self?.analytics?.reportInternalError(AnalyticsError.networkInvalidData)
self?.analytics?.reportInternalError(AnalyticsError.settingsFail(AnalyticsError.networkInvalidData))
completion(false, nil)
return
}
Expand All @@ -151,7 +151,7 @@ public class HTTPClient {
let responseJSON = try JSONDecoder.default.decode(Settings.self, from: data)
completion(true, responseJSON)
} catch {
self?.analytics?.reportInternalError(AnalyticsError.jsonUnableToDeserialize(error))
self?.analytics?.reportInternalError(AnalyticsError.settingsFail(AnalyticsError.jsonUnableToDeserialize(error)))
completion(false, nil)
return
}
Expand Down
Loading