Skip to content

Instantly share code, notes, and snippets.

@tibo
Created November 20, 2012 00:56
Show Gist options
  • Save tibo/4115246 to your computer and use it in GitHub Desktop.
Save tibo/4115246 to your computer and use it in GitHub Desktop.
handle error by inheriting viewcontrollers
@interface LoginViewController : ParentViewController ()
@end
// ...
@implementation
#pragma mark LoginServiceDelegate
-(void)loginDidFailWithError:(NSError *)loginError
{
[self handleError:loginError];
}
// overwrite error handling
-(void)handleError:(NSError *)error
{
if (error.code == INVALIDE_PASSWORD_ERROR_CODE)
{
// manage invalid password error case...
return;
}
[super handleError:error];
}
//...
@end
@interface ParentViewController : UIViewController ()
-(void)handleError:(NSError *)error;
@end
//...
@implementation ParentViewController
-(void)handleError:(NSError *)error
{
NSLog(@"%@",[error localizedDescription]);
NSString *message;
if (error.code == CUSTOM_NETWORK_ERROR_CODE)
{ // handle specific error kind
message = @"Network Error";
}
else if (error.code == KNOWN_SERVER_ERROR_CODE)
{ // known error to display as if from the server
message = error.domain;
}
else
{ //generic error message
message = @"Something went wrong";
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:message
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitle:nil];
}
//...
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment