OpenAPITools / OpenAPITools/openapi-generator

[REQ] [Python] Add custom exceptions for HTTP status codes 409 (Conflict) and 422 (Unprocessable Entity)

Open
#20,244 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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

This issue expands on #2151.

Currently, the Python generator provides custom exception classes for the following HTTP error codes:

  • 400 (Bad Request)
  • 401 (Unauthorized)
  • 403 (Forbidden)
  • 404 (Not Found)

However, other common HTTP status codes like 409 (Conflict) and 422 (Unprocessable Entity) are not handled by custom exception classes:

  • 409 is often used to indicate a conflict in the request, such as trying to create a resource that already exists.
  • 422 is commonly used in frameworks like FastAPI to signal validation errors in the request payload.

The lack of dedicated exception classes for these codes makes error handling less intuitive, because developers need to catch a generic ApiException in order to inspect and classify the error.

Describe the solution you'd like

Add custom exception classes for HTTP status codes 409 and 422, similar to the existing custom exceptions for other 4XX codes. More specifically, something like:

if http_resp.status == 409:
    raise ConflictException(http_resp=http_resp, body=body, data=data)

if http_resp.status == 422:
    raise UnprocessableEntityException(http_resp=http_resp, body=body, data=data)

This modification would align with the current implementation while providing more convenient and meaningful error handling for these particular HTTP status codes.

Describe alternatives you've considered

The current alternative is going with something like:

try:
  # API call expected to produce a 409 or 422 goes here.
except ApiException as e:
  if e.status == 409:
    # Code expected to handle the 409 error goes here.
  elif e.status == 422:
    # Code expected to handle the 422 error goes here.
  else:
    raise

Although this code is valid, it's not as elegant as using dedicated exception classes. The need to manually inspect the status field to handle specific cases adds unnecessary boilerplate and detracts from the clarity of the error-handling logic.

Additional context

I'm ready to submit a PR with these changes for consideration.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Python generator's existing custom exception handling for HTTP statuses 400-404 and trace how generated clients select those exceptions. Done means status 409 raises ConflictException and status 422 raises UnprocessableEntityException, while other statuses retain their current behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.