Skip to content

Commit 77f0abb

Browse files
nohalonpre-commit-ci[bot]
authored andcommitted
lightning apps: add flow fail() (#18883)
* add flow fail() * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> (cherry picked from commit fd48627)
1 parent e4893b9 commit 77f0abb

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/lightning/app/core/app.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
from lightning.app.utilities.commands.base import _process_requests
5252
from lightning.app.utilities.component import _convert_paths_after_init, _validate_root_flow
5353
from lightning.app.utilities.enum import AppStage, CacheCallsKeys
54-
from lightning.app.utilities.exceptions import CacheMissException, ExitAppException
54+
from lightning.app.utilities.exceptions import CacheMissException, ExitAppException, LightningFlowException
5555
from lightning.app.utilities.layout import _collect_layout
5656
from lightning.app.utilities.proxies import ComponentDelta
5757
from lightning.app.utilities.scheduler import SchedulerThread
@@ -462,6 +462,9 @@ def run_once(self) -> bool:
462462
self.root.run()
463463
except CacheMissException:
464464
self._on_cache_miss_exception()
465+
except LightningFlowException:
466+
done = True
467+
self.stage = AppStage.FAILED
465468
except (ExitAppException, KeyboardInterrupt):
466469
done = True
467470
self.stage = AppStage.STOPPING

src/lightning/app/core/flow.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from lightning.app.storage.path import Path
2828
from lightning.app.utilities.app_helpers import _is_json_serializable, _LightningAppRef, _set_child_name, is_overridden
2929
from lightning.app.utilities.component import _sanitize_state
30-
from lightning.app.utilities.exceptions import ExitAppException
30+
from lightning.app.utilities.exceptions import ExitAppException, LightningFlowException
3131
from lightning.app.utilities.introspection import _is_init_context, _is_run_context
3232
from lightning.app.utilities.packaging.cloud_compute import CloudCompute, _maybe_create_cloud_compute
3333

@@ -391,6 +391,12 @@ def stop(self, end_msg: str = "") -> None:
391391
print(end_msg)
392392
raise ExitAppException
393393

394+
def fail(self, end_msg: str = "") -> None:
395+
"""Method used to exit and fail the application."""
396+
if end_msg:
397+
print(end_msg)
398+
raise LightningFlowException
399+
394400
def _exit(self, end_msg: str = "") -> None:
395401
"""Used to exit the application.
396402

0 commit comments

Comments
 (0)