Skip to content

Commit bf3c799

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

File tree

6 files changed

+49
-17
lines changed

6 files changed

+49
-17
lines changed

CoreFoundation/URL.subproj/CFURLSessionInterface.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
//===----------------------------------------------------------------------===//
2020

2121
#include "CFURLSessionInterface.h"
22+
#include <CoreFoundation/CFString.h>
2223
#include <curl/curl.h>
2324

2425
FILE* aa = NULL;
@@ -31,6 +32,11 @@ static CFURLSessionMultiCode MakeMultiCode(CURLMcode value) {
3132
return (CFURLSessionMultiCode) { value };
3233
}
3334

35+
CFStringRef CFURLSessionCreateErrorDescription(int value) {
36+
const char *description = curl_easy_strerror(value);
37+
return CFStringCreateWithBytes(kCFAllocatorSystemDefault,
38+
(const uint8_t *)description, strlen(description), kCFStringEncodingUTF8, NO);
39+
}
3440

3541
CFURLSessionEasyHandle _Nonnull CFURLSessionEasyHandleInit() {
3642
return curl_easy_init();
@@ -135,6 +141,7 @@ CFURLSessionEasyCode CFURLSessionInit(void) {
135141
return MakeEasyCode(curl_global_init(CURL_GLOBAL_SSL));
136142
}
137143

144+
int const CFURLSessionEasyErrorSize = { CURL_ERROR_SIZE + 1 };
138145

139146
CFURLSessionEasyCode const CFURLSessionEasyCodeOK = { CURLE_OK };
140147
CFURLSessionEasyCode const CFURLSessionEasyCodeUNSUPPORTED_PROTOCOL = { CURLE_UNSUPPORTED_PROTOCOL };

CoreFoundation/URL.subproj/CFURLSessionInterface.h

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

51+
CF_EXPORT CFStringRef _Nonnull CFURLSessionCreateErrorDescription(int value);
52+
53+
CF_EXPORT int const CFURLSessionEasyErrorSize;
54+
5155
/// CURLcode
5256
CF_EXPORT CFURLSessionEasyCode const CFURLSessionEasyCodeOK; // CURLE_OK
5357
CF_EXPORT CFURLSessionEasyCode const CFURLSessionEasyCodeUNSUPPORTED_PROTOCOL; // CURLE_UNSUPPORTED_PROTOCOL

Foundation/NSString.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ extension unichar : ExpressibleByUnicodeScalarLiteral {
2020
}
2121
}
2222

23+
// Placeholder for a future implementation
24+
public func NSLocalizedString(_ key: String, comment: String?) -> String {
25+
return key
26+
}
27+
2328
#if os(OSX) || os(iOS)
2429
internal let kCFStringEncodingMacRoman = CFStringBuiltInEncodings.macRoman.rawValue
2530
internal let kCFStringEncodingWindowsLatin1 = CFStringBuiltInEncodings.windowsLatin1.rawValue

Foundation/URLSession/http/EasyHandle.swift

Lines changed: 6 additions & 4 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+
internal lazy var errorBuffer = [UInt8](repeating: 0, count: Int(CFURLSessionEasyErrorSize))
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(withError error: NSError?) {
94+
delegate?.transferCompleted(withError: error)
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(withError error: NSError?)
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.
@@ -146,7 +147,8 @@ extension _EasyHandle {
146147
/// Set error buffer for error messages
147148
/// - SeeAlso: https://curl.haxx.se/libcurl/c/CURLOPT_ERRORBUFFER.html
148149
func set(errorBuffer buffer: UnsafeMutableBufferPointer<UInt8>?) {
149-
try! CFURLSession_easy_setopt_ptr(rawHandle, CFURLSessionOptionERRORBUFFER, buffer?.baseAddress ?? nil).asError()
150+
let buffer = buffer ?? errorBuffer.withUnsafeMutableBufferPointer { $0 }
151+
try! CFURLSession_easy_setopt_ptr(rawHandle, CFURLSessionOptionERRORBUFFER, buffer.baseAddress).asError()
150152
}
151153
/// Request failure on HTTP response >= 400
152154
func set(failOnHTTPErrorCode flag: Bool) {

Foundation/URLSession/http/HTTPURLProtocol.swift

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ fileprivate extension _HTTPURLProtocol {
136136
// NSURLErrorNoPermissionsToReadFile
137137
// NSURLErrorFileDoesNotExist
138138
self.internalState = .transferFailed
139-
failWith(errorCode: errorCode(fileSystemError: e), request: request)
139+
let error = NSError(domain: NSURLErrorDomain, code: errorCode(fileSystemError: e),
140+
userInfo: [NSLocalizedDescriptionKey: "File system error"])
141+
failWith(error: error, request: request)
140142
return
141143
}
142144

@@ -330,17 +332,19 @@ internal extension _HTTPURLProtocol {
330332
case stream(InputStream)
331333
}
332334

333-
func failWith(errorCode: Int, request: URLRequest) {
335+
func failWith(error: NSError, request: URLRequest) {
334336
//TODO: Error handling
335337
let userInfo: [String : Any]? = request.url.map {
336338
[
339+
NSUnderlyingErrorKey: error,
337340
NSURLErrorFailingURLErrorKey: $0,
338341
NSURLErrorFailingURLStringErrorKey: $0.absoluteString,
342+
NSLocalizedDescriptionKey: NSLocalizedString(error.localizedDescription, comment: nil)
339343
]
340344
}
341-
let error = URLError(_nsError: NSError(domain: NSURLErrorDomain, code: errorCode, userInfo: userInfo))
342-
completeTask(withError: error)
343-
self.client?.urlProtocol(self, didFailWithError: error)
345+
let urlError = URLError(_nsError: NSError(domain: NSURLErrorDomain, code: error.code, userInfo: userInfo))
346+
completeTask(withError: urlError)
347+
self.client?.urlProtocol(self, didFailWithError: urlError)
344348
}
345349
}
346350

@@ -530,16 +534,16 @@ extension _HTTPURLProtocol: _EasyHandleDelegate {
530534
}
531535
}
532536

533-
func transferCompleted(withErrorCode errorCode: Int?) {
537+
func transferCompleted(withError error: NSError?) {
534538
// At this point the transfer is complete and we can decide what to do.
535539
// If everything went well, we will simply forward the resulting data
536540
// to the delegate. But in case of redirects etc. we might send another
537541
// request.
538542
guard case .transferInProgress(let ts) = internalState else { fatalError("Transfer completed, but it wasn't in progress.") }
539543
guard let request = task?.currentRequest else { fatalError("Transfer completed, but there's no current request.") }
540-
guard errorCode == nil else {
544+
guard error == nil else {
541545
internalState = .transferFailed
542-
failWith(errorCode: errorCode!, request: request)
546+
failWith(error: error!, request: request)
543547
return
544548
}
545549

@@ -557,7 +561,9 @@ extension _HTTPURLProtocol: _EasyHandleDelegate {
557561
completeTask()
558562
case .failWithError(let errorCode):
559563
internalState = .transferFailed
560-
failWith(errorCode: errorCode, request: request)
564+
let error = NSError(domain: NSURLErrorDomain, code: errorCode,
565+
userInfo: [NSLocalizedDescriptionKey: "Completion failure"])
566+
failWith(error: error, request: request)
561567
case .redirectWithRequest(let newRequest):
562568
redirectFor(request: newRequest)
563569
}

Foundation/URLSession/http/MultiHandle.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,20 @@ 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+
var error: NSError?
218+
if let errorCode = easyHandle.urlErrorCode(for: easyCode) {
219+
let errorDescription = easyHandle.errorBuffer[0] != 0 ?
220+
String(cString: easyHandle.errorBuffer) :
221+
CFURLSessionCreateErrorDescription(easyCode.value)._swiftObject
222+
error = NSError(domain: NSURLErrorDomain, code: errorCode, userInfo: [
223+
NSLocalizedDescriptionKey: errorDescription
224+
])
225+
}
226+
completedTransfer(forEasyHandle: easyHandle, error: error)
219227
}
220228
/// Transfer completed.
221-
func completedTransfer(forEasyHandle handle: _EasyHandle, errorCode: Int?) {
222-
handle.completedTransfer(withErrorCode: errorCode)
229+
func completedTransfer(forEasyHandle handle: _EasyHandle, error: NSError?) {
230+
handle.completedTransfer(withError: error)
223231
}
224232
}
225233

0 commit comments

Comments
 (0)