Skip to content

Flesh out URLError information upon URLSessionTask cancellation #2825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2020

Conversation

glessard
Copy link
Contributor

The URLError received upon URLSessionTask cancellation by a URLSessionTaskDelegate or completion handler has a blank userInfo dictionary. This is an inconsistency when compared with macOS's Foundation and it makes actual error handling harder than it could be.

This PR fills it out to match the fields filled out by macOS Foundation.

I previously reported this as https://bugs.swift.org/browse/SR-13017, and I believe this fixes that (if a fix is even desirable, that is.)

@glessard
Copy link
Contributor Author

glessard commented Jun 16, 2020

There are 6 other uses of the URLError constructor in the FoundationNetworking project, and their userInfo situation is quite inconsistent. Would it be desirable to change that?

@spevans
Copy link
Contributor

spevans commented Jun 17, 2020

The testcase is ok, I ran it under DarwinCompatibilityTests and it works correctly against Darwin.

However the test failed on both Linux and macOS. Note that currently the TestURLSession tests are disabled and need to be re-enabled in the Tests/Foundation/main.swift file. URLSession still has some other issues that makes the tests generally flaky.

Note in this case it looks like request?.url was nil so the failingURL and failureURLString entries werent added.

@glessard
Copy link
Contributor Author

I thought it ran in the TestFoundation target of the Xcode project, but maybe it didn't. (I should have tried to crash the test to make sure!) Building on Linux failed yesterday, and didn't have enough time to fix that so I crossed my fingers and submitted it. I'll try again.

@glessard
Copy link
Contributor Author

@spevans I haven't had time to solve my Linux build, but now I've gotten the test to properly run from Xcode.

@spevans
Copy link
Contributor

spevans commented Jun 17, 2020

I think we should fix the URLError.failingURL and .failureURLString properties that your original tests exposed. The following seems to fix it in my tests:

diff --git a/Sources/Foundation/NSError.swift b/Sources/Foundation/NSError.swift
index 0b55deea..ff71279e 100644
--- a/Sources/Foundation/NSError.swift
+++ b/Sources/Foundation/NSError.swift
@@ -919,12 +919,12 @@ extension URLError {
 
     /// The URL which caused a load to fail.
     public var failingURL: URL? {
-        return _nsUserInfo[NSURLErrorFailingURLErrorKey._bridgeToObjectiveC()] as? URL
+        return _nsUserInfo[NSURLErrorFailingURLErrorKey] as? URL
     }
 
     /// The string for the URL which caused a load to fail.
     public var failureURLString: String? {
-        return _nsUserInfo[NSURLErrorFailingURLStringErrorKey._bridgeToObjectiveC()] as? String
+        return _nsUserInfo[NSURLErrorFailingURLStringErrorKey] as? String
     }
 }

@glessard
Copy link
Contributor Author

glessard commented Jun 18, 2020

I wasn't sure what _bridgeToObjectiveC() was supposed to do, so I didn't go that route. Also: shouldn't that be in a different PR?

This being said, there are other things with URLError: none of the properties added in iOS 13 are on it yet. Obviously they're for either darwin-specific or not-yet-supported things, but shouldn't they be there, returning nil?

@spevans
Copy link
Contributor

spevans commented Jun 18, 2020

Yes I think a separate PR to fix up the properties in URLError would be a good start. I think the

private var _nsUserInfo: [AnyHashable : Any] {
        return _nsError.userInfo
    }

can be changed to a [String: Any] and then the properties fixed up.

It just requires adding some tests constructing URLError and then testing the properties and validating the tests in the DarwinCompatibilityTests project which will run the tests against the native Foundation. It would be good to add in the missing properties as well.

@spevans
Copy link
Contributor

spevans commented Jun 18, 2020

@swift-ci test

@spevans
Copy link
Contributor

spevans commented Jun 18, 2020

I just tested it locally on Linux with the TestURLSession tests enabled and it passed ok.

@spevans
Copy link
Contributor

spevans commented Jun 19, 2020

@swift-ci test

@spevans spevans merged commit aa07812 into swiftlang:master Jun 19, 2020
@glessard
Copy link
Contributor Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants