-
Notifications
You must be signed in to change notification settings - Fork 6
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
Conversation
webapp/python/app/main.py
Outdated
@@ -28,12 +29,21 @@ class PostInitializeResponse(BaseModel): | |||
|
|||
|
|||
@app.exception_handler(HTTPStatus.INTERNAL_SERVER_ERROR) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここだけstatus codeを渡しているのが見づらい気がしたので、かわりにException
を渡すようにしたほうがいいと思います
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exception 全てを受けるのですが、調査したところ簡単に出来なさそうでした。
拾えれば拾おうと思っていたのですが、他言語でも基本的にフレームワーク依存だと思いましたので、
この対応は無しで進めれればと考えています
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
なるほど、すべて拾うようなexception handlerは作れないんですね。了解です!
dee45ca
to
33042df
Compare
@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: |
There was a problem hiding this comment.
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)
で問題ない
) -> JSONResponse: | ||
return JSONResponse( | ||
status_code=HTTPStatus.METHOD_NOT_ALLOWED, | ||
content={"message": str(exc.errors())}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exc.errors()
で中身が見れる
https://fastapi.tiangolo.com/ja/tutorial/handling-errors/#requestvalidationerror
close #471
バリデーションを {"message": } の形式で返却する