Created
January 12, 2017 17:42
-
-
Save khanlou/2c25c7674162322d35ee8bda9e32ef50 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct APIError: NetworkError { | |
let json: JSON | |
let apiCode: Int? | |
let message: String? | |
let httpResponse: HTTPURLResponse | |
var statusCode: Int { | |
return httpResponse.statusCode | |
} | |
init?(json: JSON, httpResponse: HTTPURLResponse) { | |
guard let errorJSON = json["error"] as? [String: Any] else { | |
return nil | |
} | |
let apiCode = errorJSON["errorCode"] as? Int | |
let message = errorJSON["errorMessage"] as? String ?? json["errorMessage"] as? String | |
self.init(json: json, apiCode: apiCode, message: message, httpResponse: httpResponse) | |
} | |
init(json: JSON, apiCode: Int?, message: String?, httpResponse: HTTPURLResponse) { | |
self.json = json | |
self.apiCode = apiCode | |
self.message = message | |
self.httpResponse = httpResponse | |
} | |
} | |
extension APIError: LocalizedError, CustomNSError { | |
var errorDescription: String? { | |
return message | |
} | |
var errorCode: Int { | |
return apiCode ?? -1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment