Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add convenience method for exception reporting #2792

Merged
merged 12 commits into from
Jul 17, 2023
Merged

Conversation

ahopkins
Copy link
Member

@ahopkins ahopkins commented Jul 17, 2023

#2724 adds a new signal to trigger on all exceptions: "server.lifecycle.exception"

This PR adds a convenience for setting up a task for capturing this signal to do something with it. The obvious use case is for error reporting that does not need to be in the way of returning a response.

@app.report_exception
async def catch_any_exception(app: Sanic, exception: Exception):
    print("Caught exception:", exception)

Also, since it is still an unreleased signal, this PR renames it to "server.exception.report", which is I think is a less confusing name since it is not strictly tied to server lifecycle.

Lastly, in exploring this earlier PR, I realized that there was a bug (double dispatch on some built-in signals that happened inline in a request), and I also realized that we were not capturing exceptions in background tasks. This PR handles these scenarios as well.

@ahopkins ahopkins requested a review from a team as a code owner July 17, 2023 06:25
@codecov
Copy link

codecov bot commented Jul 17, 2023

Codecov Report

Patch coverage: 84.615% and project coverage change: -0.116 ⚠️

Comparison is base (31d7ba8) 88.911% compared to head (8b83306) 88.795%.

Additional details and impacted files
@@              Coverage Diff              @@
##              main     #2792       +/-   ##
=============================================
- Coverage   88.911%   88.795%   -0.116%     
=============================================
  Files           92        92               
  Lines         7034      7051       +17     
  Branches      1195      1197        +2     
=============================================
+ Hits          6254      6261        +7     
- Misses         534       540        +6     
- Partials       246       250        +4     
Impacted Files Coverage Δ
sanic/signals.py 91.275% <66.666%> (+0.058%) ⬆️
sanic/app.py 88.870% <86.956%> (-0.865%) ⬇️

... and 1 file with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

sanic/app.py Show resolved Hide resolved
sanic/signals.py Show resolved Hide resolved
sanic/app.py Show resolved Hide resolved
@ahopkins ahopkins merged commit 9cbe1fb into main Jul 17, 2023
27 checks passed
@ahopkins ahopkins deleted the report-exception branch July 17, 2023 21:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants