From 387eca3bd36e3fd27e3dfb96f7a280f489e0a022 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 22 Mar 2019 15:25:10 +0300 Subject: Handle non-200 status codes --- Juick/APIClient.m | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/Juick/APIClient.m b/Juick/APIClient.m index e2ef67d..ac827e0 100644 --- a/Juick/APIClient.m +++ b/Juick/APIClient.m @@ -56,13 +56,18 @@ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self.backgroundQueue addOperationWithBlock:^{ [self.manager GET:path parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { - NSMutableArray *messages = [NSMutableArray new]; - [((NSArray *)responseObject) enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { - [messages addObject:[Message fromJSON:obj]]; - }]; - [[NSOperationQueue mainQueue] addOperationWithBlock:^{ - callback(messages, nil); - }]; + NSInteger statusCode = [((NSHTTPURLResponse *) task.response) statusCode]; + if (statusCode == 200) { + NSMutableArray *messages = [NSMutableArray new]; + [((NSArray *)responseObject) enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { + [messages addObject:[Message fromJSON:obj]]; + }]; + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ + callback(messages, nil); + }]; + } else { + callback(nil, [NSError errorWithDomain:@"JuickErrorDomain" code:statusCode userInfo:nil]); + } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"REST Error: %@", error); -- cgit v1.2.3