honeybadger-io / honeybadger-python

Send Python and Django errors to Honeybadger.
https://www.honeybadger.io/
MIT License
15 stars 25 forks source link

Reporting errors in fastapi repeating functions #118

Closed joshuap closed 7 months ago

joshuap commented 1 year ago

This is a bug report from a past user. @Kelvin4664 @subzero10 could we automatically report errors in this case? Or would it be better to manually try/catch the error in the function (and if so, can we update the docs)?

In FastAPI, I run functions every 60 seconds using the building mechanism in fastapi like this:

app = FastAPI()
@app.on_event("startup")
@repeat_every(seconds=60)
def every_minute_sync_job() -> None:
    # Do stuff
    pass

I want to catch the exceptions that occur in the every_minute_sync_job() function. I think it creates a separate thread for those, and therefore it doesn't seem to catch those. The docs i saw from honeybadger seemed to be specifically for api call and not for the repeated functions that i use.

joshuap commented 1 year ago

Here's some additional feedback:

I later found that the function has some optional arguments that may help in this case : "raise_exceptions" and "logger". https://fastapi-utils.davidmontague.xyz/user-guide/repeated-tasks/

I didn't had time to test it with Honeybadger.

Kelvin4664 commented 7 months ago

I did some digging into this one, Including the source code for fastapi_utils. The default behaviour is for errors to fail silently (And they don't get reported to honeybadger since these exceptions are caught by fastapi_utils) And indeed, there's an argument to change this behaviour by setting raise_exceptions to True and providing a logger. Honeybadger already provides a log handler that can be configured for this purpose.

subzero10 commented 7 months ago

Thank you @Kelvin4664. In that case, we should update our documentation (I created an issue to do that).