Skip to content

Commit 33d9281

Browse files
Improve fallback docstrings
1 parent ff3d4ef commit 33d9281

1 file changed

Lines changed: 28 additions & 1 deletion

File tree

src/labthings_fastapi/server/fallback.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,18 @@ class FallbackContext:
3232
"""A dataclass to provide the context of the server failing to load."""
3333

3434
error: BaseException | None
35+
"""The error caught when running uvicorn.run."""
36+
3537
server: ThingServer | None
38+
"""The ThingServer that failed to start."""
39+
3640
config: ThingServerConfig | dict[str, Any] | None
41+
"""The config used to set up the server.
42+
43+
This can be the ThingServerConfig, or the dict read from the JSON file."""
44+
3745
log_history: str | None
46+
"""Any logging history to show."""
3847

3948

4049
class FallbackApp(FastAPI):
@@ -63,11 +72,27 @@ def set_context(self, context: FallbackContext) -> None:
6372
"""Set the fallback runtime context.
6473
6574
This should be called exactly once during failure handling.
75+
76+
:param context: A FallbackContext object with the server, the captured error,
77+
the configuration, and log history.
6678
"""
6779
self._context = context
6880

6981
def set_template_str(self, template_str: str) -> None:
70-
"""Compile and set a Jinja template from a string."""
82+
"""Compile and set a Jinja template from a string.
83+
84+
:param template_str: A Jinja2 template string. The template should be
85+
self-contained and must not extend or include other templates. If
86+
customised, the template must handle the following template context
87+
variables (each may be ``None``):
88+
89+
- ``error_message`` (``str`` | ``None``): Error message to display, if any.
90+
- ``things`` (``list[str]`` | ``None``): Names of successfully loaded
91+
things.
92+
- ``config`` (``str`` | ``None``): The server configuration.
93+
- ``traceback`` (``str`` | ``None``): Formatted error traceback.
94+
- ``logginginfo`` (``str`` | ``None``): Captured logging output.
95+
"""
7196
self._template = self._env.from_string(template_str)
7297

7398
def fallback_page(self) -> HTMLResponse:
@@ -116,6 +141,8 @@ def _format_error_and_traceback(context: FallbackContext) -> tuple[str, str]:
116141
If the error was in lifespan causing Uvicorn to raise SystemExit(3) without a
117142
traceback. Try to extract the saved exception from the server.
118143
144+
:param context:The FallbackContext object with all fallback information.
145+
119146
:return: A tuple of error message and error traceback.
120147
"""
121148
err = context.error

0 commit comments

Comments
 (0)