-1

I have made a global method to post a HTTP request. I want to globally handle the response and make the request again in case error code = -1005 occurs. How should I do that?.

The code I am using is as below:-

- (void)callHTTPAPIForRequest:(NSDictionary *)request onServer:(NSString *)serverURL resultBlock:(void (^)(id))resultBlock failureBlock:(void (^)(NSError *error))failureBlock{
    NSLog(@"%@", request);
    
    [self POST: serverURL parameters:request success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         NSDictionary *dicResponce =[APIHelper getDictionaryFromNSData:operation.responseData];
         NSLog(@"dicResponce: %@", dicResponce);
         //NSLog(@"responseObject: %@", responseObject);
         NSLog(@"the header fields in the request are %@",[operation.response allHeaderFields]);
         BlockSafeRun(resultBlock, dicResponce);
         
         
     }failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         
         
             NSLog(@"Error::: %@", error);
             BlockSafeRun(failureBlock, error);
         
         
         //failure(error);
     }];
    
    
    
}

blockSafeRun is macro defined below:-

define BlockSafeRun(block, ...) block ? block(VA_ARGS) : nil

Please suggest. Thanks in advance.

Community
  • 1
  • 1
Shikhar varshney
  • 816
  • 1
  • 9
  • 23

2 Answers2

0

Check the error code

if(error.code==-1005)

than re call the service there are many more errors not just -1005 please check NSURLError.h file in foundation framework you will get the clear idea. Here i am posting some of the errors from NSURLError.h

NS_ENUM(NSInteger)
{
    NSURLErrorUnknown =             -1,
    NSURLErrorCancelled =           -999,
    NSURLErrorBadURL =              -1000,
    NSURLErrorTimedOut =            -1001,
    NSURLErrorUnsupportedURL =          -1002,
    NSURLErrorCannotFindHost =          -1003,
    NSURLErrorCannotConnectToHost =         -1004,
    NSURLErrorNetworkConnectionLost =       -1005,
    NSURLErrorDNSLookupFailed =         -1006,
    NSURLErrorHTTPTooManyRedirects =        -1007,
    NSURLErrorResourceUnavailable =         -1008,
    NSURLErrorNotConnectedToInternet =      -1009,
    NSURLErrorRedirectToNonExistentLocation =   -1010,
    NSURLErrorBadServerResponse =       -1011,
    NSURLErrorUserCancelledAuthentication =     -1012,
    NSURLErrorUserAuthenticationRequired =  -1013,
    NSURLErrorZeroByteResource =        -1014,
    NSURLErrorCannotDecodeRawData =             -1015,
    NSURLErrorCannotDecodeContentData =         -1016,
    NSURLErrorCannotParseResponse =             -1017,
    NSURLErrorAppTransportSecurityRequiresSecureConnection NS_ENUM_AVAILABLE(10_11, 9_0) = -1022,
    NSURLErrorFileDoesNotExist =        -1100,
    NSURLErrorFileIsDirectory =         -1101,
    NSURLErrorNoPermissionsToReadFile =     -1102,
    NSURLErrorDataLengthExceedsMaximum NS_ENUM_AVAILABLE(10_5, 2_0) =   -1103,

    // SSL errors
    NSURLErrorSecureConnectionFailed =      -1200,
    NSURLErrorServerCertificateHasBadDate =     -1201,
    NSURLErrorServerCertificateUntrusted =  -1202,
    NSURLErrorServerCertificateHasUnknownRoot = -1203,
    NSURLErrorServerCertificateNotYetValid =    -1204,
    NSURLErrorClientCertificateRejected =   -1205,
    NSURLErrorClientCertificateRequired =   -1206,
    NSURLErrorCannotLoadFromNetwork =       -2000,

    // Download and file I/O errors
    NSURLErrorCannotCreateFile =        -3000,
    NSURLErrorCannotOpenFile =          -3001,
    NSURLErrorCannotCloseFile =         -3002,
    NSURLErrorCannotWriteToFile =       -3003,
    NSURLErrorCannotRemoveFile =        -3004,
    NSURLErrorCannotMoveFile =          -3005,
    NSURLErrorDownloadDecodingFailedMidStream = -3006,
    NSURLErrorDownloadDecodingFailedToComplete =-3007,

    NSURLErrorInternationalRoamingOff NS_ENUM_AVAILABLE(10_7, 3_0) =         -1018,
    NSURLErrorCallIsActive NS_ENUM_AVAILABLE(10_7, 3_0) =                    -1019,
    NSURLErrorDataNotAllowed NS_ENUM_AVAILABLE(10_7, 3_0) =                  -1020,
    NSURLErrorRequestBodyStreamExhausted NS_ENUM_AVAILABLE(10_7, 3_0) =      -1021,

    NSURLErrorBackgroundSessionRequiresSharedContainer NS_ENUM_AVAILABLE(10_10, 8_0) = -995,
    NSURLErrorBackgroundSessionInUseByAnotherProcess NS_ENUM_AVAILABLE(10_10, 8_0) = -996,
    NSURLErrorBackgroundSessionWasDisconnected NS_ENUM_AVAILABLE(10_10, 8_0)= -997,
};
Smile
  • 667
  • 1
  • 5
  • 21
  • I am also aware of such comparison to the error code. The question is to how to call the request again without having the recursion issue? – Shikhar varshney Dec 21 '15 at 16:08
  • fire the request in failure if the the service executes successfully than it will not be a recursion other wise it will be. put some count in failure block to stop after some requests. – Smile Dec 21 '15 at 16:37
0
if(error.code==-1005){
    // retry the request
}
JAL
  • 41,701
  • 23
  • 172
  • 300
Abi
  • 501
  • 4
  • 6