Skip to content

Commit

Permalink
Work in progress error refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Giorgio Calderolla committed Sep 2, 2014
1 parent 499eea0 commit 6d74185
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 59 deletions.
3 changes: 2 additions & 1 deletion CTCFeedParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

@interface CTCFeedParser : NSObject

+ (NSArray*)parseFiles:(NSXMLDocument*)feed;
+ (NSArray*)parseFiles:(NSXMLDocument*)feed
error:(NSError * __autoreleasing *)error;

@end
5 changes: 3 additions & 2 deletions CTCFeedParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

@implementation CTCFeedParser

+ (NSArray*)parseFiles:(NSXMLDocument*)feed {
+ (NSArray*)parseFiles:(NSXMLDocument*)feed
error:(NSError * __autoreleasing *)outError {
NSLog(@"Parsing feed");

NSError *error = nil;
Expand All @@ -12,7 +13,7 @@ + (NSArray*)parseFiles:(NSXMLDocument*)feed {
NSArray *fileNodes = [feed nodesForXPath:@"//rss/channel/item" error:&error];

if (!fileNodes) {
NSLog(@"Parsing for URLs failed: %@", error);
*outError = error;
return nil;
}

Expand Down
6 changes: 6 additions & 0 deletions CTCFileUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@

@interface CTCFileUtils : NSObject

+ (NSData *)bookmarkForURL:(NSURL *)url
error:(NSError * __autoreleasing *)error;

+ (NSURL *)URLFromBookmark:(NSData *)bookmark
error:(NSError * __autoreleasing *)error;

+ (NSString *)computeFilenameFromURL:(NSURL*)fileURL;

+ (NSString *)addTorrentExtensionTo:(NSString*)filename;
Expand Down
36 changes: 36 additions & 0 deletions CTCFileUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,42 @@

@implementation CTCFileUtils

+ (NSData *)bookmarkForURL:(NSURL *)url
error:(NSError * __autoreleasing *)outError {
// Create a bookmark so we can transfer access to the downloads path
// to the feed checker service
NSError *error = nil;
NSData *downloadFolderBookmark = [url bookmarkDataWithOptions:NSURLBookmarkCreationMinimalBookmark
includingResourceValuesForKeys:@[]
relativeToURL:nil
error:&error];
if (!downloadFolderBookmark || error) {
*outError = error;
return nil;
}

return downloadFolderBookmark;
}

+ (NSURL *)URLFromBookmark:(NSData *)bookmark
error:(NSError * __autoreleasing *)outError {
NSError *error = nil;
BOOL isStale = NO;
NSURL *URL = [NSURL URLByResolvingBookmarkData:bookmark
options:kNilOptions
relativeToURL:nil
bookmarkDataIsStale:&isStale
error:&error];

if (!URL || error) {
NSLog(@"Could not get URL from bookmark: %@", error);
*outError = error;
return nil;
}

return URL;
}

+ (NSString *)computeFilenameFromURL:(NSURL*)fileURL {
// Compute destination filename
NSString *filename = fileURL.path.pathComponents.lastObject;
Expand Down
23 changes: 11 additions & 12 deletions CTCScheduler.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#import "CTCScheduler.h"
#import "CTCFeedChecker.h"
#import "CTCDefaults.h"
#import "CTCFileUtils.h"
#import "NSDate+TimeOfDayMath.h"


Expand Down Expand Up @@ -100,6 +101,10 @@ - (void)checkFeed {
__weak typeof(self) weakSelf = self;
[self callFeedCheckerWithReplyHandler:^(NSArray *downloadedFeedFiles,
NSError *error){
if (error) {
NSLog(@"Error checking feed: %@", error);
}

// Deal with new files
[weakSelf handleDownloadedFeedFiles:downloadedFeedFiles];

Expand All @@ -108,23 +113,17 @@ - (void)checkFeed {
}

- (NSData *)downloadFolderBookmark {
NSString *downloadPath = CTCDefaults.torrentsSavePath;
NSError *error;
NSURL *url = [NSURL fileURLWithPath:CTCDefaults.torrentsSavePath];
NSData *bookmark = [CTCFileUtils bookmarkForURL:url error:&error];

// Create a bookmark so we can transfer access to the downloads path
// to the feed checker service
NSURL *downloadFolderURL = [NSURL fileURLWithPath:downloadPath];
NSError *error = nil;
NSData *downloadFolderBookmark = [downloadFolderURL bookmarkDataWithOptions:NSURLBookmarkCreationMinimalBookmark
includingResourceValuesForKeys:@[]
relativeToURL:nil
error:&error];
if (!downloadFolderBookmark || error) {
// Not really handling this error
if (!bookmark) {
// Not really handling this at all
[NSException raise:@"Couldn't create bookmark for downloads folder"
format:@"Error: %@", error];
}

return downloadFolderBookmark;
return bookmark;
}

- (void)callFeedCheckerWithReplyHandler:(CTCFeedCheckCompletionHandler)replyHandler {
Expand Down
95 changes: 51 additions & 44 deletions CatchFeedHelper/CTCFeedChecker.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ - (void)checkShowRSSFeed:(NSURL *)feedURL
withReply:(CTCFeedCheckCompletionHandler)reply {
NSLog(@"Checking feed");

NSError *error;
NSError *error = nil;

// Resolve the bookmark (that the main app gives us to transfer access to
// the download folder) to a URL
NSURL *downloadFolderURL = [self URLFromBookmark:downloadFolderBookmark error:&error];
NSURL *downloadFolderURL = [CTCFileUtils URLFromBookmark:downloadFolderBookmark
error:&error];
if (!downloadFolderURL) {
reply(@[], error);
return;
Expand All @@ -46,20 +47,22 @@ - (void)checkShowRSSFeed:(NSURL *)feedURL
NSString *downloadFolderPath = downloadFolderURL.path;

// Download the feed
NSXMLDocument *feed = [self downloadFeed:feedURL];
NSXMLDocument *feed = [self downloadFeed:feedURL error:&error];
if (!feed) {
reply(@[], [NSError errorWithDomain:kCTCFeedCheckerErrorDomain
code:-1
userInfo:nil]);
code:-5
userInfo:@{NSLocalizedDescriptionKey: @"Could not download feed",
NSUnderlyingErrorKey: error}]);
return;
}

// Parse the feed for files
NSArray *feedFiles = [CTCFeedParser parseFiles:feed];
NSArray *feedFiles = [CTCFeedParser parseFiles:feed error:&error];
if (!feedFiles) {
reply(@[], [NSError errorWithDomain:kCTCFeedCheckerErrorDomain
code:-2
userInfo:nil]);
code:-6
userInfo:@{NSLocalizedDescriptionKey: @"Could not parse feed",
NSUnderlyingErrorKey: error}]);
return;
}

Expand All @@ -81,11 +84,12 @@ - (void)downloadFile:(NSDictionary *)file
withReply:(CTCFeedCheckDownloadCompletionHandler)reply {
NSLog(@"Downloading single file: %@", file[@"url"]);

NSError *error;
NSError *error = nil;

// Resolve the bookmark (that the main app gives us to transfer access to
// the download folder) to a URL
NSURL *downloadFolderURL = [self URLFromBookmark:downloadFolderBookmark error:&error];
NSURL *downloadFolderURL = [CTCFileUtils URLFromBookmark:downloadFolderBookmark
error:&error];
if (!downloadFolderURL) {
reply(error);
return;
Expand All @@ -105,7 +109,8 @@ - (void)downloadFile:(NSDictionary *)file
reply(error);
}

- (NSXMLDocument*)downloadFeed:(NSURL*)feedURL {
- (NSXMLDocument*)downloadFeed:(NSURL*)feedURL
error:(NSError * __autoreleasing *)outError {
NSLog(@"Downloading feed %@", feedURL);

// Flush the cache, we want fresh results
Expand All @@ -119,7 +124,7 @@ - (NSXMLDocument*)downloadFeed:(NSURL*)feedURL {
error:&error];

if (!document) {
NSLog(@"Feed download failed: %@", error);
*outError = error;
return nil;
}

Expand All @@ -128,29 +133,13 @@ - (NSXMLDocument*)downloadFeed:(NSURL*)feedURL {
return document;
}

- (NSURL *)URLFromBookmark:(NSData *)bookmark error:(NSError * __autoreleasing *)outError {
NSError *error = nil;
BOOL isStale = NO;
NSURL *URL = [NSURL URLByResolvingBookmarkData:bookmark
options:kNilOptions
relativeToURL:nil
bookmarkDataIsStale:&isStale
error:&error];

if (!URL || error) {
NSLog(@"Could not get URL from bookmark: %@", error);
*outError = error;
return nil;
}

return URL;
}

- (NSArray *)downloadFiles:(NSArray *)feedFiles
toPath:(NSString *)downloadPath
organizingByFolder:(BOOL)shouldOrganizeByFolder
skippingURLs:(NSArray *)previouslyDownloadedURLs
error:(NSError * __autoreleasing *)error {
error:(NSError * __autoreleasing *)outError {
NSError *error = nil;

NSLog(@"Downloading files (if needed)");

NSMutableArray *successfullyDownloadedFeedFiles = NSMutableArray.array;
Expand Down Expand Up @@ -179,18 +168,17 @@ - (NSArray *)downloadFiles:(NSArray *)feedFiles

NSString *downloadedTorrentFile = [self downloadFile:[NSURL URLWithString:url]
toPath:downloadPath
withShowName:showName];
withShowName:showName
error:&error];
if (downloadedTorrentFile) {
[successfullyDownloadedFeedFiles addObject:@{@"url": file[@"url"],
[successfullyDownloadedFeedFiles addObject:@{@"url": url,
@"title": file[@"title"],
@"isMagnetLink": @NO,
@"torrentFilePath": downloadedTorrentFile}];
}
else {
NSLog(@"Could not download %@", url);
*error = [NSError errorWithDomain:kCTCFeedCheckerErrorDomain
code:-3
userInfo:nil];
NSLog(@"Could not download %@: %@", url, error);
*outError = error;
}
}
}
Expand All @@ -200,7 +188,8 @@ - (NSArray *)downloadFiles:(NSArray *)feedFiles

- (NSString *)downloadFile:(NSURL *)fileURL
toPath:(NSString *)downloadPath
withShowName:(NSString *)showName {
withShowName:(NSString *)showName
error:(NSError * __autoreleasing *)outError {
NSString *folder = [CTCFileUtils folderNameForShowWithName:showName];

if (folder) NSLog(@"Downloading file %@ in folder %@",fileURL,folder);
Expand All @@ -211,12 +200,17 @@ - (NSString *)downloadFile:(NSURL *)fileURL
// Download!
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:fileURL];
NSURLResponse *urlResponse = NSURLResponse.new;
NSError *downloadError = NSError.new;
NSData *downloadedFile = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&urlResponse
error:&downloadError];
error:&error];

if (!downloadedFile) return nil;
if (!downloadedFile) {
*outError = [NSError errorWithDomain:kCTCFeedCheckerErrorDomain
code:-1
userInfo:@{NSLocalizedDescriptionKey: @"Could not download file",
NSUnderlyingErrorKey: error}];
return nil;
}

NSLog(@"Download complete, filesize: %lu", (unsigned long)downloadedFile.length);

Expand All @@ -238,6 +232,10 @@ - (NSString *)downloadFile:(NSURL *)fileURL
isDirectory:&pathAndFolderIsDirectory]) {
if (!pathAndFolderIsDirectory) {
// Exists but isn't a directory! Aaargh! Abort!
NSString *errorDescription = [NSString stringWithFormat:@"Download path is not a directory: %@", pathAndFolder];
*outError = [NSError errorWithDomain:kCTCFeedCheckerErrorDomain
code:-2
userInfo:@{NSLocalizedDescriptionKey: errorDescription}];
return nil;
}
}
Expand All @@ -248,7 +246,11 @@ - (NSString *)downloadFile:(NSURL *)fileURL
attributes:nil
error:&error]) {
// Folder creation failed :( Abort
NSLog(@"Couldn't create folder %@", pathAndFolder);
NSString *errorDescription = [NSString stringWithFormat:@"Couldn't create folder: %@", pathAndFolder];
*outError = [NSError errorWithDomain:kCTCFeedCheckerErrorDomain
code:-3
userInfo:@{NSLocalizedDescriptionKey: errorDescription,
NSUnderlyingErrorKey: error}];
return nil;
}
else {
Expand All @@ -258,9 +260,14 @@ - (NSString *)downloadFile:(NSURL *)fileURL

// Write!
BOOL wasWrittenSuccessfully = [downloadedFile writeToFile:pathAndFilename
atomically:YES];
options:NSDataWritingAtomic
error:&error];
if (!wasWrittenSuccessfully) {
NSLog(@"Couldn't save file %@ to disk", pathAndFilename);
NSString *errorDescription = [NSString stringWithFormat:@"Couldn't save file to disk: %@", pathAndFolder];
*outError = [NSError errorWithDomain:kCTCFeedCheckerErrorDomain
code:-4
userInfo:@{NSLocalizedDescriptionKey: errorDescription,
NSUnderlyingErrorKey: error}];
return nil;
}

Expand Down

0 comments on commit 6d74185

Please sign in to comment.