Skip to content

Commit

Permalink
responseErrorFromObject receives URLResponse to retrive status code
Browse files Browse the repository at this point in the history
  • Loading branch information
yonekawa committed Sep 5, 2015
1 parent 9130ef4 commit a0f7fee
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
19 changes: 13 additions & 6 deletions APIKit/API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,18 @@ public class API {
dispatch_async(mainQueue) { handler(.failure(error)) }
return
}

let statusCode = (URLResponse as? NSHTTPURLResponse)?.statusCode ?? 0
if !contains(self.acceptableStatusCodes, statusCode) {

if (URLResponse as? NSHTTPURLResponse) == nil {
let userInfo = [NSLocalizedDescriptionKey: "response is not HTTPURLResponse."]
let error = NSError(domain: APIKitErrorDomain, code: 0, userInfo: userInfo)
dispatch_async(mainQueue) { handler(.failure(error)) }
return
}

let URLResponse = URLResponse as! NSHTTPURLResponse
if !contains(self.acceptableStatusCodes, URLResponse.statusCode) {
let error = self.responseBodyParser.parseData(data).analysis(
ifSuccess: { self.responseErrorFromObject($0) },
ifSuccess: { self.responseErrorFromObject($0, URLResponse: URLResponse) },
ifFailure: { $0 }
)

Expand Down Expand Up @@ -157,9 +164,9 @@ public class API {
}
}

public class func responseErrorFromObject(object: AnyObject) -> NSError {
public class func responseErrorFromObject(object: AnyObject, URLResponse: NSHTTPURLResponse) -> NSError {
let userInfo = [NSLocalizedDescriptionKey: "received status code that represents error"]
let error = NSError(domain: APIKitErrorDomain, code: 0, userInfo: userInfo)
let error = NSError(domain: APIKitErrorDomain, code: URLResponse.statusCode, userInfo: userInfo)
return error
}
}
Expand Down
2 changes: 1 addition & 1 deletion APIKitTests/APITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class APITests: XCTestCase {
return NSURL(string: "https://api.github.com")!
}

override class func responseErrorFromObject(object: AnyObject) -> NSError {
override class func responseErrorFromObject(object: AnyObject, URLResponse: NSURLResponse) -> NSError {
return NSError(domain: "MockAPIErrorDomain", code: 10000, userInfo: nil)
}

Expand Down

0 comments on commit a0f7fee

Please sign in to comment.