This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
How to log more info on 422 unprocessable entity #3361
Comments
Just the other day I had a pretty absurd issue about exactly that My gui was doing a fetch request and thanks to the apple keyword crap, I set the content-type to The 422 error indicated that value wasn't a dict I spend too much time till I noticed the additional This would be avoided with better 422 error or a way to debug it |
@nickgieschen catch exception def register_exception(app: FastAPI):
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
exc_str = f'{exc}'.replace('\n', ' ').replace(' ', ' ')
# or logger.error(f'{exc}')
logger.error(request, exc_str)
content = {'status_code': 10422, 'message': exc_str, 'data': None}
return JSONResponse(content=content, status_code=status.HTTP_422_UNPROCESSABLE_ENTITY) after you can log error and get response and you can see issue 394 @ruslankrivoshein |
Thanks. Can you tell me what logger that is that you can pass request as the first argument? |
def log_message(request: Request, e):
logger.error('start error'.center(60, '*'))
logger.error(f'{request.method} {request.url}')
logger.error(f'error is {e}')
logger.error('end error'.center(60, '*'))
log_message(request, exc_str) or logger.error(exc_str) |
The above examples are excerpts of handling-errors documentation. import logging
from fastapi import FastAPI, Request, status
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
app = FastAPI()
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
exc_str = f'{exc}'.replace('\n', ' ').replace(' ', ' ')
logging.error(f"{request}: {exc_str}")
content = {'status_code': 10422, 'message': exc_str, 'data': None}
return JSONResponse(content=content, status_code=status.HTTP_422_UNPROCESSABLE_ENTITY) This will produce something along the lines of:
This error is not only bound to the header, but also if the data submitted does not match the |
presume it should be |
What is the significance of |
This should be the standard implementation IMHO... a beginner like me had to do quite a lot of digging simply because the error wasn't expressive enough :P |
wouldn't this catch all errors instead of only 422? and what about the 10422 like @dancaugherty mentioned? |
At least if |
yeah i agree, especially because there's no real way to log it properly if you'll get a 422 |
or it would be cool to define when creating the pydantic model...
|
@nickgieschen @Torxed @zwessels @dancaugherty @VBoB13 @JustinGuese please see codes
|
I see alot of answers here, which one is best for simply returning the field error to the user? Such as: "field" : pause is missing: ? Here is my code:
The problem is, that as soon as the scheme is not correct, i can not get further than line one, which means i can not use the RequestValidationError ? |
Thanks for the help here everyone! 👏 🙇 If that solves the original problem, then you can close this issue @nickgieschen ✔️
|
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
I get this a lot in development and can usually pretty quickly figure out what is wrong with my json payload. However, I'd love a way to see what exactly the validation error is when I get this error. Is there a way I can log what the specific schema validation error is on the server side?
The text was updated successfully, but these errors were encountered: