AlamofireObjectMapper icon indicating copy to clipboard operation
AlamofireObjectMapper copied to clipboard

How do I print error json from responseObject ?

Open tariq235 opened this issue 7 years ago • 2 comments

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 ?

tariq235 avatar Oct 12 '17 08:10 tariq235

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

AnilkumarRabhasa avatar Dec 21 '17 09:12 AnilkumarRabhasa

Me too !

WestFR avatar Apr 03 '18 20:04 WestFR