The JSONDecoder.decode method doesn't throw error when the incoming JSON doesn't match the data type.
I have a data model like this that maps the JSON to a user profile:
struct DrivetimeUserProfile: Codable {
var driverId: String?
var name: String?
var email: String?
var phoneNumber: String?
var city: String?
var state: String?
var experience: Int?
private enum CodingKeys: String, CodingKey {
case driverId = "driver_id"
case name = "name"
case email = "email"
case phoneNumber = "phone"
case city = "city"
case state = "state"
case experience = "experience"
}
}
When the username or password is incorrect, the server will return a JSON like this \"Failure\":\"Enter correct username and password.\". Which doesn't map to the user profile data model and the JSONDecoder().decode doesn't throw an error
Here is the full implementation:
func loginUser(from url: URL, with username: String, and password: String, completionHandler: @escaping (DrivetimeUserProfile?, DrivetimeAPIError.LoginError?) -> Void) {
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.cachePolicy = cachePolicy
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
let query = "email=\(username)&password=\(password)"
request.httpBody = query.data(using: .utf8)
let task = session.dataTask(with: request) { (data, response, error) in
guard let data = data else {
completionHandler(nil, .EmptyData)
return
}
do {
let userProfile = try JSONDecoder().decode(DrivetimeUserProfile.self, from: data)
self.userProfile = userProfile
completionHandler(userProfile, nil)
} catch (let error) {
completionHandler(nil, .CannotDecodeJson)
}
}
task.resume()
}
I need this completion handler to capture the thrown error so I can alert the user in my view controller of what the error is. Please advice
Thank you