Skip to content

Commit cd24709

Browse files
committed
Merge branch 'develop/0.4' into feature/no-singleton-API-instance
2 parents cd25e09 + d65dc02 commit cd24709

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

APIKit/APIKit.swift

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ public class API {
8080
return internalDefaultURLSession
8181
}
8282

83+
public class var acceptableStatusCodes: [Int] {
84+
return [Int](200..<300)
85+
}
86+
8387
// build NSURLRequest
8488
public class func URLRequest(method: Method, _ path: String, _ parameters: [String: AnyObject] = [:]) -> NSURLRequest? {
8589
if let components = NSURLComponents(URL: baseURL(), resolvingAgainstBaseURL: true) {
@@ -135,10 +139,15 @@ public class API {
135139
}
136140

137141
let statusCode = (URLResponse as? NSHTTPURLResponse)?.statusCode ?? 0
138-
if !contains(200..<300, statusCode) {
139-
let userInfo = [NSLocalizedDescriptionKey: "received status code that represents error"]
140-
let error = NSError(domain: APIKitErrorDomain, code: statusCode, userInfo: userInfo)
141-
dispatch_async(mainQueue, { handler(.Failure(Box(error))) })
142+
if !contains(self.acceptableStatusCodes, statusCode) {
143+
let error: NSError = {
144+
switch self.responseBodyParser().parseData(data) {
145+
case .Success(let box): return self.responseErrorFromObject(box.unbox)
146+
case .Failure(let box): return box.unbox
147+
}
148+
}()
149+
150+
dispatch_async(mainQueue) { handler(failure(error)) }
142151
return
143152
}
144153

@@ -150,8 +159,8 @@ public class API {
150159
let error = NSError(domain: APIKitErrorDomain, code: 0, userInfo: userInfo)
151160
return failure(error)
152161
}
153-
154162
}
163+
155164
dispatch_async(mainQueue, { handler(mappedResponse) })
156165
}
157166

@@ -166,6 +175,12 @@ public class API {
166175
return nil
167176
}
168177
}
178+
179+
public class func responseErrorFromObject(object: AnyObject) -> NSError {
180+
let userInfo = [NSLocalizedDescriptionKey: "received status code that represents error"]
181+
let error = NSError(domain: APIKitErrorDomain, code: 0, userInfo: userInfo)
182+
return error
183+
}
169184
}
170185

171186
public class URLSessionDelegate: NSObject, NSURLSessionDelegate, NSURLSessionDataDelegate {

APIKitTests/APITests.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ class APITests: XCTestCase {
1717
override class func responseBodyParser() -> ResponseBodyParser {
1818
return .JSON(readingOptions: nil)
1919
}
20+
21+
override class func responseErrorFromObject(object: AnyObject) -> NSError {
22+
return NSError(domain: "MockAPIErrorDomain", code: 10000, userInfo: nil)
23+
}
2024

2125
class Endpoint {
2226
class Get: Request {
@@ -103,7 +107,8 @@ class APITests: XCTestCase {
103107
OHHTTPStubs.stubRequestsPassingTest({ request in
104108
return true
105109
}, withStubResponse: { request in
106-
return OHHTTPStubsResponse(data: NSData(), statusCode: 400, headers: nil)
110+
let data = NSJSONSerialization.dataWithJSONObject([:], options: nil, error: nil)!
111+
return OHHTTPStubsResponse(data: data, statusCode: 400, headers: nil)
107112
})
108113

109114
let expectation = expectationWithDescription("wait for response")
@@ -116,8 +121,8 @@ class APITests: XCTestCase {
116121

117122
case .Failure(let box):
118123
let error = box.unbox
119-
assertEqual(error.domain, APIKitErrorDomain)
120-
assertEqual(error.code, 400)
124+
assertEqual(error.domain, "MockAPIErrorDomain")
125+
assertEqual(error.code, 10000)
121126
}
122127

123128
expectation.fulfill()

0 commit comments

Comments
 (0)