[BUG] FastAPI exception handler only catches exact exceptions
@EvanR-Dev ya está trabajando en esto.
Desde el 5/12/2023.
Evaluación
Este issue todavía no se ha evaluado.
Descripción
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
- Lenguaje dominante
- Python
- Estrellas
- 357
- Forks
- 117
- Merge medio
- 13 d 12 h
- PR fusionados (30 d)
- 2
Preparar el entorno
Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de Azure/azure-functions-python-worker
-
[Bug] ASGI cookie conversion serializes an absent Domain attribute and breaks __Host- cookiesPosiblemente ocupada @tharun06 la tomó hace 24 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
-
Dificultad 5/5 Más de una semana Aptitud para principiantes 28/100
Azure/azure-functions-python-worker#1908 · 2 comentarios ·
-
Dificultad 4/5 3-5 días Aptitud para principiantes 55/100
Azure/azure-functions-python-worker#1906 · 1 comentario ·
-
Dificultad 5/5 Más de una semana Aptitud para principiantes 35/100
-
[Python 3.14 proxy worker] Missing sys.modules["__main__"] breaks multiprocessing spawnPosiblemente ocupada Un pull request vinculado a esta issue está abierto o ya se fusionó. Abierto
Dificultad 4/5 3-5 días Aptitud para principiantes 55/100
Todos los issues de Azure/azure-functions-python-worker
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
huggingface/sentence-transformers#4186 ·
Los mantenedores suelen responder en 5 días
-
documentation
Dificultad 1/5 Menos de una hora Aptitud para principiantes 92/100
QuantEcon/QuantEcon.py#988 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
fabriziosalmi/certmate#1247 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
Los mantenedores suelen responder en 1 día
-
documentation good first issue
Dificultad 2/5 Medio día Aptitud para principiantes 72/100
sujithputta02/Esapay#13 ·