Skip to content

Instantly share code, notes, and snippets.

@loghound
Created September 30, 2010 00:26
Show Gist options
  • Save loghound/603810 to your computer and use it in GitHub Desktop.
Save loghound/603810 to your computer and use it in GitHub Desktop.
Error Recovery

A simple blocks based recovery object that you can use to take an error and add a recovery method. Recovery methods are blocks passed in

LHErrorRecovery *recovery=[LHErrorRecovery new];
__block BOOL cont=FALSE;
[recovery addRecoveryButton:@"Stop Syncing" action:^{
	
	
}];
[recovery addRecoveryButton:@"Continue Syncing" action:^{
	cont=TRUE;
	
}];

[[NSApplication sharedApplication] presentError:[recovery errorForError:aError]];	
if (!cont)
	cancelSync=TRUE;
    [recovery release]
//
// LHErrorRecovery.h
// FlickrPlusIphotoCoreData
//
// Created by John McLaughlin on 9/29/10.
// Copyright 2010 Loghound.com. All rights reserved.
//
#import <Cocoa/Cocoa.h>
typedef void (^mblk)();
@interface LHErrorRecovery : NSObject {
NSMutableArray *_errorButtonTitles;
NSMutableArray *_errorBlocks;
}
- (BOOL)attemptRecoveryFromError:(NSError *)error optionIndex:(NSUInteger)recoveryOptionIndex;
-(NSError*) errorForError:(NSError*)aError;
-(void) addRecoveryButton:(NSString*)buttonTitle action:(mblk)action;
// private methods
@property (retain) NSMutableArray *_errorButtonTitles;
@property (retain) NSMutableArray *_errorBlocks;
@end
//
// LHErrorRecovery.m
// FlickrPlusIphotoCoreData
//
// Created by John McLaughlin on 9/29/10.
// Copyright 2010 Loghound.com. All rights reserved.
//
#import "LHErrorRecovery.h"
@implementation LHErrorRecovery
@synthesize _errorButtonTitles;
@synthesize _errorBlocks;
- (BOOL)attemptRecoveryFromError:(NSError *)error optionIndex:(NSUInteger)recoveryOptionIndex {
if (recoveryOptionIndex < [self._errorBlocks count]) {
mblk a=[self._errorBlocks objectAtIndex:recoveryOptionIndex];
a();
}
return YES;
}
-(void) addRecoveryButton:(NSString*)buttonTitle action:(void(^)())action {
if (!self._errorBlocks) {
self._errorBlocks=[NSMutableArray array];
self._errorButtonTitles=[NSMutableArray array];
}
[self._errorButtonTitles addObject:buttonTitle];
[self._errorBlocks addObject:[action copy]];
}
-(NSError*) errorForError:(NSError*)aError {
NSMutableDictionary *errorInfo=[[aError userInfo]mutableCopy];
[errorInfo setObject:self forKey:NSRecoveryAttempterErrorKey];
[errorInfo setObject:self._errorButtonTitles forKey:NSLocalizedRecoveryOptionsErrorKey];
NSError *ret=[NSError errorWithDomain:[aError domain]
code:[aError code]
userInfo:errorInfo];
return ret;
}
-(void) dealloc {
self._errorButtonTitles=nil;
self._errorBlocks =nil;
[super dealloc]
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment