Skip to content

Instantly share code, notes, and snippets.

@khanlou
Created January 12, 2017 17:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khanlou/2c25c7674162322d35ee8bda9e32ef50 to your computer and use it in GitHub Desktop.
Save khanlou/2c25c7674162322d35ee8bda9e32ef50 to your computer and use it in GitHub Desktop.
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