To be able to catch the generated exception from validate_request (Feature Request)
While @validate_request do wonders! it also caps some important information.
When a malformed input comes, it of course fails returning Bad Request.. but there is no way to get to know about it.
I'd like to log the malformed input, because maybe it is my client API that is sending it wrong, I'd like to know in which field is the problem, maybe I'd like to push it to Prometheus/Grafana, others might like to report to a Slack Channel because it might be due to an incompatibility due to a regression in the consumer side etc..
My suggestion is to accept an additional parameter, as shown:
async def my_function(exc: RequestSchemaValidationError):
malformed_payload = await request.get_data()
...
@validate_request(list[ProfitsDay], grab_exception=my_function)
async def add_day(data: list[ProfitsDay]) -> str:
...
Thx!
Personally, i find that defining app.errorhandler work fine here, whether you are using a blueprint setup or just a single app. Here's sample code i use.
# imports
from quart_schema import (RequestSchemaValidationError,
ResponseSchemaValidationError)
# region Error handlers
@app.errorhandler(RequestSchemaValidationError)
@app.errorhandler(ResponseSchemaValidationError)
async def validation_error(
error: RequestSchemaValidationError | ResponseSchemaValidationError,
):
print(error)
return BaseResponse(
message="bad request", validation_error=str(error.validation_error)
), 400
You can have the function do whatever you want with the error message, such as extra formatting. My code assumes you have BaseResponse model defined, else you can just use jsonify. Also, i am running python3.10.
Agree, app.errorhandler is the correct solution to this problem.