Search code examples
iosswiftrequesttimeouturlsession

Can't catch "Time out" error using URLSession


I am use URLSession to "POST" request, but sometimes when request on a road I get error code with difference error, more often: "Time out" This is example of error message that I saw in console "Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline.""

But I can't catch this error in code, what I am do wrong?, also I get another code errors but it does not change situation.

Error message:

2020-11-25 09:52:07.456207+0200 TestApp[40176:2305710] Connection 4: received failure notification
2020-11-25 09:52:07.456344+0200 TestApp[40176:2305710] Connection 4: failed to connect 1:50, reason -1
2020-11-25 09:52:07.456771+0200 TestApp[40176:2305710] Connection 4: encountered error(1:50)
2020-11-25 09:52:07.459696+0200 TestApp[40176:2305710] Task <888AA895-677A-403B-A436-599FF55DB3BB>.<1> HTTP load failed, 0/0 bytes (error code: -1009 [1:50])
2020-11-25 09:52:07.460557+0200 Venom[40176:2305710] Task <888AA895-677A-403B-A436-599FF55DB3BB>.<1> finished with error [-1009] 
Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo={_kCFStreamErrorCodeKey=50, NSUnderlyingError=0x600000116880 
{Error Domain=kCFErrorDomainCFNetwork Code=-1009 "(null)" UserInfo={_kCFStreamErrorCodeKey=50, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask 
<888AA895-677A-403B-A436-599FF55DB3BB>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
                "LocalDataTask <888AA895-677A-403B-A436-599FF55DB3BB>.<1>"
            ), NSLocalizedDescription=The Internet connection appears to be offline., 
NSErrorFailingURLStringKey=https://link_to_my_request, NSErrorFailingURLKey=https://link_to_my_request, _kCFStreamErrorDomainKey=1}

My code:

func addPlayer(playerCode: String, completion: @escaping (Error?) -> ()) {
    guard let url = URL(string: "\(url)") else { return }

var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = "POST"

let playerCode = [playerCode]

let params = ["players": playerCode]
do {
    let data = try JSONSerialization.data(withJSONObject: params, options: .init())
    
    urlRequest.httpBody = data
    urlRequest.setValue("application/json", forHTTPHeaderField: "content-type")
    
    let sessionConfig = URLSessionConfiguration.default
    sessionConfig.timeoutIntervalForRequest = 5.0
    
    DispatchQueue.global(qos: .background).async {
    URLSession(configuration: sessionConfig).dataTask(with: urlRequest) { (data, resp, err) in

        guard let data = data else { return }
        guard let resp = resp else { return }

        print(data)
        print(resp)

if (err as? URLError)?.code == .timedOut {
                    // Handle session timeout
// THIS PIACE OF CODE DOES NOT EXECUTED
                    print("Oops, what is going on")
                }
        
        guard let response = resp as? HTTPURLResponse, (200 ..< 530) ~= response.statusCode else {
            print("Error: HTTP request failed")
            return
        }
        
        if response.statusCode == 200 {
            print("Status code is 200")
            do {
                let json = try JSONDecoder().decode(ListOfPlayers.self, from: data)

                // I am do what I am want

            
            } catch let jsonError {
                print("Error json serialization \(jsonError)")
            }
            
                completion(nil)
        } else if response.statusCode == 422 {
            print("Status code is 422")
                completion(nil)
            return
        }

}.resume()
}
} catch {
}
}

Solution

  • Printing the rawValues of all the possible URLError codes:

    unknown: -1
    cancelled: -999
    badURL: -1000
    timedOut: -1001
    unsupportedURL: -1002
    cannotFindHost: -1003
    cannotConnectToHost: -1004
    networkConnectionLost: -1005
    dnsLookupFailed: -1006
    httpTooManyRedirects: -1007
    resourceUnavailable: -1008
    notConnectedToInternet: -1009
    redirectToNonExistentLocation: -1010
    badServerResponse: -1011
    userCancelledAuthentication: -1012
    userAuthenticationRequired: -1013
    zeroByteResource: -1014
    cannotDecodeRawData: -1015
    cannotDecodeContentData: -1016
    cannotParseResponse: -1017
    appTransportSecurityRequiresSecureConnection: -1022
    fileDoesNotExist: -1100
    fileIsDirectory: -1101
    noPermissionsToReadFile: -1102
    dataLengthExceedsMaximum: -1103
    secureConnectionFailed: -1200
    serverCertificateHasBadDate: -1201
    serverCertificateUntrusted: -1202
    serverCertificateHasUnknownRoot: -1203
    serverCertificateNotYetValid: -1204
    clientCertificateRejected: -1205
    clientCertificateRequired: -1206
    cannotLoadFromNetwork: -2000
    cannotCreateFile: -3000
    cannotOpenFile: -3001
    cannotCloseFile: -3002
    cannotWriteToFile: -3003
    cannotRemoveFile: -3004
    cannotMoveFile: -3005
    downloadDecodingFailedMidStream: -3006
    downloadDecodingFailedToComplete: -3007
    internationalRoamingOff: -1018
    callIsActive: -1019
    dataNotAllowed: -1020
    requestBodyStreamExhausted: -1021
    backgroundSessionRequiresSharedContainer: -995
    backgroundSessionInUseByAnotherProcess: -996
    backgroundSessionWasDisconnected: -997
    

    You can see that you are looking for notConnectedToInternet code. (the one with rawValue -1009)

    So, your code might look like this:

    switch (err as? URLError)?.code {
    case .some(.timedOut):
        // Handle session timeout
    case .some(.notConnectedToInternet):
        // Handle not connected to internet
    default: break
    }