I'm revisiting a project that worked in January and is now failing a basic test, wondering if anyone can point me to some obvious updates on Swift for Linux, or perhaps a Linux update that might be causing the trouble.
The code is simply an HTTP authentication request (Reddit), which still passes on Mac, but started failing on Linux sometime in the past few months:
#if canImport(FoundationNetworking)
let data: Data = await withCheckedContinuation { continuation in
URLSession.shared.dataTask(with: request) { data, _, _ in
guard let data = data else {
fatalError() // Now breaks here
}
continuation.resume(returning: data)
}.resume()
}
#else
let (data, _) = try await session.data(for: request)
#endif
I'm new to this kind of networking code as well as Swift on Linux, so any pointers are aprpeciated. Where would you look first for debugging this? Swift or OS?
Jon suggests you should call continuation.resume(throwing: error) in case of error. To do that you'd first need to change withCheckedContinuation to withCheckedThrowingContinuation.
Note that the data parameter (even when non nil) could be not what you think, e.g.: "<page not found>" or something to that account. It's worth checking response.code:
let code = (response as! HTTPURLResponse)!.statusCode
let ok = code >= 200 && code < 300
if ok - then proceed with the data, otherwise data could be bogus (even if non nil). In that case you've got no error โ create an error of your own (e.g. NSError(domain: "HTTPURLResponseError", code: 1234)) and resume the continuation with that error.
Just wanted to say thanks @Jon_Shier and @tera. I've finally found time to dig into this, and it was a bone-headed error ... looks like the servers I was using were re-configured and network access no longer worked. So even more fundamental problem than I'd been thinking of.
However, this was a good excuse for me to learn about this code... still a ways to go, but I appreciate your help here. Thank you!