Browse Source

Deprecated errorWithCode APIs and updated NSURLError* domains to NSURLErrorDomain.

Cédric Luthi 9 years ago
parent
commit
8d9f79a86f

+ 11 - 0
Source/Error.swift

@@ -47,6 +47,7 @@ public struct Error {
 
         - returns: An `NSError` with the given error code and failure reason.
     */
+    @available(*, deprecated=3.4.0)
     public static func errorWithCode(code: Code, failureReason: String) -> NSError {
         return errorWithCode(code.rawValue, failureReason: failureReason)
     }
@@ -59,8 +60,18 @@ public struct Error {
 
         - returns: An `NSError` with the given error code and failure reason.
     */
+    @available(*, deprecated=3.4.0)
     public static func errorWithCode(code: Int, failureReason: String) -> NSError {
         let userInfo = [NSLocalizedFailureReasonErrorKey: failureReason]
         return NSError(domain: Domain, code: code, userInfo: userInfo)
     }
+
+    static func error(domain domain: String = Error.Domain, code: Code, failureReason: String) -> NSError {
+        return error(domain: domain, code: code.rawValue, failureReason: failureReason)
+    }
+
+    static func error(domain domain: String = Error.Domain, code: Int, failureReason: String) -> NSError {
+        let userInfo = [NSLocalizedFailureReasonErrorKey: failureReason]
+        return NSError(domain: domain, code: code, userInfo: userInfo)
+    }
 }

+ 15 - 21
Source/MultipartFormData.swift

@@ -217,7 +217,7 @@ public class MultipartFormData {
             appendBodyPart(fileURL: fileURL, name: name, fileName: fileName, mimeType: mimeType)
         } else {
             let failureReason = "Failed to extract the fileName of the provided URL: \(fileURL)"
-            setBodyPartError(Error.errorWithCode(NSURLErrorBadURL, failureReason: failureReason))
+            setBodyPartError(code: NSURLErrorBadURL, failureReason: failureReason)
         }
     }
 
@@ -245,8 +245,7 @@ public class MultipartFormData {
 
         guard fileURL.fileURL else {
             let failureReason = "The file URL does not point to a file URL: \(fileURL)"
-            let error = Error.errorWithCode(NSURLErrorBadURL, failureReason: failureReason)
-            setBodyPartError(error)
+            setBodyPartError(code: NSURLErrorBadURL, failureReason: failureReason)
             return
         }
 
@@ -261,8 +260,7 @@ public class MultipartFormData {
         }
 
         guard isReachable else {
-            let error = Error.errorWithCode(NSURLErrorBadURL, failureReason: "The file URL is not reachable: \(fileURL)")
-            setBodyPartError(error)
+            setBodyPartError(code: NSURLErrorBadURL, failureReason: "The file URL is not reachable: \(fileURL)")
             return
         }
 
@@ -277,8 +275,7 @@ public class MultipartFormData {
             where NSFileManager.defaultManager().fileExistsAtPath(path, isDirectory: &isDirectory) && !isDirectory else
         {
             let failureReason = "The file URL is a directory, not a file: \(fileURL)"
-            let error = Error.errorWithCode(NSURLErrorBadURL, failureReason: failureReason)
-            setBodyPartError(error)
+            setBodyPartError(code: NSURLErrorBadURL, failureReason: failureReason)
             return
         }
 
@@ -301,8 +298,7 @@ public class MultipartFormData {
 
         guard let length = bodyContentLength else {
             let failureReason = "Could not fetch attributes from the file URL: \(fileURL)"
-            let error = Error.errorWithCode(NSURLErrorBadURL, failureReason: failureReason)
-            setBodyPartError(error)
+            setBodyPartError(code: NSURLErrorBadURL, failureReason: failureReason)
             return
         }
 
@@ -312,8 +308,7 @@ public class MultipartFormData {
 
         guard let stream = NSInputStream(URL: fileURL) else {
             let failureReason = "Failed to create an input stream from the file URL: \(fileURL)"
-            let error = Error.errorWithCode(NSURLErrorCannotOpenFile, failureReason: failureReason)
-            setBodyPartError(error)
+            setBodyPartError(code: NSURLErrorCannotOpenFile, failureReason: failureReason)
             return
         }
 
@@ -413,10 +408,10 @@ public class MultipartFormData {
 
         if let path = fileURL.path where NSFileManager.defaultManager().fileExistsAtPath(path) {
             let failureReason = "A file already exists at the given file URL: \(fileURL)"
-            throw Error.errorWithCode(NSURLErrorBadURL, failureReason: failureReason)
+            throw Error.error(domain: NSURLErrorDomain, code: NSURLErrorBadURL, failureReason: failureReason)
         } else if !fileURL.fileURL {
             let failureReason = "The URL does not point to a valid file: \(fileURL)"
-            throw Error.errorWithCode(NSURLErrorBadURL, failureReason: failureReason)
+            throw Error.error(domain: NSURLErrorDomain, code: NSURLErrorBadURL, failureReason: failureReason)
         }
 
         let outputStream: NSOutputStream
@@ -425,7 +420,7 @@ public class MultipartFormData {
             outputStream = possibleOutputStream
         } else {
             let failureReason = "Failed to create an output stream with the given URL: \(fileURL)"
-            throw Error.errorWithCode(NSURLErrorCannotOpenFile, failureReason: failureReason)
+            throw Error.error(domain: NSURLErrorDomain, code: NSURLErrorCannotOpenFile, failureReason: failureReason)
         }
 
         outputStream.open()
@@ -492,7 +487,7 @@ public class MultipartFormData {
                 encoded.appendBytes(buffer, length: bytesRead)
             } else if bytesRead < 0 {
                 let failureReason = "Failed to read from input stream: \(inputStream)"
-                error = Error.errorWithCode(.InputStreamReadFailed, failureReason: failureReason)
+                error = Error.error(domain: NSURLErrorDomain, code: .InputStreamReadFailed, failureReason: failureReason)
                 break
             } else {
                 break
@@ -551,7 +546,7 @@ public class MultipartFormData {
                 try writeBuffer(&buffer, toOutputStream: outputStream)
             } else if bytesRead < 0 {
                 let failureReason = "Failed to read from input stream: \(inputStream)"
-                throw Error.errorWithCode(.InputStreamReadFailed, failureReason: failureReason)
+                throw Error.error(domain: NSURLErrorDomain, code: .InputStreamReadFailed, failureReason: failureReason)
             } else {
                 break
             }
@@ -592,7 +587,7 @@ public class MultipartFormData {
 
                 if bytesWritten < 0 {
                     let failureReason = "Failed to write to output stream: \(outputStream)"
-                    throw Error.errorWithCode(.OutputStreamWriteFailed, failureReason: failureReason)
+                    throw Error.error(domain: NSURLErrorDomain, code: .OutputStreamWriteFailed, failureReason: failureReason)
                 }
 
                 bytesToWrite -= bytesWritten
@@ -655,9 +650,8 @@ public class MultipartFormData {
 
     // MARK: - Private - Errors
 
-    private func setBodyPartError(error: NSError) {
-        if bodyPartError == nil {
-            bodyPartError = error
-        }
+    private func setBodyPartError(code code: Int, failureReason: String) {
+        guard bodyPartError == nil else { return }
+        bodyPartError = Error.error(domain: NSURLErrorDomain, code: code, failureReason: failureReason)
     }
 }

+ 5 - 5
Source/ResponseSerialization.swift

@@ -161,7 +161,7 @@ extension Request {
 
             guard let validData = data else {
                 let failureReason = "Data could not be serialized. Input data was nil."
-                let error = Error.errorWithCode(.DataSerializationFailed, failureReason: failureReason)
+                let error = Error.error(code: .DataSerializationFailed, failureReason: failureReason)
                 return .Failure(error)
             }
 
@@ -209,7 +209,7 @@ extension Request {
 
             guard let validData = data else {
                 let failureReason = "String could not be serialized. Input data was nil."
-                let error = Error.errorWithCode(.StringSerializationFailed, failureReason: failureReason)
+                let error = Error.error(code: .StringSerializationFailed, failureReason: failureReason)
                 return .Failure(error)
             }
             
@@ -227,7 +227,7 @@ extension Request {
                 return .Success(string)
             } else {
                 let failureReason = "String could not be serialized with encoding: \(actualEncoding)"
-                let error = Error.errorWithCode(.StringSerializationFailed, failureReason: failureReason)
+                let error = Error.error(code: .StringSerializationFailed, failureReason: failureReason)
                 return .Failure(error)
             }
         }
@@ -280,7 +280,7 @@ extension Request {
 
             guard let validData = data where validData.length > 0 else {
                 let failureReason = "JSON could not be serialized. Input data was nil or zero length."
-                let error = Error.errorWithCode(.JSONSerializationFailed, failureReason: failureReason)
+                let error = Error.error(code: .JSONSerializationFailed, failureReason: failureReason)
                 return .Failure(error)
             }
 
@@ -338,7 +338,7 @@ extension Request {
 
             guard let validData = data where validData.length > 0 else {
                 let failureReason = "Property list could not be serialized. Input data was nil or zero length."
-                let error = Error.errorWithCode(.PropertyListSerializationFailed, failureReason: failureReason)
+                let error = Error.error(code: .PropertyListSerializationFailed, failureReason: failureReason)
                 return .Failure(error)
             }
 

+ 2 - 2
Source/Validation.swift

@@ -80,7 +80,7 @@ extension Request {
                 return .Success
             } else {
                 let failureReason = "Response status code was unacceptable: \(response.statusCode)"
-                return .Failure(Error.errorWithCode(.StatusCodeValidationFailed, failureReason: failureReason))
+                return .Failure(Error.error(code: .StatusCodeValidationFailed, failureReason: failureReason))
             }
         }
     }
@@ -160,7 +160,7 @@ extension Request {
                 failureReason = "Response content type was missing and acceptable content type does not match \"*/*\""
             }
 
-            return .Failure(Error.errorWithCode(.ContentTypeValidationFailed, failureReason: failureReason))
+            return .Failure(Error.error(code: .ContentTypeValidationFailed, failureReason: failureReason))
         }
     }
 

+ 6 - 6
Tests/MultipartFormDataTests.swift

@@ -824,7 +824,7 @@ class MultipartFormDataFailureTestCase: BaseTestCase {
         XCTAssertNotNil(encodingError, "encoding error should not be nil")
 
         if let error = encodingError {
-            XCTAssertEqual(error.domain, "com.alamofire.error", "error domain does not match expected value")
+            XCTAssertEqual(error.domain, NSURLErrorDomain, "error domain does not match expected value")
             XCTAssertEqual(error.code, NSURLErrorBadURL, "error code does not match expected value")
 
             if let failureReason = error.userInfo[NSLocalizedFailureReasonErrorKey] as? String {
@@ -855,7 +855,7 @@ class MultipartFormDataFailureTestCase: BaseTestCase {
         XCTAssertNotNil(encodingError, "encoding error should not be nil")
 
         if let error = encodingError {
-            XCTAssertEqual(error.domain, "com.alamofire.error", "error domain does not match expected value")
+            XCTAssertEqual(error.domain, NSURLErrorDomain, "error domain does not match expected value")
             XCTAssertEqual(error.code, NSURLErrorBadURL, "error code does not match expected value")
 
             if let failureReason = error.userInfo[NSLocalizedFailureReasonErrorKey] as? String {
@@ -887,7 +887,7 @@ class MultipartFormDataFailureTestCase: BaseTestCase {
         XCTAssertNotNil(encodingError, "encoding error should not be nil")
 
         if let error = encodingError {
-            XCTAssertEqual(error.domain, "com.alamofire.error", "error domain does not match expected value")
+            XCTAssertEqual(error.domain, NSURLErrorDomain, "error domain does not match expected value")
             XCTAssertEqual(error.code, NSURLErrorBadURL, "error code does not match expected value")
 
             if let failureReason = error.userInfo[NSLocalizedFailureReasonErrorKey] as? String {
@@ -918,7 +918,7 @@ class MultipartFormDataFailureTestCase: BaseTestCase {
         XCTAssertNotNil(encodingError, "encoding error should not be nil")
 
         if let error = encodingError {
-            XCTAssertEqual(error.domain, "com.alamofire.error", "error domain does not match expected value")
+            XCTAssertEqual(error.domain, NSURLErrorDomain, "error domain does not match expected value")
             XCTAssertEqual(error.code, NSURLErrorBadURL, "error code does not match expected value")
 
             if let failureReason = error.userInfo[NSLocalizedFailureReasonErrorKey] as? String {
@@ -960,7 +960,7 @@ class MultipartFormDataFailureTestCase: BaseTestCase {
         XCTAssertNotNil(encodingError, "encoding error should not be nil")
 
         if let encodingError = encodingError {
-            XCTAssertEqual(encodingError.domain, "com.alamofire.error", "encoding error domain does not match expected value")
+            XCTAssertEqual(encodingError.domain, NSURLErrorDomain, "encoding error domain does not match expected value")
             XCTAssertEqual(encodingError.code, NSURLErrorBadURL, "encoding error code does not match expected value")
         }
     }
@@ -986,7 +986,7 @@ class MultipartFormDataFailureTestCase: BaseTestCase {
         XCTAssertNotNil(encodingError, "encoding error should not be nil")
 
         if let encodingError = encodingError {
-            XCTAssertEqual(encodingError.domain, "com.alamofire.error", "encoding error domain does not match expected value")
+            XCTAssertEqual(encodingError.domain, NSURLErrorDomain, "encoding error domain does not match expected value")
             XCTAssertEqual(encodingError.code, NSURLErrorBadURL, "encoding error code does not match expected value")
         }
     }

+ 1 - 1
Tests/ResultTests.swift

@@ -25,7 +25,7 @@ import Foundation
 import XCTest
 
 class ResultTestCase: BaseTestCase {
-    let error = Error.errorWithCode(.StatusCodeValidationFailed, failureReason: "Status code validation failed")
+    let error = Error.error(code: .StatusCodeValidationFailed, failureReason: "Status code validation failed")
 
     // MARK: - Is Success Tests