title: URLSession.shared.dataTask correct way to receive data
body: <p>Good day!</p>
  
  <p>I am a little confused in trying to find the correct sequence in checking received (data, response, error) from dataTask and doing some special error handling. </p>
  
  <p>Usually we have URLSession looking like this:</p>
  
  <pre><code>class HTTPRequest {
      static func request(urlStr: String, parameters: [String: String], completion: @escaping (_ data: Data?,_ response: URLResponse?, _ error: Error?) -&gt; ()) {
          var url = OpenExchange.base_URL + urlStr
          url += getParameters(param: parameters)
          let request = URLRequest(url: URL(string: url)!)
          let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
              if error != nil {
                  print("URLSession Error: \(String(describing: error?.localizedDescription))")
                  completion(nil,nil,error)
              } else {
                  completion(data,response,nil)
              }
          }
          task.resume()
      }
  
      static func getParameters(param: [String: String]) -&gt; String {
          var data = [String]()
          for (key,value) in param {
              data.append(key + "=\(value)")
          }
          return data.map { String($0) }.joined(separator: "&amp;")
      }
  
  }
  </code></pre>
  
  <p>I have another function that has HTTPRequest inside of it, to wrap everything to and object type I'm working with:</p>
  
  <pre><code> static func networkOperation(urlStr: String, parameters: [String: String], completion: @escaping (ReturnedData) -&gt; () ) {
          var recieved = ReturnedData()
          HTTPRequest.request(urlStr: urlStr, parameters: parameters) { (data, resp, err) in
              if let data = data, let response = resp {
  
  // TODO: try JSONDecoder() if data is API Error Struct; Moderate this section depending on results of decoding;
  
                  recieved.data = data
                  recieved.response = response 
                  recieved.result = .Success
                  completion(recieved)
                  return
              } else if err == nil {
                  recieved.result = .ErrorUnknown
                  completion(recieved)
                  return
              }
              recieved.error = err as NSError?
              completion(recieved)
          }
         }
  
  public struct ReturnedData {
      public var data: Data?
      public var response: URLResponse?
      public var error: Error?
      public var result: RequestResult = .ErrorHTTP
  }
  
  public enum RequestResult: String {
      case Success
      case ErrorAPI
      case ErrorHTTP
      case ErrorUnknown
  }
  </code></pre>
  
  <p>Using the code above I can easily create different networkOperation calls for doing different API methods and handle different Data models that are returned. What I am trying to implement is API Error check. Since my API has some error description for example when you get your APP_ID wrong or current APP_ID has no permission to get information etc.. So if any of these occur the data will look like this: </p>
  
  <pre><code>  {
    "error": true,
    "status": 401,
    "message": "invalid_app_id",
    "description": "Invalid App ID provided - please sign up at https://openexchangerates.org/signup, or contact support@openexchangerates.org."
    }
  </code></pre>
  
  <p>I think its not alright trying to decode every received data with Error struct in networkOperations "//TODO" mark, maybe there is some good way to implement this? </p>