Skip to content

Commit 0412f67

Browse files
Add more information to the fallback server
* Display the traceback, but this isn't always too helpful * Optionally pass log history for display
1 parent 721b352 commit 0412f67

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/labthings_fastapi/server/fallback.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
from traceback import format_exception
23
from fastapi import FastAPI
34
from fastapi.responses import HTMLResponse
45
from starlette.responses import RedirectResponse
@@ -10,6 +11,7 @@ def __init__(self, *args, **kwargs):
1011
self.labthings_config = None
1112
self.labthings_server = None
1213
self.labthings_error = None
14+
self.log_history = None
1315

1416

1517
app = FallbackApp()
@@ -32,6 +34,9 @@ def __init__(self, *args, **kwargs):
3234
</ul>
3335
<p>Your configuration:</p>
3436
<pre>{{config}}</pre>
37+
<p>Traceback</p>
38+
<pre>{{traceback}}</pre>
39+
{{logginginfo}}
3540
</body>
3641
</html>
3742
"""
@@ -40,6 +45,9 @@ def __init__(self, *args, **kwargs):
4045
@app.get("/")
4146
async def root():
4247
error_message = f"{app.labthings_error!r}"
48+
# use traceback.format_exception to get full traceback as list
49+
# this ends in newlines, but needs joining to be a single string
50+
error_w_trace = "".join(format_exception(app.labthings_error))
4351
things = ""
4452
if app.labthings_server:
4553
for path, thing in app.labthings_server.things.items():
@@ -49,6 +57,14 @@ async def root():
4957
content = content.replace("{{error}}", error_message)
5058
content = content.replace("{{things}}", things)
5159
content = content.replace("{{config}}", json.dumps(app.labthings_config, indent=2))
60+
content = content.replace("{{traceback}}", error_w_trace)
61+
62+
if app.log_history is None:
63+
logging_info = " <p>No logging info available</p>"
64+
else:
65+
logging_info = f" <p>Logging info</p>\n <pre>{app.log_history}</pre>"
66+
67+
content = content.replace("{{logginginfo}}", logging_info)
5268
return HTMLResponse(content=content, status_code=500)
5369

5470

0 commit comments

Comments
 (0)