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

[REQ] [Python] Generated Python clients should be smarter about what exceptions it throws #2151

Closed
philcluff opened this issue Feb 13, 2019 · 3 comments

Comments

@philcluff
Copy link

Is your feature request related to a problem? Please describe.

In Python, its idiomatic to throw exceptions when things don't go your way. The code generated by OpenAPI Generator is consistent with this, and throws a generic ApiException whenever the HTTP status code returned is not between 200 and 299.

However, it's also idiomatic to throw meaningful, actionable exceptions, for example in the case where an API 404s because an unknown object has been requested, it would be idiomatic to throw a NotFoundException. In the OpenAPI Generator generated python, While you can work out that the error was a 404 by looking at the HTTP data in the ApiException, this is inelegant.

Describe the solution you'd like

We can improve the default behavior of the code generated by:

  • Create 4 new subclasses of ApiException
    • NotFoundException
    • UnauthorizedException
    • ForbiddenException
    • ServiceException
  • Update rest.py so that...
    • NotFoundException is thrown on 404s
    • UnauthorizedException is thrown on 401s
    • ForbiddenException is thrown on 403s
    • ServiceException is thrown on 500 => 599

Describe alternatives you've considered

None

Additional context

I have updated templates which generate this. If the consensus is that this is useful, please let me know, and I'll submit a PR with the changes.

Cheers,

@rienafairefr
Copy link
Contributor

+1 to this idea

@ximon18
Copy link

ximon18 commented Jan 6, 2020

Hi @geneticgenesis,

So far the feedback has been only positive. Could you please create the PR?

Cheers,

Ximon (not part of the openapi-generator project, just another user)

@anusha94
Copy link

Hi @geneticgenesis
+1 for this feature!
Have you considered creating a PR?

Kylmakalle pushed a commit to Kylmakalle/openapi-generator that referenced this issue Dec 4, 2020
* Subclass Python exceptions:
- UnauthorizedException (401)
- ForbiddenException (403)
- NotFoundException (404)
- ServiceException [500 - 599]

Fixes OpenAPITools#2151

* add generated sample code

* use Python 2 flavor inheritance

* regenerate samples
spacether pushed a commit that referenced this issue Dec 5, 2020
* Subclass Python exceptions (#7321)

* Subclass Python exceptions:
- UnauthorizedException (401)
- ForbiddenException (403)
- NotFoundException (404)
- ServiceException [500 - 599]

Fixes #2151

* add generated sample code

* use Python 2 flavor inheritance

* regenerate samples

* update samples

Co-authored-by: Ryan Rishi <ryan@ryanrishi.com>
Co-authored-by: William Cheng <wing328hk@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants