Simon
Simon

Reputation: 6523

Alamofire Error.localizedDescription - The operation couldn’t be completed

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!

Upvotes: 2

Views: 2701

Answers (1)

Zedenem
Zedenem

Reputation: 2559

To be able to call .localizedDescription on your custom BackendAPIErrorManager conforming to the Error protocol, you will need to add this code:

extension BackendAPIErrorManager: LocalizedError {
  var errorDescription: String? {
    switch self {
      case .apiProvidedError:
        return reason
    }
  }
}

The reason is a bit foggy (as the documentation on how the Error protocol works is not too detailed yet) but basically, in Swift 3, to be able to provide localized error descriptions (and other relative info), you need to conform to the new LocalizedError protocol.

You can find more details here: How to provide a localized description with an Error type in Swift?

Upvotes: 3

Related Questions