Moya: Failure code 999

Hello, I use Mayo to wrap my network in my swift app. I want to connect with my own API.

I first created a test a User case :

public enum UserAPI {
    case Information
}

extension UserAPI: TargetType {
    public var baseURL: NSURL { return NSURL(string: "http://127.0.0.1:3000/user")!}

    public var path: String {
        switch self {
        case .Information:
            return "/"
        }
    }

    public var method: Moya.Method {
        return .GET
    }

    public var parameters: [String: AnyObject]? {
        return nil
    }

    public var sampleData: NSData {
        switch self {
        case .Information():
            return "{}".dataUsingEncoding(NSUTF8StringEncoding)!
        }
    }

    public func constructProvider(target: UserAPI) -> Endpoint<UserAPI> {
        let token = TokenAccess.sharedInstance.token!
        let endpoint: Endpoint<UserAPI> = Endpoint<UserAPI>(URL: "\(target.baseURL)\(target.path)", sampleResponseClosure: {.NetworkResponse(200, target.sampleData)}, method: target.method, parameters: target.parameters)
        return endpoint.endpointByAddingHTTPHeaderFields(["Authorization": "Bearer \(token)"])
    }
}

Then the call :

let provider = MoyaProvider<UserAPI>()
provider.request(UserAPI.Information) { (result) -> () in
    print("result request : \(result)")
}

And then the result :

result request : .Failure(Underlying(Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLKey=http://127.0.0.1:3000/user/, NSLocalizedDescription=cancelled, NSErrorFailingURLStringKey=http://127.0.0.1:3000/user/}))

The base URL is okay. I am making a transition, in a network abstraction, I used Alamofire previously, and it worked well. I am trying to move my code to Moya.

Thanks in advance for your help. 👍

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 19 (9 by maintainers)

Most upvoted comments

Are you retaining the provider anywhere? I’ve seen 999 errors when whatever is making the request is deallocated

Agreed, hard to debug. Moya tries to make it hard to crash the app 😉

Ash Furrow https://ashfurrow.com/

On April 15, 2016 at 11:30:35 AM, nikos kanellopoulos (notifications@github.com(mailto:notifications@github.com)) wrote:

@ashfurrow(https://github.com/ashfurrow) Shouldn’t this crash however? It is very hard to trace these kind of bugs.

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub(https://github.com/Moya/Moya/issues/412#issuecomment-210507282)

@remirobert - you bet! 😄

I just wanted to point out that the Basic Setup docs don’t 100% allude to the fact that you need to keep a reference to the provider, which I’m guessing is what’s responsible for (at least some of) the responses to the issue above.

@tomj it would be awesome if you could make a PR with how would you like to be informed about retaining the provider. 🙌