Swagger-codegen: [Swift] Getting 'CANCELLED' for incorrect credentials instead of 401

Created on 9 Jun 2016  路  3Comments  路  Source: swagger-api/swagger-codegen

Description

I am building an app that has a sign in using http basic authentication. If the credentials are correct, it works fine. However for invalid credentials, I cannot access the status code (to test for 401) and the errors localizedDescription shows: cancelled

Swagger-codegen version

generated online

Swagger declaration file content or url
Command line used for generation

Steps to reproduce

Log in using HTTP Basic auth with incorrect credentials

Related issues

Suggest a Fix

pass the status code even if there is a failure.

Swift Bug help wanted

Most helpful comment

in my codebase I'm adding this at the bottom - that helps this issue and I'm gonna make a PR to bring into this tree shortly.

private func processRequest(request: Request, _ managerId: String, _ completion: (response: Response<T>?, error: ErrorType?) -> Void) {

    if let credential = self.credential {
        request.authenticate(usingCredential: credential)
    }

    request.responseJSON(options: .AllowFragments) { response in
        managerStore.removeValueForKey(managerId)

        // AlamoFire doesn't automatically check status responses for error codes
        // so we do it manually here - also we could call validate() on the AlamoFire
        // request but it doesn't put the status code in the error message
        // which isn't too helpful in the client code
        if request.response?.statusCode >= 400 {
            let error = NSError(domain: (request.response?.URL?.URLString)!, code: (request.response?.statusCode)!, userInfo: nil)
            error.log()
            completion(response: nil, error: error)
            return
        }

All 3 comments

@dylan-colaco am I correct in saying that you need access to the status code of the HTTP response to determine the next action in your app (e.g. ask users to try again)?

cc @jaz-ah @Edubits

Yes, that is correct. As far as I can see, the status code of the HTTP response is accessible only for a successful response at the moment, not for unsuccessful responses(like 401).

in my codebase I'm adding this at the bottom - that helps this issue and I'm gonna make a PR to bring into this tree shortly.

private func processRequest(request: Request, _ managerId: String, _ completion: (response: Response<T>?, error: ErrorType?) -> Void) {

    if let credential = self.credential {
        request.authenticate(usingCredential: credential)
    }

    request.responseJSON(options: .AllowFragments) { response in
        managerStore.removeValueForKey(managerId)

        // AlamoFire doesn't automatically check status responses for error codes
        // so we do it manually here - also we could call validate() on the AlamoFire
        // request but it doesn't put the status code in the error message
        // which isn't too helpful in the client code
        if request.response?.statusCode >= 400 {
            let error = NSError(domain: (request.response?.URL?.URLString)!, code: (request.response?.statusCode)!, userInfo: nil)
            error.log()
            completion(response: nil, error: error)
            return
        }
Was this page helpful?
0 / 5 - 0 ratings