Skip to content

Commit f9b2c18

Browse files
committed
NSLocalizedDescription for http errors
1 parent 81048e4 commit f9b2c18

File tree

5 files changed

+30
-13
lines changed

5 files changed

+30
-13
lines changed

CoreFoundation/URL.subproj/CFURLSessionInterface.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ static CFURLSessionMultiCode MakeMultiCode(CURLMcode value) {
3131
return (CFURLSessionMultiCode) { value };
3232
}
3333

34+
const char *CFURLSessionErrorDescription(int value) {
35+
return curl_easy_strerror(value);
36+
}
37+
3438

3539
CFURLSessionEasyHandle _Nonnull CFURLSessionEasyHandleInit() {
3640
return curl_easy_init();

CoreFoundation/URL.subproj/CFURLSessionInterface.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ typedef struct CFURLSessionEasyCode {
4848
int value;
4949
} CFURLSessionEasyCode;
5050

51+
CF_EXPORT const char * _Nonnull CFURLSessionErrorDescription(int value);
52+
5153
/// CURLcode
5254
CF_EXPORT CFURLSessionEasyCode const CFURLSessionEasyCodeOK; // CURLE_OK
5355
CF_EXPORT CFURLSessionEasyCode const CFURLSessionEasyCodeUNSUPPORTED_PROTOCOL; // CURLE_UNSUPPORTED_PROTOCOL

Foundation/URLSession/http/EasyHandle.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ internal final class _EasyHandle {
5656
fileprivate var headerList: _CurlStringList?
5757
fileprivate var pauseState: _PauseState = []
5858
internal var timeoutTimer: _TimeoutSource!
59+
private var errorBuffer = [UInt8](repeating: 0, count: 1000)
5960
#if os(Android)
6061
static fileprivate var _CAInfoFile: UnsafeMutablePointer<Int8>?
6162
#endif
@@ -89,8 +90,8 @@ extension _EasyHandle {
8990
}
9091

9192
internal extension _EasyHandle {
92-
func completedTransfer(withErrorCode errorCode: Int?) {
93-
delegate?.transferCompleted(withErrorCode: errorCode)
93+
func completedTransfer(withErrorInfo errorInfo: URLErrorInfo?) {
94+
delegate?.transferCompleted(withErrorInfo: errorInfo)
9495
}
9596
}
9697
internal protocol _EasyHandleDelegate: class {
@@ -107,7 +108,7 @@ internal protocol _EasyHandleDelegate: class {
107108
func fill(writeBuffer buffer: UnsafeMutableBufferPointer<Int8>) -> _EasyHandle._WriteBufferResult
108109
/// The transfer for this handle completed.
109110
/// - parameter errorCode: An NSURLError code, or `nil` if no error occured.
110-
func transferCompleted(withErrorCode errorCode: Int?)
111+
func transferCompleted(withErrorInfo errorInfo: URLErrorInfo?)
111112
/// Seek the input stream to the given position
112113
func seekInputStream(to position: UInt64) throws
113114
/// Gets called during the transfer to update progress.
@@ -170,6 +171,9 @@ extension _EasyHandle {
170171
let protocols = (CFURLSessionProtocolHTTP | CFURLSessionProtocolHTTPS)
171172
try! CFURLSession_easy_setopt_long(rawHandle, CFURLSessionOptionPROTOCOLS, protocols).asError()
172173
try! CFURLSession_easy_setopt_long(rawHandle, CFURLSessionOptionREDIR_PROTOCOLS, protocols).asError()
174+
errorBuffer.withUnsafeMutableBufferPointer {
175+
try! CFURLSession_easy_setopt_ptr(rawHandle, CFURLSessionOptionERRORBUFFER, $0.baseAddress).asError()
176+
}
173177
#if os(Android)
174178
// See https://curl.haxx.se/docs/sslcerts.html
175179
// For SSL to work you need "cacert.pem" to be accessable

Foundation/URLSession/http/HTTPURLProtocol.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ fileprivate extension _HTTPURLProtocol {
136136
// NSURLErrorNoPermissionsToReadFile
137137
// NSURLErrorFileDoesNotExist
138138
self.internalState = .transferFailed
139-
failWith(errorCode: errorCode(fileSystemError: e), request: request)
139+
failWith(errorCode: errorCode(fileSystemError: e), errorDescription: "File system error", request: request)
140140
return
141141
}
142142

@@ -330,12 +330,13 @@ internal extension _HTTPURLProtocol {
330330
case stream(InputStream)
331331
}
332332

333-
func failWith(errorCode: Int, request: URLRequest) {
333+
func failWith(errorCode: Int, errorDescription: String?, request: URLRequest) {
334334
//TODO: Error handling
335335
let userInfo: [String : Any]? = request.url.map {
336336
[
337337
NSURLErrorFailingURLErrorKey: $0,
338338
NSURLErrorFailingURLStringErrorKey: $0.absoluteString,
339+
NSLocalizedDescriptionKey: errorDescription ?? "Unknown http error",
339340
]
340341
}
341342
let error = URLError(_nsError: NSError(domain: NSURLErrorDomain, code: errorCode, userInfo: userInfo))
@@ -530,16 +531,16 @@ extension _HTTPURLProtocol: _EasyHandleDelegate {
530531
}
531532
}
532533

533-
func transferCompleted(withErrorCode errorCode: Int?) {
534+
func transferCompleted(withErrorInfo errorInfo: URLErrorInfo?) {
534535
// At this point the transfer is complete and we can decide what to do.
535536
// If everything went well, we will simply forward the resulting data
536537
// to the delegate. But in case of redirects etc. we might send another
537538
// request.
538539
guard case .transferInProgress(let ts) = internalState else { fatalError("Transfer completed, but it wasn't in progress.") }
539540
guard let request = task?.currentRequest else { fatalError("Transfer completed, but there's no current request.") }
540-
guard errorCode == nil else {
541+
guard errorInfo == nil else {
541542
internalState = .transferFailed
542-
failWith(errorCode: errorCode!, request: request)
543+
failWith(errorCode: errorInfo!.code, errorDescription: errorInfo!.description, request: request)
543544
return
544545
}
545546

@@ -557,7 +558,7 @@ extension _HTTPURLProtocol: _EasyHandleDelegate {
557558
completeTask()
558559
case .failWithError(let errorCode):
559560
internalState = .transferFailed
560-
failWith(errorCode: errorCode, request: request)
561+
failWith(errorCode: errorCode, errorDescription: "Completion failure", request: request)
561562
case .redirectWithRequest(let newRequest):
562563
redirectFor(request: newRequest)
563564
}
@@ -575,6 +576,10 @@ extension _HTTPURLProtocol: _EasyHandleDelegate {
575576
}
576577
}
577578

579+
internal struct URLErrorInfo {
580+
let code: Int, description: String?
581+
}
582+
578583
extension _HTTPURLProtocol {
579584
/// The is independent of the public `state: URLSessionTask.State`.
580585
enum _InternalState {

Foundation/URLSession/http/MultiHandle.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,14 @@ fileprivate extension URLSession._MultiHandle {
214214
}
215215
let easyHandle = easyHandles[idx]
216216
// Find the NSURLError code
217-
let errorCode = easyHandle.urlErrorCode(for: easyCode)
218-
completedTransfer(forEasyHandle: easyHandle, errorCode: errorCode)
217+
let errorInfo = easyHandle.urlErrorCode(for: easyCode).map {
218+
URLErrorInfo(code: $0, description: String(cString: CFURLSessionErrorDescription(easyCode.value)))
219+
}
220+
completedTransfer(forEasyHandle: easyHandle, errorInfo: errorInfo)
219221
}
220222
/// Transfer completed.
221-
func completedTransfer(forEasyHandle handle: _EasyHandle, errorCode: Int?) {
222-
handle.completedTransfer(withErrorCode: errorCode)
223+
func completedTransfer(forEasyHandle handle: _EasyHandle, errorInfo: URLErrorInfo?) {
224+
handle.completedTransfer(withErrorInfo: errorInfo)
223225
}
224226
}
225227

0 commit comments

Comments
 (0)