Permalink
Browse files

Added some error helper methods

  • Loading branch information...
sebreh committed Feb 17, 2014
1 parent 19e7cb3 commit c0c092fadde090ef90a1b96e235315f28c495d9b
Showing with 40 additions and 4 deletions.
  1. +7 −0 PodioKit/Support/NSError+PKErrors.h
  2. +33 −4 PodioKit/Support/NSError+PKErrors.m
@@ -23,9 +23,16 @@ extern NSString * const PKErrorErrorIdKey;
+ (NSError *)pk_responseParseError;
+ (NSError *)pk_serverErrorWithStatusCode:(NSUInteger)statusCode parsedData:(id)parsedData;
+- (NSError *)pk_underlyingError;
+- (BOOL)pk_isServerSideError;
+- (BOOL)pk_isNetworkError;
+
- (NSString *)pk_serverSideDescription;
- (NSString *)pk_humanServerSideDescription;
- (NSString *)pk_serverSideErrorId;
- (BOOL)pk_isServerSideErrorWithId:(NSString *)errorId;
+- (NSInteger)pk_serverSideErrorStatusCode;
+- (BOOL)pk_isServerSideErrorWithStatusCode:(NSUInteger)statusCode;
+
@end
@@ -71,6 +71,18 @@ + (NSError *)pk_serverErrorWithStatusCode:(NSUInteger)statusCode parsedData:(id)
return [NSError errorWithDomain:PKPodioKitErrorDomain code:PKErrorCodeServerError userInfo:userInfo];
}
+- (NSError *)pk_underlyingError {
+ return [[self userInfo] objectForKey:NSUnderlyingErrorKey];
+}
+
+- (BOOL)pk_isServerSideError {
+ return [self.domain isEqualToString:PKPodioKitErrorDomain] && self.code == PKErrorCodeServerError;
+}
+
+- (BOOL)pk_isNetworkError {
+ return [self.domain isEqualToString:NSURLErrorDomain];
+}
+
- (NSString *)pk_serverSideDescription {
return [self pk_serverSideDescriptionPropagateRequired:NO];
}
@@ -82,8 +94,8 @@ - (NSString *)pk_humanServerSideDescription {
- (NSString *)pk_serverSideDescriptionPropagateRequired:(BOOL)propagateRequired {
NSString *description = nil;
- if (self.code == PKErrorCodeServerError) {
- NSError *error = [[self userInfo] objectForKey:NSUnderlyingErrorKey];
+ if ([self pk_isServerSideError]) {
+ NSError *error = [self pk_underlyingError];
if (!propagateRequired || [error.userInfo[PKErrorPropagateKey] boolValue]) {
description = [error localizedDescription];
@@ -96,8 +108,8 @@ - (NSString *)pk_serverSideDescriptionPropagateRequired:(BOOL)propagateRequired
- (NSString *)pk_serverSideErrorId {
NSString *errorId = nil;
- if (self.code == PKErrorCodeServerError) {
- NSError *error = [[self userInfo] objectForKey:NSUnderlyingErrorKey];
+ if ([self pk_isServerSideError]) {
+ NSError *error = [self pk_underlyingError];
if (error) {
errorId = error.userInfo[PKErrorErrorIdKey];
}
@@ -110,4 +122,21 @@ - (BOOL)pk_isServerSideErrorWithId:(NSString *)errorId {
return [[self pk_serverSideErrorId] isEqualToString:errorId];
}
+- (NSInteger)pk_serverSideErrorStatusCode {
+ NSInteger statusCode = 0;
+
+ if ([self pk_isServerSideError]) {
+ NSError *serverError = [self pk_underlyingError];
+ if (serverError) {
+ statusCode = serverError.code;
+ }
+ }
+
+ return statusCode;
+}
+
+- (BOOL)pk_isServerSideErrorWithStatusCode:(NSUInteger)statusCode {
+ return [self pk_serverSideErrorStatusCode] == statusCode;
+}
+
@end

0 comments on commit c0c092f

Please sign in to comment.