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

To be able to catch the generated exception from validate_request (Feature Request) #85

Open
rafaelcapucho opened this issue Jul 24, 2024 · 1 comment

Comments

@rafaelcapucho
Copy link

rafaelcapucho commented Jul 24, 2024

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!

@Beetroit
Copy link

Beetroit commented Sep 7, 2024

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.

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

No branches or pull requests

2 participants