Skip to content

Commit

Permalink
feat(engine): Suppress stack traces using catch-all handler (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
daryllimyt authored and topher-lo committed May 4, 2024
1 parent 4a26709 commit 6fcae30
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
13 changes: 11 additions & 2 deletions tracecat/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import polars as pl
from aio_pika import Channel
from aio_pika.pool import Pool
from fastapi import Depends, FastAPI, HTTPException, status
from fastapi import Depends, FastAPI, HTTPException, Request, status
from fastapi.middleware.cors import CORSMiddleware
from fastapi.params import Body
from fastapi.responses import StreamingResponse
from fastapi.responses import ORJSONResponse, StreamingResponse
from sqlalchemy import Engine, or_
from sqlalchemy.exc import NoResultFound
from sqlmodel import Session, select
Expand Down Expand Up @@ -132,6 +132,15 @@ async def lifespan(app: FastAPI):
)


# Catch-all exception handler to prevent stack traces from leaking
@app.exception_handler(Exception)
async def custom_exception_handler(request: Request, exc: Exception):
return ORJSONResponse(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
content={"message": "An unexpected error occurred. Please try again later."},
)


@app.get("/")
def root() -> dict[str, str]:
return {"message": "Hello world. I am the API."}
Expand Down
9 changes: 9 additions & 0 deletions tracecat/runner/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,15 @@ async def valid_workflow(workflow_id: str) -> str:
return workflow_id


# Catch-all exception handler to prevent stack traces from leaking
@app.exception_handler(Exception)
async def custom_exception_handler(request: Request, exc: Exception):
return ORJSONResponse(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
content={"message": "An unexpected error occurred. Please try again later."},
)


# Endpoints
@app.get("/")
def root() -> dict[str, str]:
Expand Down

0 comments on commit 6fcae30

Please sign in to comment.