@@ -103,21 +103,28 @@ open class HTTPWebRequestDelivery : NSObject, WebRequestDelivery {
103103 let group = DispatchGroup ( )
104104 var taskData : Data ?
105105 var taskResponse : HTTPURLResponse ?
106+ var errorCode : ErrorCode ?
106107
107108 group. enter ( )
108109 queue. async {
109110 let task = urlSession. dataTask (
110111 with: urlRequest,
111- completionHandler: ( { ( data, response, _ ) in
112+ completionHandler: ( { ( data, response, error ) in
112113 taskData = data
113114 taskResponse = response as? HTTPURLResponse
115+ errorCode = self . parseError ( error)
114116 group. leave ( )
115117 } ) )
116118 task. resume ( )
117119 }
118120
119121 let timeoutResult = group. wait ( timeout: . now( ) + timeoutInterval)
120122
123+ if let errorCode = errorCode {
124+ try complete ( request: request, errorCode: errorCode)
125+ return
126+ }
127+
121128 guard ( timeoutResult != . timedOut) else {
122129 try complete ( request: request, errorCode: . TimedOut)
123130 return
@@ -150,4 +157,19 @@ open class HTTPWebRequestDelivery : NSObject, WebRequestDelivery {
150157 let result = WebRequest . Result ( status: status, headers: [ : ] , data: nil )
151158 try request. onDataReceived ? ( result, request, percentComplete, target) ;
152159 }
160+
161+ open func parseError( _ error: Error ? ) -> ErrorCode ? {
162+ guard let error = error else { return nil }
163+ if let urlError = error as? URLError {
164+ switch urlError. code {
165+ case . notConnectedToInternet:
166+ return . NoInternetConnection
167+ case . timedOut:
168+ return . TimedOut
169+ default :
170+ ( )
171+ }
172+ }
173+ return . MalformedResponse
174+ }
153175}
0 commit comments