Getting the Alamofire NSError
Closed this issue · 6 comments
I'm unable to fetch the Alamofire response error when making a request.
The only error I am able to retrieve is the error
The operation couldn’t be completed. ObjectMapper failed to serialize response.
i'm using version: 2.1
Alamofire.request(.GET, url, parameters: nil, encoding: .JSON)
.responseArray { (response: Response<[MyModel], NSError>) in
if let error = response.result.error {
print("Error: \(error.localizedDescription)")
errorBlock(error)
return
}
completionBlock(response.result.value!)
}
I got the exact same problem, also using version 2.1.
Is there any update on this?
I'm not sure why this would be happening...
See the two Response serializers below (they both return the Alamofire error immediately if it exists):
https://github.com/tristanhimmelman/AlamofireObjectMapper/blob/master/AlamofireObjectMapper/AlamofireObjectMapper.swift#L38
https://github.com/tristanhimmelman/AlamofireObjectMapper/blob/master/AlamofireObjectMapper/AlamofireObjectMapper.swift#L107
What kind of error are you expecting to see that is not returned?
You might want to inspect the NSHTTPURLResponse (response.response
) to get the statusCode and NSData that was returned from the request
Yeah i have the same problem, it only retrieves the error of "The operation couldn’t be completed. ObjectMapper failed to serialize response". Could it be posible that the callback is retrieving the success or the failure of the objectmapper action
instead of the request?
By example, at a service is retrieving me 500 status error code but the function keeps going to the success case.
Is there any other way to get the status code or the error message?
After some research it turns out this is a feature of Alamofire. To get the error, you need to call the validate()
function on the request. See the Alamofire docs: https://github.com/Alamofire/Alamofire#validation
Thank so much, now it works as expected.
I am having the same issue i could not retrive the status as expected.
Alamofire.upload(data!, with: (myrouter)
.uploadProgress { progress in // main queue by default
// print("Upload Progress: (progress.fractionCompleted)")
}.validate()
.responseJSON { [weak self] response in{
// what can i put here so that i can retrive the status code like 200 or 500
NSURLErrorCannotFindHost
NSURLErrorNetworkConnectionLost
}