[BUG] FastAPI exception handler only catches exact exceptions
@EvanR-Dev đang làm issue này rồi.
Từ ngày 5/12/2023.
Đánh giá
Issue này chưa được đánh giá.
Mô tả
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
- Ngôn ngữ chính
- Python
- Star
- 357
- Fork
- 117
- Merge trung bình
- 13 ngày 12 giờ
- Pull request đã merge (30 ngày)
- 2
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của Azure/azure-functions-python-worker
-
[Bug] ASGI cookie conversion serializes an absent Domain attribute and breaks __Host- cookiesCó thể đã có người làm @tharun06 đã nhận 21 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 28/100
Azure/azure-functions-python-worker#1908 · 2 bình luận ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 55/100
Azure/azure-functions-python-worker#1906 · 1 bình luận ·
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 35/100
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 55/100
Tất cả issue của Azure/azure-functions-python-worker
Issue tương tự
-
bug
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
openwisp/openwisp-utils#777 ·
-
Boolean statistics (Covered, Confident, JaccardDistant) aggregate to 1.0 without float weightsĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 63/100
google-research/weatherbenchX#310 ·
Maintainer thường phản hồi trong vòng 3 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
huggingface/peft#3955 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
author-page correction
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 60/100
acl-org/acl-anthology#10313 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
AOSSIE-Org/Gitcord-GithubDiscordBot#147 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày