Skip to content

Commit

Permalink
Add logging for upload failures (#22)
Browse files Browse the repository at this point in the history
* REMOVE: unused import statement

* ADD: log statements for upload failures and retry attempts
  • Loading branch information
AniVerma17 authored Jan 22, 2024
1 parent 0193c77 commit dacbb6a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 24 deletions.
1 change: 0 additions & 1 deletion ImageKit/ImageKitUploader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import Foundation
import Network
//import Reachability

public class ImageKitUploader {

Expand Down
72 changes: 49 additions & 23 deletions ImageKit/Util/UploadApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
//

import Foundation
import OSLog

class UploadAPI: NSObject, URLSessionTaskDelegate {
internal static var baseUrl = "https://upload.imagekit.io"
internal static let log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "ImageKitIO")

internal static func upload(
file: Any,
Expand Down Expand Up @@ -87,7 +89,6 @@ class UploadAPI: NSObject, URLSessionTaskDelegate {
}

request.setValue(formData.contentType, forHTTPHeaderField: "Content-Type")
print("Retry attempt: \(retryCount), timeout: \(getRetryTimeOut(uploadPolicy, retryCount))")

DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(getRetryTimeOut(uploadPolicy, retryCount))) {
do {
Expand All @@ -97,39 +98,64 @@ class UploadAPI: NSObject, URLSessionTaskDelegate {
let urlSession = URLSession(configuration: urlConfiguration, delegate: URLSession.shared.delegate, delegateQueue: URLSession.shared.delegateQueue)
uploadDelegate.uploadProgressHandler = progressClosure
let task = urlSession.dataTask(with: request) { data, response, error in
if let error = error {
completion(Result.failure(error))
if let err = error {
os_log("[Upload file=%s] Upload request failed, error: %@", log: log, fileName, err as CVarArg)
if retryCount == uploadPolicy.maxErrorRetries {
completion(Result.failure(err))
} else {
os_log("[Upload file=%s] Retry attempt %d", log: log, fileName, retryCount + 1)
upload(
file: file,
token: token,
fileName: fileName,
useUniqueFileName: useUniqueFileName,
tags: tags,
folder: folder,
isPrivateFile: isPrivateFile,
progressClosure: progressClosure,
urlConfiguration: urlConfiguration,
uploadPolicy: uploadPolicy,
completion: completion,
retryCount: retryCount + 1
)
}
return
}
let response = response as! HTTPURLResponse
let status = response.statusCode
let httpResponse = response as! HTTPURLResponse
let status = httpResponse.statusCode
if (200...299).contains(status) {
completion(Result.success((response, try? IKJSONDecoder().decode(UploadAPIResponse.self, from: data!))))
} else if (400..<500).contains(status) || retryCount == uploadPolicy.maxErrorRetries {
let uploadApiError = try? IKJSONDecoder().decode(UploadAPIError.self, from: data!)
completion(Result.failure(uploadApiError!))
completion(Result.success((httpResponse, try? IKJSONDecoder().decode(UploadAPIResponse.self, from: data!))))
} else {
upload(
file: file,
token: token,
fileName: fileName,
useUniqueFileName: useUniqueFileName,
tags: tags,
folder: folder,
isPrivateFile: isPrivateFile,
progressClosure: progressClosure,
urlConfiguration: urlConfiguration,
uploadPolicy: uploadPolicy,
completion: completion,
retryCount: retryCount + 1
)
let uploadApiError = try? IKJSONDecoder().decode(UploadAPIError.self, from: data!)
os_log("[Upload file=%s] Upload request failed, response status: HTTP %d, response message: %s", log: log, fileName, status, uploadApiError?.message ?? "")
if (400..<500).contains(status) || retryCount == uploadPolicy.maxErrorRetries {
completion(Result.failure(uploadApiError!))
} else {
os_log("[Upload file=%s] Retry attempt %d", log: log, fileName, retryCount + 1)
upload(
file: file,
token: token,
fileName: fileName,
useUniqueFileName: useUniqueFileName,
tags: tags,
folder: folder,
isPrivateFile: isPrivateFile,
progressClosure: progressClosure,
urlConfiguration: urlConfiguration,
uploadPolicy: uploadPolicy,
completion: completion,
retryCount: retryCount + 1
)
}
}
}
task.resume()
} catch let error {
os_log("[Upload file=%s] Retry attempt %@", log: log, fileName, error as CVarArg)
if retryCount == uploadPolicy.maxErrorRetries {
completion(Result.failure(error))
} else {
os_log("[Upload file=%s] Retry attempt %d", log: log, fileName, retryCount + 1)
upload(
file: file,
token: token,
Expand Down

0 comments on commit dacbb6a

Please sign in to comment.