Thanks to visit codestin.com
Credit goes to github.com

Skip to content

add body to RequestValidationError for easier tracing and debugging#853

Merged
tiangolo merged 4 commits into
fastapi:masterfrom
aviramha:feature/request_body_in_exception
Jan 17, 2020
Merged

add body to RequestValidationError for easier tracing and debugging#853
tiangolo merged 4 commits into
fastapi:masterfrom
aviramha:feature/request_body_in_exception

Conversation

@aviramha

Copy link
Copy Markdown
Contributor

FastAPI is awesome, and it's schema validation is one of it's key elements.
While debugging issues in production, the current errors data isn't sufficient to understand if it's a bad schema definition (when the API is used by multiple sources, in different ways) or really a client defect.
Adding the body data to the error can help trace problems and reproducing the issue.

@codecov

codecov Bot commented Jan 13, 2020

Copy link
Copy Markdown

Codecov Report

Merging #853 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #853   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files         289    291    +2     
  Lines        7595   7638   +43     
=====================================
+ Hits         7595   7638   +43
Impacted Files Coverage Δ
..._tutorial/test_handling_errors/test_tutorial006.py 100% <100%> (ø)
docs/src/handling_errors/tutorial006.py 100% <100%> (ø)
..._tutorial/test_handling_errors/test_tutorial005.py 100% <100%> (ø) ⬆️
docs/src/handling_errors/tutorial005.py 100% <100%> (ø) ⬆️
fastapi/routing.py 100% <100%> (ø) ⬆️
fastapi/exceptions.py 100% <100%> (ø) ⬆️
docs/src/sql_databases_peewee/sql_app/database.py 100% <0%> (ø) ⬆️
docs/src/sql_databases_peewee/sql_app/main.py 100% <0%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3eca945...21aadbb. Read the comment docs.

@victoraugustolls

Copy link
Copy Markdown

Can this be made optional? For sensitive bodies one might not want it to be in the error, depending on how they log them.

@aviramha

Copy link
Copy Markdown
Contributor Author

Can this be made optional? For sensitive bodies one might not want it to be in the error, depending on how they log them.

Handling and logging of the error hasn't changed, so it's up to the user of the framework what to do with this data.
For example in my application I wish to have this so when I catch and handle this exception, I'd be able to get the body that caused the parsing exception.

@victoraugustolls

victoraugustolls commented Jan 13, 2020

Copy link
Copy Markdown

For example using sentry sdk that logs the whole exception automatically out of the box. The body will be logged and will be up to the user to reconfigure his sentry to don’t log the body part. So this might change how logging should be handled in some applications.

@aviramha

Copy link
Copy Markdown
Contributor Author

For example using sentry sdk that logs the whole exception automatically out of the box. The body will be logged and will be up to the user to reconfigure his sentry to don’t log the body part. So this might change how logging should be handled in some applications.

My opinion then is it's upto the middleware to handle this case, because I can't think of a pretty way to do this in a configurable manner...

@victoraugustolls

Copy link
Copy Markdown

Me neither :D, just worried because this is something that caught me in the past (using sentry). I think it’s nice to have this information btw

@aviramha

Copy link
Copy Markdown
Contributor Author

Me neither :D, just worried because this is something that caught me in the past (using sentry). I think it’s nice to have this information btw

Let's see what @tiangolo or other maintainer has to say then :)

@aviramha aviramha requested review from dmontagu and tiangolo January 16, 2020 10:57
@tiangolo

Copy link
Copy Markdown
Member

Thanks for your contribution @aviramha ! 🚀

And thanks for the discussion and points touched @victoraugustolls .


So, about Sentry, the current behavior is, if you have an app like:

import sentry_sdk
from pydantic import BaseModel
from sentry_sdk import capture_exception
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
from starlette.responses import PlainTextResponse

from fastapi import FastAPI
from fastapi.exceptions import RequestValidationError

app = FastAPI()


class User(BaseModel):
    name: str
    age: int


@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc: Exception):
    capture_exception(exc)
    return PlainTextResponse(str(exc), status_code=400)


@app.post("/users/")
def create_user(user: User):
    return user


sentry_sdk.init(dsn="https://[email protected]/6789")

app.add_middleware(SentryAsgiMiddleware)

And you post invalid data like:

{
  "name": "string",
  "age": "a"
}

As that body is part of the traceback for the RequestValidationError, it will be sent to Sentry (I just tested it).

But it would only happen if the developer manually captures the exception for Sentry.

If the developer doesn't do it, nothing happens.

And this is all with the current implementation, before this PR.


The possibly sensitive data won't be sent automatically to Sentry, as by default, the exception is caught in FastAPI's code. Sensitive data would only be sent to Sentry if the developer explicitly sends the data or the exception. As @aviramha says, it would be up to the middleware how to handle it. If the developer wants to capture the exception and send it to Sentry, then that developer would have to make sure that any sensitive data is not a problem. And that still holds independently of this PR.

What I see this PR would do is allow to more easily handle and debug those bodies. In fact, it could help reducing sensitive data sent to a third party like Sentry, as it would be easier to log the failing data without having to resort to capture it and send it to Sentry or similar to be able to debug it.


I'll make a couple of tweaks to the PR first but I think this is a good idea 💡 👍 🎉 🍰

@aviramha

aviramha commented Jan 17, 2020

Copy link
Copy Markdown
Contributor Author

Thanks for the elaborate comment! I'm glad you liked my idea.
Can't wait for it to get upstream.

@tiangolo tiangolo merged commit 5db99a2 into fastapi:master Jan 17, 2020
@tiangolo

Copy link
Copy Markdown
Member

Thanks for your contribution! 🍰 🚀

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.

3 participants