Permalink
Browse files

Added proper error handling for NSURLConnection Delegate Methods

  • Loading branch information...
coneybeare committed Jun 23, 2012
1 parent 8dc1506 commit a40fa3df4410a8e62d907ffc4f521ec82a6d5115
Showing with 20 additions and 3 deletions.
  1. +20 −3 SSPostmark.m
View
@@ -186,6 +186,7 @@ - (void)sendBatchMessages:(NSArray *)messages {
}
- (void)_send:(NSData *)data toURL:(NSURL *)url {
+ _recievedData = nil;
_request = nil;
_request = [[NSMutableURLRequest alloc] initWithURL:url];
// Setup Headers
@@ -202,11 +203,27 @@ - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLRespon
NSHTTPURLResponse * resp = (NSHTTPURLResponse *)response;
NSInteger code = resp.statusCode;
if (code >= 400) {
- [NSNotification notificationWithName:pm_POSTMARK_NOTIFICATION
- object:self
- userInfo:[NSDictionary dictionaryWithObject:@"failed" forKey:@"status"]];
+ NSLog(@"Postmark url returned error %d %@", resp.statusCode, [NSHTTPURLResponse localizedStringForStatusCode:[resp statusCode]]);
+ NSNotification *errorNot = [NSNotification notificationWithName:pm_POSTMARK_NOTIFICATION
+ object:self
+ userInfo:[NSDictionary dictionaryWithObject:@"failed" forKey:@"status"]];
+ [[NSNotificationCenter defaultCenter] postNotification:errorNot];
+ if ([self delegate] && [[self delegate] respondsToSelector:@selector(postmark:encounteredError:)]) {
+ [[self delegate] postmark:self encounteredError:SSPMError_Unknown];
+ }
}
}
+
+- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
+ NSNotification *errorNot = [NSNotification notificationWithName:pm_POSTMARK_NOTIFICATION
+ object:self
+ userInfo:[NSDictionary dictionaryWithObject:@"failed" forKey:@"status"]];
+ [[NSNotificationCenter defaultCenter] postNotification:errorNot];
+ if ([self delegate] && [[self delegate] respondsToSelector:@selector(postmark:encounteredError:)]) {
+ [[self delegate] postmark:self encounteredError:SSPMError_Unknown];
+ }
+}
+
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// Feedback
void (^feedback)(NSDictionary *dict) = ^(NSDictionary *dict) {

0 comments on commit a40fa3d

Please sign in to comment.