[BUG] FastAPI exception handler only catches exact exceptions
@EvanR-Dev ci sta già lavorando.
Dal 5/12/2023.
Valutazione
Questa issue non è ancora stata valutata.
Descrizione
Investigative information
Please provide the following:
- Timestamp: Nov 22, 2023
- Function App name: running locally (reproduction of a production issue)
- Function name(s) (as appropriate): see above
- Core Tools version: ❯ func --version: 4.0.5455
Repro steps
Provide the steps required to reproduce the problem:
I'm using the AsgiMiddleware to wrap a FastApi app. If I add an exception_handler for Exception, then specific errors like ZeroDivisionError are not caught when running with func start, however, it works if I use uvicorn to run the app instead of func start. Please see https://github.com/ashic/azfn-fastapi-demo/blob/main/src/azfn_fastapi_demo/main.py
The main function is here:
from fastapi import FastAPI, Body, Response
from fastapi.responses import JSONResponse
import azure.functions as func
app = FastAPI()
@app.exception_handler(Exception)
async def catch_all(req, e:Exception):
return JSONResponse("Catch all handler")
# @app.exception_handler(ZeroDivisionError)
# async def zero_handler(req, e:ZeroDivisionError):
# return JSONResponse("Zero handler")
@app.post("/test")
async def test():
a = 20 / 0
return {
'content': "Hello"
}
main = func.AsgiMiddleware(app).main
If I remove the comments on the ZeroDivisionError handler, then running with func start cathes the divide by zero exception. However, with the code as is, when running with uvicorn, the Exception handler catches is, but running with func start, doesn't.
I use scripts/run-local.sh to run via uvicorn and scripts/run-local-az.sh to run with the azure function host.
Expected behavior
Provide a description of the expected behavior.
When running with func start, an Exception handler should catch all exceptions. The behaviour should be the same as when running with uvicorn.
Actual behavior
Provide a description of the actual behavior observed.
The behaviour differs from running with uvicorn. Only specific exceptions are caught.
Known workarounds
Provide a description of any known workarounds.
Other than writing a handler for every possible error, there doesn't seem to be one.
Contents of the requirements.txt file:
Provide the requirements.txt file to help us find out module related issues.
azure-functions==1.17.0
fastapi==0.104.1
uvicorn==0.24.0.post1
- Lingua principale
- Python
- Stelle
- 357
- Fork
- 117
- Merge medio
- 13g 12h
- PR unite (30g)
- 2
Preparare l'ambiente
Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di Azure/azure-functions-python-worker
-
[Bug] ASGI cookie conversion serializes an absent Domain attribute and breaks __Host- cookiesForse già presa @tharun06 l’ha presa 21 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 28/100
Azure/azure-functions-python-worker#1908 · 2 commenti ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 55/100
Azure/azure-functions-python-worker#1906 · 1 commento ·
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 35/100
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 55/100
Tutte le issue di Azure/azure-functions-python-worker
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
huggingface/peft#3955 ·
I maintainer di solito rispondono entro 1 giorno
-
author-page correction
Difficoltà 2/5 1-3 ore Idoneità per principianti 60/100
acl-org/acl-anthology#10313 ·
I maintainer di solito rispondono entro 1 giorno
-
anime duplicate exception request-theme
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 82/100
LizardByte/ThemerrDB#8926 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
huggingface/transformers#49556 ·
I maintainer di solito rispondono entro 1 giorno
-
area/security bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
mozilla-ai/otari#2245 ·
I maintainer di solito rispondono entro 1 giorno