Skip to content

Commit

Permalink
Adding response data to Failure
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Sargent committed Mar 1, 2016
1 parent e9e9b0a commit a8f6a44
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion LKAPI.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = "LKAPI"
s.version = "0.1.2"
s.version = "0.1.3"
s.summary = "Wrapper around Alamofire"
s.description = "Wrapper built around Alamofire to make working with APIs easier"
s.homepage = "https://github.com/Lightningkite/LKAPI"
Expand Down
11 changes: 7 additions & 4 deletions Pod/Classes/API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ public struct Failure {
public let error: ErrorType
public let message: String?
public let code: Int?
public let data: [String: AnyObject]

public init(error: ErrorType, message: String? = nil, code: Int? = nil) {
public init(error: ErrorType, message: String? = nil, code: Int? = nil, data: [String: AnyObject] = [String: AnyObject]()) {
self.error = error
self.message = message
self.code = code
self.data = data
}
}

Expand Down Expand Up @@ -128,23 +130,24 @@ public class API {
//else there really was an error, so parse any data, and return a failure
else if let error = response.result.error {
var message = "There was an error"
var responseData = [String: AnyObject]()

do {
if let data = response.data, values = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) as? [String: AnyObject] {
responseData = values
if let error = values["message"] as? String {
message = error
}
}
} catch _ {
if let data = response.data, error = String(data: data, encoding: NSUTF8StringEncoding) {
print(error)
message = error
}
}

print("Status code \(response.response?.statusCode). message: \(message)\n")
print("Status code \(response.response?.statusCode). message: \(message)\ndata: \(responseData)\n\n")

failure?(Failure(error: error, message: message, code: response.response?.statusCode))
failure?(Failure(error: error, message: message, code: response.response?.statusCode, data: responseData))
}

return
Expand Down

0 comments on commit a8f6a44

Please sign in to comment.