I am trying to access the .localizedDescription
property through Alamofire's callback in the event that there is an error
I have an enum which handles the Error
types specifically similar to the Alamofire docs
enum BackendAPIErrorManager: Error {
case network(error: Error)
case apiProvidedError(reason: String) // this causes me problems
...
}
In my request, if there is an error I store the applicable error to the .failure
case Alamofire provides as follows:
private func resultsFromResponse(response: DataResponse<Any>) -> Result<Object> {
guard response.result.error == nil else {
print(response.result.error!)
return .failure(BackendAPIErrorManager.network(error: response.result.error!))
}
if let jsonDictionary = response.result.value as? [String: Any], let errorMessage = jsonDictionary["error"] as? String, let errorDescription = jsonDictionary["error_description"] as? String {
print("\(errorMessage): \(errorDescription)")
return .failure(BackendAPIErrorManager.apiProvidedError(reason: errorDescription))
}
....
}
where that method is called in:
func performRequest(completionHandler: @escaping (Result<someObject>) -> Void) {
Alamofire.request("my.endpoint").responseJSON {
response in
let result = resultsFromResponse(response: response)
completionHandler(result)
}
}
Then when I call the performRequest(:)
method, I check for the error in the completion handler:
performRequest { result in
guard result.error == nil else {
print("Error \(result.error!)") // This works
//prints apiProvidedError("Error message populated here that I want to log")
print(result.error!.localizedDescription) // This gives me the below error
}
}
In the event of an error where .failure(BackendAPIErrorManager.apiProvidedError(reason: errorDescription))
is returned, I receive the below error
The operation couldn’t be completed. (my_proj.BackendErrorManager error 1.)
How would I get the String
value from the returned Error? I have tried .localizedDescription
to no luck. Any help would be great!