Swagger-codegen: [Swift] generated ClientSDK - excute callback response type

Created on 11 Feb 2016  ·  19Comments  ·  Source: swagger-api/swagger-codegen

Are using Swagger codegen, when the execute, request to API (in my case UserAPI) looks like can only model that is mapped to.
There is no problem if http code is 200, when receiving the other StatusCode, if the model is the return comes to the other model (in my case Error) could only given model when you request excute it troubled they become.
Do you not allow you to specify each type made of a excute?
Well I want to use. help me.

Swift Bug

Most helpful comment

You can use switch operator like in this answer https://github.com/swagger-api/swagger-codegen/issues/4190, but a little different way:

 if let err = error as? ErrorResponse {

    switch(err){

    case .Error(409, let dataValue, _):

        //now there are possibility to use dataValue object

    break

}

All 19 comments

Hi @wing328,

@junyun0516 works with me and additional phenomenon would be help. Our YAML file includes below:

  responses:
    '200':
      description: User info
      schema:
        $ref: '#/definitions/User'
    '400':
      description: Error
      schema:
        $ref: '#/definitions/Error'

definitions:
Error:
type: object
required:
- reason
properties:
reason:
type: string
User:
type: object
required:
- id
properties:
id:
type: integer
format: int64
api_token:
type: string

Swagger Client for Swift works well when sever returns with status code 200, but when returns 400, client seems can't get Error object. I don't have experience with Swift so this would be problem of own code, but @junyun0516 is in trouble with client.

Ruby or Python clients work well with above YAML when server returns 400 or others like as response object contains status code and response body.

Is there any way to handling return object following status code?

Best Regards,
Inska

@junyun0516 would suggest you guys to pull the latest master if you guys have not done so as recently there are a few PRs to enhancement the Swift client.

@wing328, just pulled master, run "mvn install", and re-generate swift client. However, above situation not resolved..

@inska thanks for performing more tests with the latest master. I'm afraid more works need to be done to improve the exceptions handling in Swift client.

One suggestion is to create an ApiException object (which extends the basic Exception object) in Swift to capture the HTTP status code, header, body, etc similar to how other languages capture exceptions (4xx, 5xx):

(not sure if Swift has similar mechanism for error handling)

cc @jaz-ah who has recently made a lot of contribution to the Swift client.

yeah the error handling isn't too great - right now I have added code in my local branch to AlamofireImplementations.swift right after request.responseJSON that grabs any error > 400 and returns that in the promise's error - you don't get any custom error objects but at least you get an NSError that describes the url/statuscode that hit the error...

        // 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
        }

this still doesn't solve the custom error handling though - but that would be the place to modify the code to add it...

@jaz-ah do you mind submitting a PR for the solution you proposed?

@wing328 sure I can push a change in this week.

Any news on this, not being able to handle errors gracefully is a major flaw in the generated client in my opinion. Is any work done on improving handling error-responses from APIs, or are you looking for contributions for this matter?

sorry guys - it looks like some error handling has been added recently, so after the Swift3 merge let's look at what we have and we can augment that.

Hi @jaz-ah, are there any in-progress branches that are working on addressing this issue that I could look at for inspiration for a temporary solution? Thanks!

@normand1 ah sorry to just get back here - could you try the swift3 language - that has some better work for this....

@jaz-ah thanks, I'll give it a shot

@normand1 may I know if you've a chance to test the Swift3 API client?

Hi @wing328, the problem still exists! I tested Swift3 API client.
generated error is enum
public enum ErrorResponse : Error { case Error(Int, Data?, Error) }

but it is not possible to get data from this error
there are only possibility to get status code from this answer https://github.com/swagger-api/swagger-codegen/issues/4190

I would suggest you to test with the Swift3 API client in 2.3.0 branch as
well (which contains breaking changes)

On Sat, Apr 29, 2017 at 6:48 PM, inovik notifications@github.com wrote:

Hi @wing328 https://github.com/wing328, the problem still exists! I
tested Swift3 API client.
generated error is enum
`public enum ErrorResponse : Error {

case Error(Int, Data?, Error)

}`

but it is not possible to get data from this error
there are only possibility to get status code from this answer #4190
https://github.com/swagger-api/swagger-codegen/issues/4190


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/swagger-api/swagger-codegen/issues/2110#issuecomment-298161617,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AA5BdABahAlKpb3R5-6XWrU0jLkPdA1lks5r0xWEgaJpZM4HX4S8
.

You will need clone the repo, checkout 2.3.0 branch and build the JAR
locally.

On Apr 29, 2017 8:00 PM, "inovik" notifications@github.com wrote:

Can you please help me install version 2.3.0?
I use OSX and when I try call brew install swagger-codegen installed
version is 2.2.2
also I try call
brew install swagger-codegen 2.3.0 -> Error: No formulae found in taps.

От 29 апреля 2017 г. в 14:08:58, wing328 ([email protected])
написал:

I would suggest you to test with the Swift3 API client in 2.3.0 branch as
well (which contains breaking changes)

On Sat, Apr 29, 2017 at 6:48 PM, inovik notifications@github.com wrote:

Hi @wing328 https://github.com/wing328, the problem still exists! I
tested Swift3 API client.
generated error is enum
`public enum ErrorResponse : Error {

case Error(Int, Data?, Error)

}`

but it is not possible to get data from this error
there are only possibility to get status code from this answer #4190
https://github.com/swagger-api/swagger-codegen/issues/4190


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<
https://github.com/swagger-api/swagger-codegen/issues/
2110#issuecomment-298161617
,
or mute the thread
<
https://github.com/notifications/unsubscribe-auth/AA5BdABahAlKpb3R5-
6XWrU0jLkPdA1lks5r0xWEgaJpZM4HX4S8

.


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
2110#issuecomment-298162491>,
or mute the thread
AGbuOOBoToRehotJy6xyChHsN5JFm2iTks5r0xpJgaJpZM4HX4S8>
.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/swagger-api/swagger-codegen/issues/2110#issuecomment-298164668,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AA5BdFmiCXSFHDDUv_u4cizDHhNowAAjks5r0yZIgaJpZM4HX4S8
.

I am also having the same issue.
How do I get the Data from ErrorResponse?

You can use switch operator like in this answer https://github.com/swagger-api/swagger-codegen/issues/4190, but a little different way:

 if let err = error as? ErrorResponse {

    switch(err){

    case .Error(409, let dataValue, _):

        //now there are possibility to use dataValue object

    break

}

You answer helped me a lot, I just had to change a few things for my swift3 generated api, mainly because I don't have the .Error enum case but HttpError(statusCode: Int, data: Data?, error: Error):

if error == nil {
    print("success")
} else {
    switch(error!){
    case .HttpError(statusCode: 404, data: _, error: _):
        print("not found")
        break
    default:
        print("other error")
    }
}
Was this page helpful?
0 / 5 - 0 ratings