dmontagu / fastapi-utils

Reusable utilities for FastAPI

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG] @repeat_every will have no stack trace in terminal

shizidushu opened this issue · comments

Describe the bug
No error will show

To Reproduce

from fastapi import FastAPI

from fastapi_utils.tasks import repeat_every


app = FastAPI()

items = {}


@app.on_event("startup")
@repeat_every(seconds=60)
async def startup_event():
    raise Exception
    items["foo"] = {"name": "Fighters"}
    items["bar"] = {"name": "Tenders"}


@app.get("/items/{item_id}")
async def read_items(item_id: str):
    return items[item_id]

uvicorn app.main:app --reload, the raise Exception should produce a stack trace in terminal, but none.

Environment:

  • Windows
Python 3.7.7 (tags/v3.7.7:d7c567b08f, Mar 10 2020, 10:41:24) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import fastapi_utils
>>> import fastapi
>>> import pydantic.utils
>>> print(fastapi_utils.__version__)
0.2.1
>>> print(fastapi.__version__)
0.58.0
>>> print(pydantic.utils.version_info())
             pydantic version: 1.5.1
            pydantic compiled: True
                 install path: D:\git\testFastapiutils\venv\Lib\site-packages\pydantic
               python version: 3.7.7 (tags/v3.7.7:d7c567b08f, Mar 10 2020, 10:41:24) [MSC v.1900 64 bit (AMD64)]
                     platform: Windows-10-10.0.18362-SP0
     optional deps. installed: []

@shizidushu FastAPI seems to be handling and ignoring exceptions by default. There is a parameter to control this behavior.

To receive traceback in the logs, you can try following

@repeat_every(seconds=60 , raise_exceptions=True)
async def startup_event():

Note that FastAPI will stop executing the function if exceptions are not handled.
Ideally, FastAPI should handle by default and also log the traceback.

@deepio-oc Thanks. It works.