1

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

1 Answer 1

3

That's the huge disadvantage if you declare all struct members thoughtlessly as optional.

If the key does not exist in the struct all members are set to nil and no error is thrown.

Sign up to request clarification or add additional context in comments.

2 Comments

What would be the remedy?
Remove the question marks then you'll get the error. Or add an optional Failure member

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.