Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Provide a means of accessing request errors through the response obje…

…ct, so block-based

callbacks can detect if the request was successful and act accordingly.

Updated example app to show usage.
  • Loading branch information...
commit d94b69100a1a01458276bad9317c047ec8fceeb8 1 parent 82e0ef9
Luke Redpath authored
1  Classes/LRRestyRequest.h
View
@@ -25,6 +25,7 @@
}
@property (nonatomic, readonly) NSURL *URL;
@property (nonatomic, assign, readonly) NSUInteger numberOfRetries;
+@property (nonatomic, readonly) NSError *error;
/// ---------------------------------
/// @name Initializing
14 Classes/LRRestyRequest.m
View
@@ -19,6 +19,7 @@ @implementation LRRestyRequest
@synthesize numberOfRetries;
@synthesize HTTPClient;
+@synthesize error = _error;
- (id)initWithURL:(NSURL *)aURL method:(NSString *)httpMethod delegate:(id<LRRestyRequestDelegate>)theDelegate;
{
@@ -32,6 +33,7 @@ - (id)initWithURL:(NSURL *)aURL method:(NSString *)httpMethod delegate:(id<LRRes
- (void)dealloc
{
+ [_error release];
[credential release];
[delegate release];
[_URLRequest release];
@@ -179,11 +181,19 @@ - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
- [super connection:connection didFailWithError:error];
+ _error = [error retain];
if ([delegate respondsToSelector:@selector(restyRequest:didFailWithError:)]) {
- [delegate restyRequest:self didFailWithError:error];
+ [delegate restyRequest:self didFailWithError:_error];
}
+
+ LRRestyResponse *restResponse = [[LRRestyResponse alloc] initWithStatus:0 responseData:nil headers:nil originalRequest:self];
+ [delegate restyRequest:self didFinishWithResponse:restResponse];
+ [restResponse release];
+
+ [LRResty log:[NSString stringWithFormat:@"Failed with error %@", error]];
+
+ [super connection:connection didFailWithError:error];
}
@end
12 Classes/LRRestyResponse.h
View
@@ -22,6 +22,11 @@
NSDictionary *cookies;
}
/**
+ The original request that this is the response for.
+ */
+@property (nonatomic, readonly) LRRestyRequest *originalRequest;
+
+/**
Returns the raw response data.
*/
@property (nonatomic, readonly) NSData *responseData;
@@ -60,4 +65,11 @@
Returns the value for the named cookie.
*/
- (NSString *)valueForCookie:(NSString *)cookieName;
+
+/**
+ Indicates that a response was received without error.
+ */
+- (BOOL)wasSuccessful;
+
@end
+
8 Classes/LRRestyResponse.m
View
@@ -27,6 +27,7 @@ @implementation LRRestyResponse
@synthesize responseData;
@synthesize headers;
@synthesize status;
+@synthesize originalRequest = _originalRequest;
- (id)initWithStatus:(NSInteger)statusCode responseData:(NSData *)data headers:(NSDictionary *)theHeaders originalRequest:(LRRestyRequest *)originalRequest;
{
@@ -35,12 +36,14 @@ - (id)initWithStatus:(NSInteger)statusCode responseData:(NSData *)data headers:(
responseData = [data retain];
headers = [theHeaders copy];
cookies = [extractCookiesFromHeaders(headers, originalRequest.URL) copy];
+ _originalRequest = [originalRequest retain];
}
return self;
}
- (void)dealloc
{
+ [_originalRequest release];
[cookies release];
[headers release];
[responseData release];
@@ -78,4 +81,9 @@ - (NSString *)valueForCookie:(NSString *)cookieNamed;
return [[self cookieNamed:cookieNamed] value];
}
+- (BOOL)wasSuccessful
+{
+ return (status > 0) && !self.originalRequest.error;
+}
+
@end
2  Examples/GithubAPI.m
View
@@ -9,7 +9,7 @@
#import "GithubAPI.h"
#import "LRResty.h"
-#define kGITHUB_API_ROOT_URL @"http://github.com/api/v2/json"
+#define kGITHUB_API_ROOT_URL @"http://sgithub.com/api/v2/json"
NSString *githubUsername(NSString *user);
1  Examples/RemoteRepositoryExample.h
View
@@ -36,6 +36,7 @@ typedef void (^GithubUserRepositoryResultBlock)(GithubUser *user);
@optional
- (void)repositoryDidStartRemoteOperation:(RemoteResourceRepository *)repository;
- (void)repositoryDidFinishRemoteOperation:(RemoteResourceRepository *)repository;
+- (void)repository:(RemoteResourceRepository *)repository didFailWithError:(NSError *)error;
@end
@class LRRestyResource;
18 Examples/RemoteRepositoryExample.m
View
@@ -90,6 +90,14 @@ - (void)finishRemoteOperation
}
}
+- (void)failWithError:(NSError *)error
+{
+ if ([self.delegate respondsToSelector:@selector(repository:didFailWithError:)]) {
+ [self.delegate repository:self didFailWithError:error];
+ }
+ [self finishRemoteOperation];
+}
+
@end
@implementation GithubUserRepository
@@ -108,12 +116,22 @@ - (void)getUserWithUsername:(NSString *)username
[self startRemoteOperation];
[[resource at:[NSString stringWithFormat:@"user/show/%@", username]] get:^(LRRestyResponse *response, LRRestyResource *userResource) {
+ if (![response wasSuccessful]) {
+ [self failWithError:response.originalRequest.error];
+ return;
+ }
+
NSDictionary *userData = [[response asJSONObject] objectForKey:@"user"];
GithubUser *user = [[GithubUser alloc] initWithUsername:[userData objectForKey:@"login"] remoteID:[[userData objectForKey:@"id"] integerValue]];
user.fullName = [userData objectForKey:@"name"];
[[userResource at:@"followers"] get:^(LRRestyResponse *response, LRRestyResource *followersResource) {
+ if (![response wasSuccessful]) {
+ [self failWithError:response.originalRequest.error];
+ return;
+ }
+
[user setFollowers:[[response asJSONObject] objectForKey:@"users"]];
resultBlock(user);
12 Examples/Resources/ExamplesViewController.m
View
@@ -148,5 +148,17 @@ - (void)repositoryDidFinishRemoteOperation:(RemoteResourceRepository *)repositor
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
+- (void)repository:(RemoteResourceRepository *)repository didFailWithError:(NSError *)error
+{
+ [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
+
+ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:error.localizedDescription message:error.localizedFailureReason delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
+ [alert show];
+ [alert release];
+
+ NSIndexPath *selectedCellIndexPath = [self.tableView indexPathForSelectedRow];
+ [self.tableView deselectRowAtIndexPath:selectedCellIndexPath animated:YES];
+}
+
@end
Please sign in to comment.
Something went wrong with that request. Please try again.