Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.mapError() not working? #27

Closed
pablorecio opened this issue Jun 21, 2017 · 2 comments
Closed

.mapError() not working? #27

pablorecio opened this issue Jun 21, 2017 · 2 comments

Comments

@pablorecio
Copy link

pablorecio commented Jun 21, 2017

We are using Promissum in a legacy project that we are trying to upgrade to Swift 3, but I found some issues when doing so.

Say that I have something like:

class Api {
  // ... 
  fileprivate func requestJSON(_ path: Path) -> Promise<SuccessResponse<AnyObject>, ApiError> {
    let url = path.url(baseUrl)
    return manager
      .request(url)
      .responseJSONPromise()
      .mapError(self.errorTransformer)
  }
  
  // MARK: Error helpers
  
  fileprivate func errorTransformer(_ error: ErrorResponse) -> ApiError {
    return .networkingError(error.result as NSError)
  }
    
  // ...
}

Where ApiError is:

import Foundation
import Statham

public enum ApiError : Error {
  case notFound
  case networkingError(NSError)
  case jsonDecodingError(Error)
}

extension ApiError: CustomStringConvertible {
  public var description: String {
    switch self {
    case .notFound: return NSLocalizedString("Not Found", comment: "Api Error: resource not found")
    case .networkingError(let error): return error.localizedDescription
    case .jsonDecodingError(let error):
      #if DEBUG
        if let error = error as? JsonDecodeError {
          print(error.fullDescription)
        }
      #endif
      return NSLocalizedString("Server error", comment: "Api Error: Server error")
    }
  }
}

The problem comes when using .mapError(), where the compiler complains about "Argument passed to call that takes no arguments". Digging in the github, I can see that indeed there is this following method mapError() that does not take any arguments and that is a new addition since the 0.5.0 version.

I can see however the right method here, but it seems that extension is sort of overriding the right one? Or am I missing something important here?

@tomlokhorst
Copy link
Owner

The swift compiler often shows the "wrong" type of error in case of a type error.

Looking at your code, I'm guessing you're using the responseJSONPromise from the Alamofire extension. That returns a Promise<SuccessResponse<Any>, ErrorResponse>, not a Promise<SuccessResponse<AnyObject>, ErrorResponse> as you're using here.

@pablorecio
Copy link
Author

Wow, Xcode does not help sometimes.

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants