Skip to content

Commit

Permalink
Better errors information
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardopereira committed Nov 2, 2017
1 parent d05a0ba commit 28134c2
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 6 deletions.
8 changes: 8 additions & 0 deletions Source/ARTPushChannelSubscriptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#import "ARTEncoder.h"
#import "ARTNSArray+ARTFunctional.h"
#import "ARTRest+Private.h"
#import "ARTTypes.h"

@implementation ARTPushChannelSubscriptions {
__weak ARTRest *_rest;
Expand Down Expand Up @@ -59,9 +60,12 @@ - (void)save:(ARTPushChannelSubscription *)channelSubscription callback:(void (^
}
else if (error) {
[_logger error:@"%@: save channel subscription failed (%@)", NSStringFromClass(self.class), error.localizedDescription];
callback([ARTErrorInfo createFromNSError:error]);
}
else {
[_logger error:@"%@: save channel subscription failed with status code %ld", NSStringFromClass(self.class), (long)response.statusCode];
NSString *plain = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
callback([ARTErrorInfo createWithCode:response.statusCode*100 status:response.statusCode message:[plain shortString]]);
}
}];
} ART_TRY_OR_REPORT_CRASH_END
Expand Down Expand Up @@ -179,12 +183,16 @@ - (void)_removeWhere:(NSDictionary<NSString *, NSString *> *)params callback:(vo
[_rest executeRequest:request withAuthOption:ARTAuthenticationOn completion:^(NSHTTPURLResponse *response, NSData *data, NSError *error) {
if (response.statusCode == 200 /*OK*/) {
[_logger debug:__FILE__ line:__LINE__ message:@"%@: channel subscription removed successfully", NSStringFromClass(self.class)];
callback(nil);
}
else if (error) {
[_logger error:@"%@: remove channel subscription failed (%@)", NSStringFromClass(self.class), error.localizedDescription];
callback([ARTErrorInfo createFromNSError:error]);
}
else {
[_logger error:@"%@: remove channel subscription failed with status code %ld", NSStringFromClass(self.class), (long)response.statusCode];
NSString *plain = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
callback([ARTErrorInfo createWithCode:response.statusCode*100 status:response.statusCode message:[plain shortString]]);
}
}];
}
Expand Down
3 changes: 3 additions & 0 deletions Source/ARTPushDeviceRegistrations.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,12 @@ - (void)save:(ARTDeviceDetails *)deviceDetails callback:(void (^)(ARTErrorInfo *
}
else if (error) {
[_logger error:@"%@: save device failed (%@)", NSStringFromClass(self.class), error.localizedDescription];
callback([ARTErrorInfo createFromNSError:error]);
}
else {
[_logger error:@"%@: save device failed with status code %ld", NSStringFromClass(self.class), (long)response.statusCode];
NSString *plain = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
callback([ARTErrorInfo createWithCode:response.statusCode*100 status:response.statusCode message:[plain shortString]]);
}
}];
} ART_TRY_OR_REPORT_CRASH_END
Expand Down
6 changes: 1 addition & 5 deletions Source/ARTRest.m
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,8 @@ - (void)executeRequest:(NSURLRequest *)request completion:(void (^)(NSHTTPURLRes

if (!validContentType) {
NSString *plain = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// Short response data
NSRange stringRange = {0, MIN([plain length], 1000)}; //1KB
stringRange = [plain rangeOfComposedCharacterSequencesForRange:stringRange];
NSString *shortPlain = [plain substringWithRange:stringRange];
// Construct artificial error
error = [ARTErrorInfo createWithCode:response.statusCode * 100 status:response.statusCode message:shortPlain];
error = [ARTErrorInfo createWithCode:response.statusCode * 100 status:response.statusCode message:[plain shortString]];
data = nil; // Discard data; format is unreliable.
[self.logger error:@"Request %@ failed with %@", request, error];
}
Expand Down
4 changes: 4 additions & 0 deletions Source/ARTTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ NSString *generateNonce();
@interface NSString (ARTJsonCompatible) <ARTJsonCompatible>
@end

@interface NSString (Utilities)
- (NSString *)shortString;
@end

@interface NSDictionary (ARTJsonCompatible) <ARTJsonCompatible>
@end

Expand Down
12 changes: 12 additions & 0 deletions Source/ARTTypes.m
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,15 @@ - (id)peek {
}

@end

#pragma mark - NSString (Utilities)

@implementation NSString (Utilities)

- (NSString *)shortString {
NSRange stringRange = {0, MIN([self length], 1000)}; //1KB
stringRange = [self rangeOfComposedCharacterSequencesForRange:stringRange];
return [self substringWithRange:stringRange];
}

@end
5 changes: 4 additions & 1 deletion Source/ARTURLSessionServerTrust.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didRece
if (challenge.protectionSpace.serverTrust) {
completionHandler(NSURLSessionAuthChallengeUseCredential, [[NSURLCredential alloc] initWithTrust:challenge.protectionSpace.serverTrust]);
}
else {
else if ([challenge.sender respondsToSelector:@selector(performDefaultHandlingForAuthenticationChallenge:)]) {
[challenge.sender performDefaultHandlingForAuthenticationChallenge:challenge];
}
else {
completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
}
}

@end

0 comments on commit 28134c2

Please sign in to comment.