Commit 766988e4 by Dima Bart

Remove duplication surrounding error creation.

parent c8f04698
...@@ -120,7 +120,7 @@ ...@@ -120,7 +120,7 @@
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode]; NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
if (!error) { if (!error) {
error = [self extractErrorFromResponse:response json:json]; error = [self errorFromJSON:json response:response];
} }
block(statusCode, error); block(statusCode, error);
...@@ -141,7 +141,7 @@ ...@@ -141,7 +141,7 @@
} }
if (!error) { if (!error) {
error = [self extractErrorFromResponse:response json:json]; error = [self errorFromJSON:json response:response];
} }
block(accessToken, error); block(accessToken, error);
......
...@@ -540,19 +540,13 @@ NSString *const BUYClientCustomerAccessToken = @"X-Shopify-Customer-Access-Token ...@@ -540,19 +540,13 @@ NSString *const BUYClientCustomerAccessToken = @"X-Shopify-Customer-Access-Token
return status; return status;
} }
- (NSError *)errorFromJSON:(NSDictionary *)errorDictionary statusCode:(NSInteger)statusCode - (NSError *)errorFromJSON:(NSDictionary *)json response:(NSURLResponse *)response
{ {
return [[NSError alloc] initWithDomain:kShopifyError code:statusCode userInfo:errorDictionary];
}
- (NSError *)extractErrorFromResponse:(NSURLResponse *)response json:(NSDictionary *)json
{
NSError *error = nil;
NSInteger statusCode = [((NSHTTPURLResponse *) response) statusCode]; NSInteger statusCode = [((NSHTTPURLResponse *) response) statusCode];
if (statusCode < kMinSuccessfulStatusCode || statusCode > kMaxSuccessfulStatusCode) { if (statusCode < kMinSuccessfulStatusCode || statusCode > kMaxSuccessfulStatusCode) {
error = [NSError errorWithDomain:NSURLErrorDomain code:statusCode userInfo:json]; return [[NSError alloc] initWithDomain:kShopifyError code:statusCode userInfo:json];
} }
return error; return nil;
} }
#pragma mark - Convenience Requests #pragma mark - Convenience Requests
...@@ -620,29 +614,21 @@ NSString *const BUYClientCustomerAccessToken = @"X-Shopify-Customer-Access-Token ...@@ -620,29 +614,21 @@ NSString *const BUYClientCustomerAccessToken = @"X-Shopify-Customer-Access-Token
request.HTTPMethod = method; request.HTTPMethod = method;
NSURLSessionDataTask *task = [self.session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { NSURLSessionDataTask *task = [self.session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
NSDictionary *json = nil;
BOOL unauthorized = statusCode == 401;
BOOL failedValidation = statusCode == 422;
if (unauthorized) { NSDictionary *json = nil;
error = [[NSError alloc] initWithDomain:kShopifyError code:statusCode userInfo:nil]; if (data.length > 2) { // 2 is the minimum amount of data {} for a JSON Object. Just ignore anything less.
json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
} }
else {
//2 is the minimum amount of data {} for a JSON Object. Just ignore anything less. if (!error) {
if ((!error || failedValidation) && [data length] > 2) { error = [self errorFromJSON:json response:response];
id jsonData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
json = [jsonData isKindOfClass:[NSDictionary class]] ? jsonData : nil;
}
if (statusCode < kMinSuccessfulStatusCode || statusCode > kMaxSuccessfulStatusCode) {
error = [self errorFromJSON:json statusCode:statusCode];
}
} }
dispatch_async(self.queue, ^{ dispatch_async(self.queue, ^{
completionHandler(json, response, error); completionHandler(json, response, error);
}); });
}]; }];
[self startTask:task]; [self startTask:task];
return task; return task;
} }
......
...@@ -37,6 +37,7 @@ extern NSString *const kShopifyError; ...@@ -37,6 +37,7 @@ extern NSString *const kShopifyError;
- (NSURLSessionDataTask *)requestForURL:(NSURL *)url method:(NSString *)method body:(NSData *)body additionalHeaders:(NSDictionary *)headers completionHandler:(void (^)(NSDictionary *json, NSURLResponse *response, NSError *error))completionHandler; - (NSURLSessionDataTask *)requestForURL:(NSURL *)url method:(NSString *)method body:(NSData *)body additionalHeaders:(NSDictionary *)headers completionHandler:(void (^)(NSDictionary *json, NSURLResponse *response, NSError *error))completionHandler;
- (NSURLComponents *)URLComponentsForAPIPath:(NSString *)apiPath appendingPath:(NSString *)appendingPath queryItems:(NSDictionary*)queryItems; - (NSURLComponents *)URLComponentsForAPIPath:(NSString *)apiPath appendingPath:(NSString *)appendingPath queryItems:(NSDictionary*)queryItems;
- (NSError *)extractErrorFromResponse:(NSURLResponse *)response json:(NSDictionary *)json;
- (NSError *)errorFromJSON:(NSDictionary *)json response:(NSURLResponse *)response;
@end @end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment