     }
     
     private func handleResult<T>(data: Data?, response: URLResponse?, error: Error?, cursor: Cursor<T>, completion: @escaping ((CloudResult<T>) -> ())) {
+        var cloudError: CloudError? = nil
+        var result: [T] = []
+        var deleted: [T] = []
+        var continuation: (() -> Void)? = nil
+        
+        defer {
+            completion(CloudResult(records: result, deleted: deleted, error: cloudError, continuation: continuation))
+        }
+        
         guard let responseData = data else {
             Logging.log("No response data")
+            cloudError = .noData
             return
         }
         
         guard let responseJSON = try! JSONSerialization.jsonObject(with: responseData) as? [String: AnyObject] else {
             Logging.log("Could not get response content")
-            return
-        }
-        
-        if let errorCode = responseJSON["serverErrorCode"] as? String, let reason = responseJSON["reason"] {
+            cloudError = .invalidData
+            return
+        }
+        
+        if let errorCode = responseJSON["serverErrorCode"] as? String, let reason = responseJSON["reason"] as? String {
             Logging.log("\(errorCode) - \(reason)")
+            cloudError = CloudError.server(code: errorCode, reason: reason)
             return
         }
         

        let createHandler: ((CloudCodedResult<AssetCreateResponse>) -> Void) = {
            result in
            
            switch result {
            case .failure:
                target = nil
            case .success(let result, _):
                target = result.tokens.first
            }
        }
        

        
        fetch.fetch(request: request as URLRequest) {
            data, response, error in
            
            var cloudError: CloudError? = nil
            var result: R? = nil
            var continuation: (() -> Void)? = nil
            
            defer {
                completion(CloudCodedResult(result: result, error: cloudError, continuation: continuation))
            }

            if let error = error {
                Logging.log(error)
                cloudError = .network(error)
                return
            }

            guard let received = data else {
                Logging.log("No response data")
                cloudError = .noData
                return
            }

            do {
                result = try self.decoder.decode(R.self, from: received)
            } catch {
                Logging.error("Response not decoded: \(error)")
                cloudError = .decode(error)
            }
        }
    }
}
