Skip to content

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

Closed
@philcluff

Description

@philcluff

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,

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions