-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
70 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
"""Integration with fastapi. | ||
See: https://fastapi.tiangolo.com/ | ||
""" | ||
|
||
import rollbar | ||
|
||
from fastapi import Request | ||
|
||
|
||
def report_exception(request: Request): | ||
rollbar.report_exc_info(request=request) | ||
|
||
|
||
def _hook(request, data): | ||
data["framework"] = "fastapi" | ||
|
||
if request: | ||
endpoint = request.scope["endpoint"] | ||
data["context"] = f"{endpoint.__module__}.{endpoint.__name__}" | ||
|
||
|
||
rollbar.BASE_DATA_HOOK = _hook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import rollbar | ||
import rollbar.contrib.fastapi | ||
import uvicorn | ||
|
||
from fastapi import FastAPI, Request, status | ||
from fastapi.responses import JSONResponse, Response | ||
|
||
app = FastAPI() | ||
|
||
|
||
rollbar.init("ACCESS_TOKEN", environment="development") | ||
|
||
|
||
@app.exception_handler(Exception) | ||
async def handle_unexpected_exceptions(request: Request, exc: Exception): | ||
"""This won't capture HTTPException.""" | ||
try: | ||
raise exc | ||
except Exception: | ||
rollbar.contrib.fastapi.report_exception(request=request) | ||
|
||
return Response(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR) | ||
|
||
|
||
@app.get("/") | ||
async def root(raise_exception: bool = False): | ||
"""Hello world api endpoint. | ||
Use `?raise_exception=1` to raise an exception. | ||
""" | ||
if raise_exception: | ||
raise Exception("Testing exceptions") | ||
return JSONResponse({"message": "Hello World"}) | ||
|
||
|
||
if __name__ == "__main__": | ||
uvicorn.run(app) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters