Ben Copsey

Change error handling to use different error codes for each type of error

Error userInfo now uses the standard localizedDescription for the description of the error
Stream errors now embed the original error in userInfo dictionary
@@ -10,117 +10,132 @@ @@ -10,117 +10,132 @@
10 // Portions are based on the ImageClient example from Apple: 10 // Portions are based on the ImageClient example from Apple:
11 // See: http://developer.apple.com/samplecode/ImageClient/listing37.html 11 // See: http://developer.apple.com/samplecode/ImageClient/listing37.html
12 12
  13 +#import <Cocoa/Cocoa.h>
  14 +
  15 +
  16 +typedef enum _ASINetworkErrorType {
  17 + ASIConnectionFailureErrorType = 1,
  18 + ASIRequestTimedOutErrorType = 2,
  19 + ASIAuthenticationErrorType = 3,
  20 + ASIRequestCancelledErrorType = 4,
  21 + ASIUnableToCreateRequestErrorType = 5,
  22 + ASIInternalErrorWhileBuildingRequestType = 6,
  23 + ASIInternalErrorWhileApplyingCredentialsType = 7
  24 +
  25 +} ASINetworkErrorType;
  26 +
13 @interface ASIHTTPRequest : NSOperation { 27 @interface ASIHTTPRequest : NSOperation {
14 28
15 - //The url for this operation, should include GET params in the query string where appropriate 29 + // The url for this operation, should include GET params in the query string where appropriate
16 NSURL *url; 30 NSURL *url;
17 31
18 - //The delegate, you need to manage setting and talking to your delegate in your subclasses 32 + // The delegate, you need to manage setting and talking to your delegate in your subclasses
19 id delegate; 33 id delegate;
20 34
21 - //HTTP method to use (GET / POST / PUT / DELETE). Defaults to GET 35 + // HTTP method to use (GET / POST / PUT / DELETE). Defaults to GET
22 NSString *requestMethod; 36 NSString *requestMethod;
23 37
24 - //Request body 38 + // Request body
25 NSData *postBody; 39 NSData *postBody;
26 40
27 - //Dictionary for custom HTTP request headers 41 + // Dictionary for custom HTTP request headers
28 NSMutableDictionary *requestHeaders; 42 NSMutableDictionary *requestHeaders;
29 43
30 - //Will be populated with HTTP response headers from the server 44 + // Will be populated with HTTP response headers from the server
31 NSDictionary *responseHeaders; 45 NSDictionary *responseHeaders;
32 46
33 - //Can be used to manually insert cookie headers to a request, but it's more likely that sessionCookies will do this for you 47 + // Can be used to manually insert cookie headers to a request, but it's more likely that sessionCookies will do this for you
34 NSMutableArray *requestCookies; 48 NSMutableArray *requestCookies;
35 49
36 - //Will be populated with Cookies 50 + // Will be populated with Cookies
37 NSArray *responseCookies; 51 NSArray *responseCookies;
38 52
39 - //If use cokie persistance is true, network requests will present valid cookies from previous requests 53 + // If use cokie persistance is true, network requests will present valid cookies from previous requests
40 BOOL useCookiePersistance; 54 BOOL useCookiePersistance;
41 55
42 - //If useKeychainPersistance is true, network requests will attempt to read credentials from the keychain, and will save them in the keychain when they are successfully presented 56 + // If useKeychainPersistance is true, network requests will attempt to read credentials from the keychain, and will save them in the keychain when they are successfully presented
43 BOOL useKeychainPersistance; 57 BOOL useKeychainPersistance;
44 58
45 - //If useSessionPersistance is true, network requests will save credentials and reuse for the duration of the session (until clearSession is called) 59 + // If useSessionPersistance is true, network requests will save credentials and reuse for the duration of the session (until clearSession is called)
46 BOOL useSessionPersistance; 60 BOOL useSessionPersistance;
47 61
48 - //When downloadDestinationPath is set, the result of this request will be downloaded to the file at this location 62 + // When downloadDestinationPath is set, the result of this request will be downloaded to the file at this location
49 - //If downloadDestinationPath is not set, download data will be stored in memory 63 + // If downloadDestinationPath is not set, download data will be stored in memory
50 NSString *downloadDestinationPath; 64 NSString *downloadDestinationPath;
51 65
52 - //Used for writing data to a file when downloadDestinationPath is set 66 + // Used for writing data to a file when downloadDestinationPath is set
53 NSOutputStream *outputStream; 67 NSOutputStream *outputStream;
54 68
55 - //When the request fails or completes successfully, complete will be true 69 + // When the request fails or completes successfully, complete will be true
56 BOOL complete; 70 BOOL complete;
57 71
58 - //If an error occurs, error will contain an NSError 72 + // If an error occurs, error will contain an NSError
  73 + // If error code is = ASIConnectionFailureErrorType (1, Connection failure occurred) - inspect [[error userInfo] objectForKey:NSUnderlyingErrorKey] for more information
59 NSError *error; 74 NSError *error;
60 75
61 - //If an authentication error occurs, we give the delegate a chance to handle it, ignoreError will be set to true 76 + // If an authentication error occurs, we give the delegate a chance to handle it, ignoreError will be set to true
62 BOOL ignoreError; 77 BOOL ignoreError;
63 78
64 - //Username and password used for authentication 79 + // Username and password used for authentication
65 NSString *username; 80 NSString *username;
66 NSString *password; 81 NSString *password;
67 - 82 +
68 - //Domain used for NTLM authentication 83 + // Domain used for NTLM authentication
69 NSString *domain; 84 NSString *domain;
70 85
71 - //Delegate for displaying upload progress (usually an NSProgressIndicator, but you can supply a different object and handle this yourself) 86 + // Delegate for displaying upload progress (usually an NSProgressIndicator, but you can supply a different object and handle this yourself)
72 id uploadProgressDelegate; 87 id uploadProgressDelegate;
73 88
74 - //Delegate for displaying download progress (usually an NSProgressIndicator, but you can supply a different object and handle this yourself) 89 + // Delegate for displaying download progress (usually an NSProgressIndicator, but you can supply a different object and handle this yourself)
75 id downloadProgressDelegate; 90 id downloadProgressDelegate;
76 - 91 +
77 // Whether we've seen the headers of the response yet 92 // Whether we've seen the headers of the response yet
78 BOOL haveExaminedHeaders; 93 BOOL haveExaminedHeaders;
79 94
80 - //Data we receive will be stored here 95 + // Data we receive will be stored here
81 NSMutableData *receivedData; 96 NSMutableData *receivedData;
82 97
83 - //Used for sending and receiving data 98 + // Used for sending and receiving data
84 CFHTTPMessageRef request; 99 CFHTTPMessageRef request;
85 CFReadStreamRef readStream; 100 CFReadStreamRef readStream;
86 101
87 // Authentication currently being used for prompting and resuming 102 // Authentication currently being used for prompting and resuming
88 CFHTTPAuthenticationRef requestAuthentication; 103 CFHTTPAuthenticationRef requestAuthentication;
89 NSMutableDictionary *requestCredentials; 104 NSMutableDictionary *requestCredentials;
90 - 105 +
91 // HTTP status code, eg: 200 = OK, 404 = Not found etc 106 // HTTP status code, eg: 200 = OK, 404 = Not found etc
92 int responseStatusCode; 107 int responseStatusCode;
93 108
94 - //Size of the response 109 + // Size of the response
95 unsigned long long contentLength; 110 unsigned long long contentLength;
96 - 111 +
97 - //Size of the POST payload 112 + // Size of the POST payload
98 unsigned long long postLength; 113 unsigned long long postLength;
99 114
100 - //The total amount of downloaded data 115 + // The total amount of downloaded data
101 unsigned long long totalBytesRead; 116 unsigned long long totalBytesRead;
102 117
103 - //Last amount of data read (used for incrementing progress) 118 + // Last amount of data read (used for incrementing progress)
104 unsigned long long lastBytesRead; 119 unsigned long long lastBytesRead;
105 - //Last amount of data sent (used for incrementing progress) 120 + // Last amount of data sent (used for incrementing progress)
106 unsigned long long lastBytesSent; 121 unsigned long long lastBytesSent;
107 122
108 - //Realm for authentication when credentials are required 123 + // Realm for authentication when credentials are required
109 NSString *authenticationRealm; 124 NSString *authenticationRealm;
110 - 125 +
111 - //This lock will block the request until the delegate supplies authentication info 126 + // This lock will block the request until the delegate supplies authentication info
112 NSConditionLock *authenticationLock; 127 NSConditionLock *authenticationLock;
113 128
114 - //This lock prevents the operation from being cancelled at an inopportune moment 129 + // This lock prevents the operation from being cancelled at an inopportune moment
115 NSLock *cancelledLock; 130 NSLock *cancelledLock;
116 131
117 - //Called on the delegate when the request completes successfully 132 + // Called on the delegate when the request completes successfully
118 SEL didFinishSelector; 133 SEL didFinishSelector;
119 134
120 - //Called on the delegate when the request fails 135 + // Called on the delegate when the request fails
121 SEL didFailSelector; 136 SEL didFailSelector;
122 137
123 - //Used for recording when something last happened during the request, we will compare this value with the current date to time out requests when appropriate 138 + // Used for recording when something last happened during the request, we will compare this value with the current date to time out requests when appropriate
124 NSDate *lastActivityTime; 139 NSDate *lastActivityTime;
125 140
126 // Number of seconds to wait before timing out - default is 10 141 // Number of seconds to wait before timing out - default is 10
@@ -141,10 +156,10 @@ @@ -141,10 +156,10 @@
141 // Also see the comments in ASINetworkQueue.h 156 // Also see the comments in ASINetworkQueue.h
142 BOOL showAccurateProgress; 157 BOOL showAccurateProgress;
143 158
144 - //Used to ensure the progress indicator is only incremented once when showAccurateProgress = NO 159 + // Used to ensure the progress indicator is only incremented once when showAccurateProgress = NO
145 BOOL updatedProgress; 160 BOOL updatedProgress;
146 161
147 - //Prevents the body of the post being built more than once (largely for subclasses) 162 + // Prevents the body of the post being built more than once (largely for subclasses)
148 BOOL haveBuiltPostBody; 163 BOOL haveBuiltPostBody;
149 } 164 }
150 165
@@ -155,16 +170,14 @@ @@ -155,16 +170,14 @@
155 170
156 #pragma mark setup request 171 #pragma mark setup request
157 172
158 -//Add a custom header to the request 173 +// Add a custom header to the request
159 - (void)addRequestHeader:(NSString *)header value:(NSString *)value; 174 - (void)addRequestHeader:(NSString *)header value:(NSString *)value;
160 175
161 - (void)buildPostBody; 176 - (void)buildPostBody;
162 177
163 #pragma mark get information about this request 178 #pragma mark get information about this request
164 179
165 -- (BOOL)isFinished; //Same thing, for NSOperationQueues to read 180 +// Returns the contents of the result as an NSString (not appropriate for binary data - used receivedData instead)
166 -  
167 -// Returns the contents of the result as an NSString (not appropriate for binary data!)  
168 - (NSString *)dataString; 181 - (NSString *)dataString;
169 182
170 #pragma mark request logic 183 #pragma mark request logic
@@ -192,11 +205,11 @@ @@ -192,11 +205,11 @@
192 205
193 #pragma mark handling request complete / failure 206 #pragma mark handling request complete / failure
194 207
195 -// Called when a request completes successfully - defaults to: @selector(requestFinished:) 208 +// Called when a request completes successfully, lets the delegate now via didFinishSelector
196 - (void)requestFinished; 209 - (void)requestFinished;
197 210
198 -// Called when a request fails - defaults to: @selector(requestFailed:) 211 +// Called when a request fails, and lets the delegate now via didFailSelector
199 -- (void)failWithProblem:(NSString *)problem; 212 +- (void)failWithError:(NSError *)theError;
200 213
201 #pragma mark http authentication stuff 214 #pragma mark http authentication stuff
202 215
@@ -216,8 +229,6 @@ @@ -216,8 +229,6 @@
216 // Apply authentication information and resume the request after an authentication challenge 229 // Apply authentication information and resume the request after an authentication challenge
217 - (void)attemptToApplyCredentialsAndResume; 230 - (void)attemptToApplyCredentialsAndResume;
218 231
219 -// Customise or overidde this to have a generic error for authentication failure  
220 -- (NSError *)authenticationError;  
221 232
222 #pragma mark stream status handlers 233 #pragma mark stream status handlers
223 234
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
@@ -48,7 +48,6 @@ @@ -48,7 +48,6 @@
48 STAssertTrue(success,@"Failed to increment progress properly"); 48 STAssertTrue(success,@"Failed to increment progress properly");
49 49
50 //Now test again with accurate progress 50 //Now test again with accurate progress
51 -  
52 [networkQueue cancelAllOperations]; 51 [networkQueue cancelAllOperations];
53 [networkQueue setShowAccurateProgress:YES]; 52 [networkQueue setShowAccurateProgress:YES];
54 53