There are several scenarios where we have to provide users recovery options while the application encountered an error. However, creating a recoverable NSError is not a very pleasant experience. It's just tedious, so I wrapped up the creation process into one single API with block support.
This NSError category changed the process of creating a recoverable NSError from
// Assume id recoveryDelegate exists
NSMutableDictionary *info = [[NSMutableDictionary alloc] initWithCapacity:0];
[info setObject:@"title" forKey:NSLocalizedDescriptionKey];
[info setObject:@"suggestion" forKey:NSLocalizedRecoverySuggestionErrorKey];
[info setObject:[NSArray arrayWithObjects:@"default", @"alternate", @"other option", nil] forKey:NSLocalizedRecoveryOptionsErrorKey];
[info setObject:recoveryDelegate forKey:NSRecoveryAttempterErrorKey];
NSError *error = [NSError errorWithDomain:@"MyErrorDomain" code:0 userInfo:info];
// somewhere in recoveryDelegate's implementation file
#pragma mark NSErrorRecoveryAttempting Protocol
// This will be invoked while the error was presented with -[NSView presentError:modalForWindow:....]
- (void)attemptRecoveryFromError:(NSError *)error optionIndex:(NSUInteger)recoveryOptionIndex delegate:(id)delegate didRecoverSelector:(SEL)didRecoverSelector contextInfo:(void *)contextInfo
{
// Do something here to recover the error based on the selected option
}
// This will be invoked while the error was presented with -[NSApp presentError:]
- (BOOL)attemptRecoveryFromError:(NSError *)error optionIndex:(NSUInteger)recoveryOptionIndex
{
// Do something here to recover the error based on the selected option
return YES;
}
to
NSError *error = [NSError ot_errorWithTitle:@"title"
recoverySuggestion:@"suggestion"
recoveryOptions:[NSArray arrayWithObjects:@"default", @"alternate", @"other option", nil]
recoveryHandler:^(NSUInteger optionIndex) {
// Do something here to recover the error based on the selected option
}];
You can obtain the source code here. Happy coding!