Skip to content

Commit

Permalink
feat: add AnalyticsError type for setting fetch failed. (#366)
Browse files Browse the repository at this point in the history
* feat: add AnalyticsError type for setting fetch failed.

* feat: adding URL? to network error types.

* feat: only pass url instead of entire request.
  • Loading branch information
didiergarcia authored Oct 14, 2024
1 parent 825bac9 commit 98f1316
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
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

0 comments on commit 98f1316

Please sign in to comment.