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

python: use custom handler for validation error #487

Merged
merged 2 commits into from
Dec 5, 2024

Conversation

kyoto7250
Copy link
Member

@kyoto7250 kyoto7250 commented Nov 28, 2024

close #471
バリデーションを {"message": } の形式で返却する

@kyoto7250 kyoto7250 self-assigned this Nov 28, 2024
@kyoto7250 kyoto7250 changed the title python: use custom handler for validation error wip: python: use custom handler for validation error Nov 30, 2024
@@ -28,12 +29,21 @@ class PostInitializeResponse(BaseModel):


@app.exception_handler(HTTPStatus.INTERNAL_SERVER_ERROR)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここだけstatus codeを渡しているのが見づらい気がしたので、かわりにExceptionを渡すようにしたほうがいいと思います

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exception 全てを受けるのですが、調査したところ簡単に出来なさそうでした。
拾えれば拾おうと思っていたのですが、他言語でも基本的にフレームワーク依存だと思いましたので、
この対応は無しで進めれればと考えています

fastapi/fastapi#4025

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど、すべて拾うようなexception handlerは作れないんですね。了解です!

@kyoto7250 kyoto7250 force-pushed the python_custom_handler_for_validator branch from dee45ca to 33042df Compare December 5, 2024 08:56
@kyoto7250 kyoto7250 changed the title wip: python: use custom handler for validation error python: use custom handler for validation error Dec 5, 2024
@app.exception_handler(HTTPStatus.INTERNAL_SERVER_ERROR)
def internal_exception_handler(_request: Request, exc: Exception) -> JSONResponse:
@app.exception_handler(SQLAlchemyError)
def sql_alchemy_error_handler(_: Request, exc: SQLAlchemyError) -> JSONResponse:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SQLAlchemyErrorがパッケージのExceptionの基底で、__str__ が定義されているので str(exc) で問題ない

https://github.com/sqlalchemy/sqlalchemy/blob/5ded16fae8abfc31d43430cb25757fb434c37ba2/lib/sqlalchemy/exc.py#L72-L115

) -> JSONResponse:
return JSONResponse(
status_code=HTTPStatus.METHOD_NOT_ALLOWED,
content={"message": str(exc.errors())},
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kyoto7250 kyoto7250 requested a review from zhanpon December 5, 2024 09:02
@zhanpon zhanpon merged commit e9ed4e7 into main Dec 5, 2024
4 checks passed
@zhanpon zhanpon deleted the python_custom_handler_for_validator branch December 5, 2024 09:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Python] バリデーションエラーのレスポンス形式を合わせる
2 participants