Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #13 from tibr/master

Adding error handling when trying to unarchive an invalid archive
  • Loading branch information...
commit 42ad9c79edcc5c863cf3fe02c585f92e45255441 2 parents 476065e + 23b51d2
@steipete steipete authored
Showing with 17 additions and 6 deletions.
  1. +17 −6 SDURLCache.m
View
23 SDURLCache.m
@@ -700,12 +700,23 @@ - (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request {
dispatch_sync(get_disk_cache_queue(), ^{
NSMutableDictionary *accesses = [self.diskCacheInfo objectForKey:kAFURLCacheInfoAccessesKey];
if ([accesses objectForKey:cacheKey]) { // OPTI: Check for cache-hit in a in-memory dictionnary before to hit the FS
- response = [NSKeyedUnarchiver unarchiveObjectWithFile:[_diskCachePath stringByAppendingPathComponent:cacheKey]];
- if (response) {
- // OPTI: Log the entry last access time for LRU cache eviction algorithm but don't save the dictionary
- // on disk now in order to save IO and time
- [accesses setObject:[NSDate date] forKey:cacheKey];
- _diskCacheInfoDirty = YES;
+ @try {
+ response = [NSKeyedUnarchiver unarchiveObjectWithFile:[_diskCachePath stringByAppendingPathComponent:cacheKey]];
+ if (response) {
+ // OPTI: Log the entry last access time for LRU cache eviction algorithm but don't save the dictionary
+ // on disk now in order to save IO and time
+ [accesses setObject:[NSDate date] forKey:cacheKey];
+ _diskCacheInfoDirty = YES;
+ }
+ }
+ @catch (NSException *exception) {
+ if ([exception.name isEqualToString:NSInvalidArgumentException]) {
+ NSLog(@"Could not unarchive object at %@, Invalid archive!", [_diskCachePath stringByAppendingPathComponent:cacheKey]);
+ [self removeCachedResponseForRequest:request];
+ }
+ }
+ @finally {
+ // do nothing
}
}
});
Please sign in to comment.
Something went wrong with that request. Please try again.