Permalink
Browse files

Improved error handling

  • Loading branch information...
1 parent fc792fc commit 6523a59a37dfe8211af97150332ecf300166d7dd Magnus Ernstsson committed Oct 30, 2010
@@ -32,6 +32,7 @@ -(void)cleanUp {
}
-(void)dealloc {
+
self.gravatarid = nil;
self.email = nil;
[self cleanUp];
@@ -50,9 +51,11 @@ -(void)setConnection:(NSURLConnection *)newConnection {
#pragma mark - Class
+(NSString *)md5:(NSString *)str {
+
const char *cStr = [str UTF8String];
unsigned char result[16];
CC_MD5( cStr, strlen(cStr), result );
+
return [NSString stringWithFormat:
@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
result[0], result[1], result[2], result[3],
@@ -101,9 +104,7 @@ -(void)makeRequest:(NSString *)request {
if (self.connection) {
self.receivedData = [NSMutableData data];
- } else {
- [self handleErrorWithCode:GravatarServerConnectionError];
}
}
@@ -153,13 +154,21 @@ -(void)connectionDidFinishLoading:(NSURLConnection *)connection {
#pragma mark - Instance
-(void)cancelRequest {
+
self.cancelling = YES;
[self cleanUp];
}
-(void)requestUIImageByEmail:(NSString *)anEmail
defaultImage:(NSString *)defaultImage {
+ if (!anEmail) {
+
+ @throw [NSException exceptionWithName:NSInvalidArgumentException
+ reason:@"Email must not be nil"
+ userInfo:nil];
+ }
+
self.email = anEmail;
[self requestUIImageByGravatarId:[GravatarUIImageFactory
@@ -171,6 +180,13 @@ -(void)requestUIImageByEmail:(NSString *)anEmail
defaultImage:(NSString *)defaultImage
size:(NSInteger)size {
+ if (!anEmail) {
+
+ @throw [NSException exceptionWithName:NSInvalidArgumentException
+ reason:@"Email must not be nil"
+ userInfo:nil];
+ }
+
self.email = anEmail;
[self requestUIImageByGravatarId:[GravatarUIImageFactory
@@ -182,28 +198,62 @@ -(void)requestUIImageByEmail:(NSString *)anEmail
-(void)requestUIImageByGravatarId:(NSString *)gravatarId
defaultImage:(NSString *)defaultImage
size:(NSInteger)size {
-
+
+ if (!gravatarId) {
+
+ @throw [NSException exceptionWithName:NSInvalidArgumentException
+ reason:@"GravatarId must not be nil"
+ userInfo:nil];
+ }
+
self.gravatarid = gravatarId;
if ((size > 0) && (size < 512)) {
- [self makeRequest:
- [NSString stringWithFormat:@"http://www.gravatar.com/avatar/%@?s=%i&d=%@",
- gravatarId, size, defaultImage]];
+ if (defaultImage) {
+
+ [self makeRequest:
+ [NSString stringWithFormat:@"http://www.gravatar.com/avatar/%@?s=%i&d=%@",
+ gravatarId, size, defaultImage]];
+
+ } else {
+
+ @throw [NSException exceptionWithName:NSInvalidArgumentException
+ reason:@"Default image must not be nil"
+ userInfo:nil];
+ }
} else {
- [self handleErrorWithCode:GravatarServerArgumentError];
+ @throw [NSException exceptionWithName:NSInvalidArgumentException
+ reason:@"Size should be between 0 and 512"
+ userInfo:nil];
}
}
-(void)requestUIImageByGravatarId:(NSString *)gravatarId
defaultImage:(NSString *)defaultImage {
+ if (!gravatarId) {
+
+ @throw [NSException exceptionWithName:NSInvalidArgumentException
+ reason:@"GravatarId must not be nil"
+ userInfo:nil];
+ }
+
self.gravatarid = gravatarId;
- [self makeRequest:
- [NSString stringWithFormat:@"http://www.gravatar.com/avatar/%@&d=%@",
- gravatarId, defaultImage]];
+ if (defaultImage) {
+
+ [self makeRequest:
+ [NSString stringWithFormat:@"http://www.gravatar.com/avatar/%@&d=%@",
+ gravatarId, defaultImage]];
+
+ } else {
+
+ @throw [NSException exceptionWithName:NSInvalidArgumentException
+ reason:@"Default image must not be nil"
+ userInfo:nil];
+ }
}
#pragma mark - Class
@@ -8,7 +8,6 @@
#import <Foundation/Foundation.h>
-
@protocol GravatarService <NSObject>
@property (readonly, copy) NSString *email;
@@ -21,11 +20,8 @@
extern NSString * const GravatarServerErrorDomain;
typedef enum {
- GravatarServerInternalError = 1,
- GravatarServerOutOfMemoryError = 2,
- GravatarServerServerError = 3,
- GravatarServerArgumentError = 4,
- GravatarServerConnectionError = 5,
+ GravatarServerServerError = 1,
+ GravatarServerConnectionError = 2,
} GravatarServerError;
extern NSString * const gravatarServerImageDefault;
@@ -8,8 +8,7 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
-
-@protocol GravatarService;
+#import "GravatarService.h"
@protocol GravatarServiceDelegate
-(void)gravatarServiceDone:(id<GravatarService>)gravatarService
@@ -8,11 +8,10 @@
#import <Foundation/Foundation.h>
-@protocol GravatarService;
-@protocol GravatarServiceDelegate;
+#import "GravatarService.h"
+#import "GravatarServiceDelegate.h"
@interface GravatarServiceFactory : NSObject {
-
}
+(id<GravatarService>)requestUIImageByGravatarId:(NSString *)gravtarId
@@ -7,8 +7,7 @@
//
#import "GravatarService.h"
-
-@protocol GravatarServiceDelegate;
+#import "GravatarServiceDelegate.h"
@interface GravatarUIImageFactory : NSObject <GravatarService> {
NSString *email;

0 comments on commit 6523a59

Please sign in to comment.