tristanhimmelman / AlamofireObjectMapper

An Alamofire extension which converts JSON response data into swift objects using ObjectMapper
MIT License
2.66k stars 474 forks source link

How do I print error json from responseObject ? #232

Open tariq235 opened 6 years ago

tariq235 commented 6 years ago
Alamofire.request(URL, method: Endpoints.login.method, parameters: parameters, encoding: JSONEncoding.default, headers: nil)
            .validate()
            .responseObject { [weak self] (response: DataResponse<User>) in
                switch response.result {
                case .success(let user):
                    cb(.success(user))
                case .failure(let error):
                    log.error(error)
                    cb(.failure(error))
                }
        }

If I enter wrong password then I am unable to print the exact error message which api is sending it to me.

po error.localizedDescription I am always getting default message from alamofire i.e. "Response status code was unacceptable: 401.". It should print the dynamic error message like Invalid username and password.

Btw I can use responseJson to print the dynamic error message using

 .responseJSON { response in
                if response.response?.statusCode == 200 {
                    print("Success with JSON: \(response.result.value)")
                }
                else {
                    let error = response.result.value as! NSDictionary
                    let errorMessage = error.object(forKey: "message") as! String
                    print(errorMessage)
                }

But how I should do the same with responseObject ?

AnilkumarRabhasa commented 6 years ago

Did you got solution for responseObject? while I am also facing same issue now.

WestFR commented 6 years ago

Me too !