title: Can't catch "Time out" error using URLSession
body: <p>I am use URLSession to &quot;POST&quot; request, but sometimes when request on a road I get error code with  difference error, more often: &quot;Time out&quot;
  This is example of error message that I saw in console &quot;Error Domain=NSURLErrorDomain Code=-1009 &quot;The Internet connection appears to be offline.&quot;&quot;</p>
  <p>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.</p>
  <p>Error message:</p>
  <pre><code>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 &lt;888AA895-677A-403B-A436-599FF55DB3BB&gt;.&lt;1&gt; HTTP load failed, 0/0 bytes (error code: -1009 [1:50])
  2020-11-25 09:52:07.460557+0200 Venom[40176:2305710] Task &lt;888AA895-677A-403B-A436-599FF55DB3BB&gt;.&lt;1&gt; finished with error [-1009] 
  Error Domain=NSURLErrorDomain Code=-1009 &quot;The Internet connection appears to be offline.&quot; UserInfo={_kCFStreamErrorCodeKey=50, NSUnderlyingError=0x600000116880 
  {Error Domain=kCFErrorDomainCFNetwork Code=-1009 &quot;(null)&quot; UserInfo={_kCFStreamErrorCodeKey=50, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask 
  &lt;888AA895-677A-403B-A436-599FF55DB3BB&gt;.&lt;1&gt;, _NSURLErrorRelatedURLSessionTaskErrorKey=(
                  &quot;LocalDataTask &lt;888AA895-677A-403B-A436-599FF55DB3BB&gt;.&lt;1&gt;&quot;
              ), NSLocalizedDescription=The Internet connection appears to be offline., 
  NSErrorFailingURLStringKey=https://link_to_my_request, NSErrorFailingURLKey=https://link_to_my_request, _kCFStreamErrorDomainKey=1}
  </code></pre>
  <p>My code:</p>
  <pre><code>func addPlayer(playerCode: String, completion: @escaping (Error?) -&gt; ()) {
      guard let url = URL(string: &quot;\(url)&quot;) else { return }
  
  var urlRequest = URLRequest(url: url)
  urlRequest.httpMethod = &quot;POST&quot;
  
  let playerCode = [playerCode]
  
  let params = [&quot;players&quot;: playerCode]
  do {
      let data = try JSONSerialization.data(withJSONObject: params, options: .init())
      
      urlRequest.httpBody = data
      urlRequest.setValue(&quot;application/json&quot;, forHTTPHeaderField: &quot;content-type&quot;)
      
      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(&quot;Oops, what is going on&quot;)
                  }
          
          guard let response = resp as? HTTPURLResponse, (200 ..&lt; 530) ~= response.statusCode else {
              print(&quot;Error: HTTP request failed&quot;)
              return
          }
          
          if response.statusCode == 200 {
              print(&quot;Status code is 200&quot;)
              do {
                  let json = try JSONDecoder().decode(ListOfPlayers.self, from: data)
  
                  // I am do what I am want
  
              
              } catch let jsonError {
                  print(&quot;Error json serialization \(jsonError)&quot;)
              }
              
                  completion(nil)
          } else if response.statusCode == 422 {
              print(&quot;Status code is 422&quot;)
                  completion(nil)
              return
          }
  
  }.resume()
  }
  } catch {
  }
  }
  </code></pre>