title: Can't able to catch error when turn off network connection at the time of downloading a file
body: <p>I am trying to download <code>.mp3</code> file from server using using <code>URLSession</code>. It also works in background. Here is the variables i have declared.</p>
  <pre><code>var urlSession: URLSession?
  var sessionTask: URLSessionDownloadTask?
  var resumeData: Data?
  </code></pre>
  <p>This is how i initialised the <code>URLSession</code>.</p>
  <pre><code>let config = URLSessionConfiguration.background(withIdentifier: &quot;com.example.app.background&quot;)
  config.networkServiceType = .background
  urlSession = URLSession(configuration: config, delegate: self, delegateQueue: nil)
  </code></pre>
  <p>To download i am using URLSessionDownloadTask. I have also implemented pause/resume functionality. Here is the code.</p>
  <pre><code>func startDownload(with url: String) {
      sessionTask = urlSession?.downloadTask(with: URL(string: url)!)
      sessionTask?.resume()
  }
  
  func cancelDownload() {
      sessionTask?.cancel()
  }
  
  func pauseDownload() {
      sessionTask?.cancel(byProducingResumeData: { (data) in
          self.resumeData = data
      })
  }
  
  func resumeDownload(url: String) {
      if let resumeData = self.resumeData {
          sessionTask = urlSession?.downloadTask(withResumeData: resumeData)
      } else {
          sessionTask = urlSession?.downloadTask(with: URL(string: url)!)
      }
      sessionTask?.resume()
  }
  </code></pre>
  <p>As i have enabled download in background, to receive success, failure and progress of download i have confirmed <code>URLSessionDownloadDelegate</code> methods.</p>
  <pre><code>func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL)
  func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)
  func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64)
  </code></pre>
  <p>Everything seems fine when i download a file even in background. I have got the success, failure and progress of the download in delegate methods.</p>
  <p>But the problem is if i turn off network connection at the middle of downloading a file, i get some error logs in console. But none of the above delegate methods are called. Here is the logs</p>
  <pre><code>Task &lt;0B34AE9E-0A46-4E78-8C90-7353CDC34929&gt;.&lt;10&gt; finished with error [-1020] Error Domain=NSURLErrorDomain Code=-1020 &quot;A data connection is not currently allowed.&quot; UserInfo={_kCFStreamErrorCodeKey=50, NSUnderlyingError=0x2817b40c0 {Error Domain=kCFErrorDomainCFNetwork Code=-1020 &quot;(null)&quot; UserInfo={_kCFStreamErrorCodeKey=50, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask &lt;0B34AE9E-0A46-4E78-8C90-7353CDC34929&gt;.&lt;10&gt;, _NSURLErrorRelatedURLSessionTaskErrorKey=(
      &quot;LocalDataTask &lt;0B34AE9E-0A46-4E78-8C90-7353CDC34929&gt;.&lt;10&gt;&quot;
  ), NSLocalizedDescription=A data connection is not currently allowed.
  </code></pre>
  <p>How can i get the error response to show the user that you have lost your network connection?</p>
  <p>I have searched for it but didn't find the solution. Pardon me if it's a duplicate question. Thanks.</p>