jason810496 commented on code in PR #44322:
URL: https://github.com/apache/airflow/pull/44322#discussion_r1856919097
##########
tests/api_fastapi/core_api/routes/public/test_connections.py:
##########
@@ -235,9 +235,7 @@ def test_post_should_respond_already_exist(self,
test_client, body):
# Another request
response = test_client.post("/public/connections/", json=body)
assert response.status_code == 409
- assert response.json() == {
- "detail": f"Connection with connection_id: `{TEST_CONN_ID}`
already exists",
- }
+ assert response.json() == {"detail": "Unique constraint violation"}
Review Comment:
After conducting some research, I have come up with a solution to add custom
error message handlers for specific endpoints. If a custom error handler is
provided, it will take precedence and be used before the default global error
handler.
The usage would look like this:
```python
from airflow.api_fastapi.common.exceptions import
register_unique_constraint_error_handler, BaseCustomErrorHandler
@connections_router.post(
"",
status_code=status.HTTP_201_CREATED,
responses=create_openapi_http_exception_doc(
[status.HTTP_409_CONFLICT]
), # handled by global exception handler
)
def post_connection(
post_body: ConnectionBody,
session: Annotated[Session, Depends(get_session)],
) -> ConnectionResponse:
"""Create connection entry."""
try:
helpers.validate_key(post_body.connection_id, max_length=200)
except Exception as e:
raise HTTPException(status.HTTP_400_BAD_REQUEST, f"{e}")
connection = Connection(**post_body.model_dump(by_alias=True))
session.add(connection)
return connection
class _ConnectionUniqueConstraintErrorHandler(BaseCustomErrorHandler):
def exception_handler(self, request_schema: ConnectionBody):
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail=f"Connection with connection_id:
`{request_schema.connection_id}` already exists",
)
register_unique_constraint_error_handler(post_connection,
_ConnectionUniqueConstraintErrorHandler(ConnectionBody))
```
Not sure is this clean enough for providing verbose error message for
specific endpoints, WDYT ?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]