Skip to content

Commit

Permalink
Throw errors when creating cache file failure
Browse files Browse the repository at this point in the history
  • Loading branch information
onevcat committed Dec 8, 2019
1 parent aebb5cc commit 8e879b0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
19 changes: 18 additions & 1 deletion Sources/Cache/DiskStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ public enum DiskStorage {
}

let fileURL = cacheFileURL(forKey: key)
do {
try data.write(to: fileURL)
} catch {
throw KingfisherError.cacheError(
reason: .cannotCreateCacheFile(fileURL: fileURL, key: key, data: data, error: error)
)
}

let now = Date()
let attributes: [FileAttributeKey : Any] = [
Expand All @@ -116,7 +123,17 @@ public enum DiskStorage {
// The estimated expiration date.
.modificationDate: expiration.estimatedExpirationSinceNow.fileAttributeDate
]
config.fileManager.createFile(atPath: fileURL.path, contents: data, attributes: attributes)
do {
try config.fileManager.setAttributes(attributes, ofItemAtPath: fileURL.path)
} catch {
throw KingfisherError.cacheError(
reason: .cannotSetCacheFileAttribute(
filePath: fileURL.path,
attributes: attributes,
error: error
)
)
}
}

func value(forKey key: String, extendingExpiration: ExpirationExtending = .cacheTime) throws -> T? {
Expand Down
26 changes: 26 additions & 0 deletions Sources/General/KingfisherError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ public enum KingfisherError: Error {
/// - imageNotExisting: The requested image does not exist in cache. Code 3006.
/// - cannotConvertToData: Cannot convert an object to data for storing. Code 3007.
/// - cannotSerializeImage: Cannot serialize an image to data for storing. Code 3008.
/// - cannotCreateCacheFile: Cannot create the cache file at a certain fileURL under a key. Code 3009.
/// - cannotSetCacheFileAttribute: Cannot set file attributes to a cached file. Code 3010.
public enum CacheErrorReason {

/// Cannot create a file enumerator for a certain disk URL. Code 3001.
Expand Down Expand Up @@ -139,6 +141,22 @@ public enum KingfisherError: Error {
/// - original: The original image data, if exists.
/// - serializer: The `CacheSerializer` used for the image serializing.
case cannotSerializeImage(image: KFCrossPlatformImage?, original: Data?, serializer: CacheSerializer)

/// Cannot create the cache file at a certain fileURL under a key. Code 3009.
/// - fileURL: The url where the cache file should be created.
/// - key: The cache key used for the cache. When caching a file through `KingfisherManager` and Kingfisher's
/// extension method, it is the resolved cache key based on your input `Source` and the image processors.
/// - data: The data to be cached.
/// - error: The underlying error originally thrown by Foundation when writing the `data` to the disk file at
/// `fileURL`.
case cannotCreateCacheFile(fileURL: URL, key: String, data: Data, error: Error)

/// Cannot set file attributes to a cached file. Code 3010.
/// - filePath: The path of target cache file.
/// - attributes: The file attribute to be set to the target file.
/// - error: The underlying error originally thrown by Foundation when setting the `attributes` to the disk
/// file at `filePath`.
case cannotSetCacheFileAttribute(filePath: String, attributes: [FileAttributeKey : Any], error: Error)
}


Expand Down Expand Up @@ -346,6 +364,12 @@ extension KingfisherError.CacheErrorReason {
return "Cannot serialize an image due to the cache serializer returning `nil`. " +
"Image: \(String(describing:image)), original data: \(String(describing: originalData)), " +
"serializer: \(serializer)."
case .cannotCreateCacheFile(let fileURL, let key, let data, let error):
return "Cannot create cache file at url: \(fileURL), key: \(key), data length: \(data.count). " +
"Underlying foundation error: \(error)."
case .cannotSetCacheFileAttribute(let filePath, let attributes, let error):
return "Cannot set file attribute for the cache file at path: \(filePath), attributes: \(attributes)." +
"Underlying foundation error: \(error)."
}
}

Expand All @@ -359,6 +383,8 @@ extension KingfisherError.CacheErrorReason {
case .imageNotExisting: return 3006
case .cannotConvertToData: return 3007
case .cannotSerializeImage: return 3008
case .cannotCreateCacheFile: return 3009
case .cannotSetCacheFileAttribute: return 3010
}
}
}
Expand Down

0 comments on commit 8e879b0

Please sign in to comment.