使用AlamofireObjectMapper与Alamofire + ObjectMapper进行错误处理

Eya*_*uda 5 ios swift alamofire objectmapper

目前我正在使用Alamofire + ObjectMapper.我想改用AlamofireObjectMapper.但我有一个问题:如何处理api内容级错误?

例如,当我尝试保存新用户时:如果我发送到服务器的所有详细信息都有效,那么我的服务器将新用户详细信息返回为json.但如果我发送错误的细节(在应用程序逻辑级别),api将返回一个json,只显示一条错误消息,如"此用户名已被占用"

目前我正在使用Alamofire返回json:

Alamofire.request(myURL, method: .post, parameters: dic, encoding: JSONEncoding.default).validate().responseString { response in
                switch response.result {
                case .success:

                    if let JSON = response.result.value {

                            callback(JSON)

                    } else {
                        print("error with response.result.value")}

                case .failure:

                    debugPrint(request)
                    //alert to user 

                }

            }
Run Code Online (Sandbox Code Playgroud)

然后,在回调中,我检查json以查看它是否看起来像用户结构或错误结构(如果它是用户结构,我使用ObjectMapper使其成为User对象):

MyAPI.saveUser(user){
                    (response) in

                    // response to json (to check error)
                    if (MyAPI.JSONToDictionary(response)!)["error"] != nil{

                        // Application level error (logic)
                        print((MyAPI.JSONToDictionary(response)!)["error"])

                    }
                    else{

                        //Using ObjectMapper - response to User object 
                        let responsedUser = MyAPI.jsonToUser(response)

                        self.performSegue(withIdentifier: "userDetailsSegue", sender: self)

                    }
                }
Run Code Online (Sandbox Code Playgroud)

问题是:AlamofireObjectMapper将响应直接映射到用户.我如何检查映射是否成功并处理它没有的情况?如何获取服务器发送的错误消息?

谢谢!