Skip to content

Commit e13a461

Browse files
committed
Darwin Option 4
1 parent 7b3c205 commit e13a461

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

Foundation/URLSession/URLSessionTask.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ open class URLSessionTask : NSObject, NSCopying {
8888
self._protocol = urlProtocol.init(task: self, cachedResponse: nil, client: nil)
8989
}
9090
}
91-
guard self._protocol != nil else { fatalError("Unable to find protocol handler for \(request)") }
9291
}
9392
deinit {
9493
//TODO: Do we remove the EasyHandle from the session here? This might run on the wrong thread / queue.
@@ -251,7 +250,20 @@ open class URLSessionTask : NSObject, NSCopying {
251250
self.updateTaskState()
252251
if self.suspendCount == 0 {
253252
self.workQueue.async {
254-
self._protocol?.startLoading()
253+
if let _protocol = self._protocol {
254+
_protocol.startLoading()
255+
}
256+
else {
257+
var userInfo: [String: Any] = [NSLocalizedDescriptionKey: "unsupported URL"]
258+
if let url = self.originalRequest?.url {
259+
userInfo[NSURLErrorFailingURLErrorKey] = url
260+
userInfo[NSURLErrorFailingURLStringErrorKey] = url.absoluteString
261+
}
262+
let error = NSError(domain: NSURLErrorDomain,
263+
code: URLError.unsupportedURL.rawValue,
264+
userInfo: userInfo)
265+
_ProtocolClient().urlProtocol(task: self, didFailWithError: error)
266+
}
255267
}
256268
}
257269
}
@@ -576,6 +588,10 @@ extension _ProtocolClient : URLProtocolClient {
576588

577589
func urlProtocol(_ protocol: URLProtocol, didFailWithError error: Error) {
578590
guard let task = `protocol`.task else { fatalError() }
591+
urlProtocol(task: task, didFailWithError: error)
592+
}
593+
594+
func urlProtocol(task: URLSessionTask, didFailWithError error: Error) {
579595
guard let session = task.session as? URLSession else { fatalError() }
580596
switch session.behaviour(for: task) {
581597
case .taskDelegate(let delegate):

0 commit comments

Comments
 (0)