Skip to content

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

Catch exception type of Exception and process body request #575

Closed
Hedgehogues opened this issue Sep 30, 2019 · 5 comments
Closed

Catch exception type of Exception and process body request #575

Hedgehogues opened this issue Sep 30, 2019 · 5 comments
Labels
question Question or problem question-migrate

Comments

@Hedgehogues
Copy link

Description

How can I catch exception type of Exception and process body request?

Additional context

My example

@app.exception_handler(Exception)
async def exception_callback(request: requests.Request, exc: Exception):
request_json = await request.json() # Here i want a body, query_params, headers and path of request
print(request_json)

@app.post('/create_workflow')
async def create_workflow():
raise Exception(13)

@Hedgehogues Hedgehogues added the question Question or problem label Sep 30, 2019
@Hedgehogues Hedgehogues changed the title [QUESTION] Catch exception type of Exception and process body request Sep 30, 2019
@euri10
Copy link
Contributor

euri10 commented Sep 30, 2019

mmm I'm not sure, but I don't think you can consume body in a middleware (see #394)

getting the other parameters you mentioned is possible though (note that the Request below is from starlette and not requests I think you imported the wrong one), but should you need the body that won't happen

I'd ask in Starlette if I were you

import pytest
from async_asgi_testclient import TestClient
from fastapi import FastAPI
from starlette.requests import Request

app = FastAPI()


@app.exception_handler(Exception)
async def exception_callback(request: Request, exc: Exception):
    print(request.query_params)
    print(request.headers)
    print(request.path_params)
    print(exc.args)


@app.post("/create_workflow")
async def create_workflow():
    raise Exception(13)


@pytest.mark.asyncio
async def test_raise_exc():
    async with TestClient(app) as client:
        with pytest.raises(Exception) as pe:
            response = await client.post("/create_workflow")

@Hedgehogues
Copy link
Author

Hedgehogues commented Sep 30, 2019

Yes, I saw it. But this code cannot extract body of request. Example of printing:

Headers({'host': 'localhost:8000', 'connection': 'keep-alive', 'content-length': '82', 'accept': 'application/json', 'origin': 'http://localhost:8000', 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36', 'content-type': 'application/json', 'referer': 'http://localhost:8000/docs', 'accept-encoding': 'gzip, deflate, br', 'accept-language': 'en-US,en;q=0.9', 'cookie': 'redirect_to=%252F'})

{}

(123,)

I cannot get body of request

@euri10
Copy link
Contributor

euri10 commented Sep 30, 2019

yes as I pointed out in the above linked issue #394, this is not something you can achieve in a middleware.
you might want to read encode/starlette#495 (comment) as well and ask on Starlette

@tiangolo
Copy link
Owner

A new way to handle these cases was added in #589.

The docs are here: https://fastapi.tiangolo.com/tutorial/custom-request-and-route/

@github-actions
Copy link
Contributor

Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.

@tiangolo tiangolo reopened this Feb 28, 2023
Repository owner locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #8104 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Question or problem question-migrate
Projects
None yet
Development

No branches or pull requests

3 participants