diff --git a/src/openflexure_microscope_server/server/__init__.py b/src/openflexure_microscope_server/server/__init__.py index dfffc067..6d2a70ac 100644 --- a/src/openflexure_microscope_server/server/__init__.py +++ b/src/openflexure_microscope_server/server/__init__.py @@ -18,7 +18,12 @@ from labthings_fastapi.server.config_model import ThingServerConfig from openflexure_microscope_server.things.camera import BaseCamera from openflexure_microscope_server.utilities import load_patched_config -from ..logging import configure_logging, retrieve_log, retrieve_log_from_file +from ..logging import ( + OFM_HANDLER, + configure_logging, + retrieve_log, + retrieve_log_from_file, +) from .legacy_api import add_v2_endpoints from .serve_static_files import add_static_files @@ -128,10 +133,18 @@ def serve_from_cli(argv: Optional[list[str]] = None) -> None: # presented in the fallback logs. print(f"Error: {e}") # noqa: T201 print("Starting fallback server.") # noqa: T201 + try: + log_history = OFM_HANDLER.log_history + except BaseException: + # If log history fails for any reason carry on. + log_history = None + app = fallback.app - app.labthings_config = lt_config - app.labthings_server = server - app.labthings_error = e + app.set_context( + fallback.FallbackContext( + server=server, config=lt_config, error=e, log_history=log_history + ) + ) uvicorn.run( app, host=args.host, diff --git a/tests/unit_tests/test_server_cli.py b/tests/unit_tests/test_server_cli.py index 079cdf15..7901fc69 100644 --- a/tests/unit_tests/test_server_cli.py +++ b/tests/unit_tests/test_server_cli.py @@ -102,4 +102,4 @@ def test_failed_customise(mocker): # Check it really is a fastapi assert isinstance(fallback_app, FastAPI) # An that it has the error to display - assert str(fallback_app.labthings_error) == "Can't touch this" + assert str(fallback_app._context.error) == "Can't touch this"