Skip to content

Commit

Permalink
Added error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
VukW committed Jul 29, 2024
1 parent 6b28ebb commit 881b281
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cli/medperf/web_ui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from medperf import config
from medperf.decorators import clean_except
from medperf.web_ui.common import custom_exception_handler
from medperf.web_ui.datasets.routes import router as datasets_router
from medperf.web_ui.benchmarks.routes import router as benchmarks_router
from medperf.web_ui.mlcubes.routes import router as mlcubes_router
Expand All @@ -26,6 +27,7 @@
)
)

web_app.add_exception_handler(Exception, custom_exception_handler)

@web_app.get("/", include_in_schema=False)
def read_root():
Expand Down
18 changes: 18 additions & 0 deletions cli/medperf/web_ui/common.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
import logging

from fastapi.templating import Jinja2Templates
from importlib import resources

from fastapi.requests import Request

templates = Jinja2Templates(directory=str(resources.path("medperf.web_ui", "templates")))

logger = logging.getLogger(__name__)


async def custom_exception_handler(request: Request, exc: Exception):
# Log the exception details
logger.error(f"Unhandled exception: {exc}", exc_info=True)

# Prepare the context for the error page
context = {"request": request, "exception": exc}

# Return a detailed error page
return templates.TemplateResponse("error.html", context, status_code=500)
15 changes: 15 additions & 0 deletions cli/medperf/web_ui/templates/error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends "base.html" %}

{% block title %}Error{% endblock %}

{% block content %}
<div class="container">
<h1 class="my-4">An Error Occurred</h1>
<div class="alert alert-danger" role="alert">
<p><strong>Error:</strong> {{ exception }}</p>
<p><strong>Request:</strong> {{ request.url }}</p>
<p><strong>Method:</strong> {{ request.method }}</p>
<p><strong>Client:</strong> {{ request.client }}</p>
</div>
</div>
{% endblock %}

0 comments on commit 881b281

Please sign in to comment.