This is an automated email from the ASF dual-hosted git repository.
bugraoz pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/main by this push:
new cf831e5eb2b feat (airflowctl): Shortcircuit command call on error
(#49438)
cf831e5eb2b is described below
commit cf831e5eb2b019e2bc14ecf601a205bfce50e1da
Author: Aritra Basu <[email protected]>
AuthorDate: Wed Apr 23 22:08:04 2025 +0530
feat (airflowctl): Shortcircuit command call on error (#49438)
* Moved error check up to ctl level
* More granular exceptions
---
airflow-ctl/src/airflowctl/__main__.py | 3 ++-
airflow-ctl/src/airflowctl/api/client.py | 4 ++--
airflow-ctl/src/airflowctl/ctl/cli_config.py | 10 ++++++++++
airflow-ctl/src/airflowctl/exceptions.py | 4 ++++
4 files changed, 18 insertions(+), 3 deletions(-)
diff --git a/airflow-ctl/src/airflowctl/__main__.py
b/airflow-ctl/src/airflowctl/__main__.py
index 86aef8ab798..c8af217f250 100644
--- a/airflow-ctl/src/airflowctl/__main__.py
+++ b/airflow-ctl/src/airflowctl/__main__.py
@@ -24,13 +24,14 @@ from __future__ import annotations
import argcomplete
from airflowctl.ctl import cli_parser
+from airflowctl.ctl.cli_config import safe_call_command
def main():
parser = cli_parser.get_parser()
argcomplete.autocomplete(parser)
args = parser.parse_args()
- args.func(args)
+ safe_call_command(args.func, args=args)
if __name__ == "__main__":
diff --git a/airflow-ctl/src/airflowctl/api/client.py
b/airflow-ctl/src/airflowctl/api/client.py
index 7beaf942e71..de180f82a2d 100644
--- a/airflow-ctl/src/airflowctl/api/client.py
+++ b/airflow-ctl/src/airflowctl/api/client.py
@@ -48,7 +48,7 @@ from airflowctl.api.operations import (
VariablesOperations,
VersionOperations,
)
-from airflowctl.exceptions import AirflowCtlNotFoundException
+from airflowctl.exceptions import AirflowCtlCredentialNotFoundException,
AirflowCtlNotFoundException
from airflowctl.typing_compat import ParamSpec
if TYPE_CHECKING:
@@ -143,7 +143,7 @@ class Credentials:
self.api_url = credentials["api_url"]
self.api_token = keyring.get_password("airflowctl",
f"api_token-{self.api_environment}")
return self
- raise AirflowCtlNotFoundException(f"No credentials found in
{default_config_dir}")
+ raise AirflowCtlCredentialNotFoundException(f"No credentials found in
{default_config_dir}")
class BearerAuth(httpx.Auth):
diff --git a/airflow-ctl/src/airflowctl/ctl/cli_config.py
b/airflow-ctl/src/airflowctl/ctl/cli_config.py
index b55c93c9922..79cc4c824ee 100644
--- a/airflow-ctl/src/airflowctl/ctl/cli_config.py
+++ b/airflow-ctl/src/airflowctl/ctl/cli_config.py
@@ -36,6 +36,7 @@ import rich
import airflowctl.api.datamodels.generated as generated_datamodels
from airflowctl.api.client import NEW_API_CLIENT, Client, ClientKind,
provide_api_client
from airflowctl.api.operations import BaseOperations, ServerResponseError
+from airflowctl.exceptions import AirflowCtlCredentialNotFoundException,
AirflowCtlNotFoundException
from airflowctl.utils.module_loading import import_string
BUILD_DOCS = "BUILDING_AIRFLOW_DOCS" in os.environ
@@ -54,6 +55,15 @@ def lazy_load_command(import_path: str) -> Callable:
return command
+def safe_call_command(function: Callable, args: Iterable[Arg]) -> None:
+ try:
+ function(args)
+ except AirflowCtlCredentialNotFoundException as e:
+ rich.print(f"command failed due to {e}")
+ except AirflowCtlNotFoundException as e:
+ rich.print(f"command failed due to {e}")
+
+
class DefaultHelpParser(argparse.ArgumentParser):
"""CustomParser to display help message."""
diff --git a/airflow-ctl/src/airflowctl/exceptions.py
b/airflow-ctl/src/airflowctl/exceptions.py
index 04bb15d230b..b313e969b2c 100644
--- a/airflow-ctl/src/airflowctl/exceptions.py
+++ b/airflow-ctl/src/airflowctl/exceptions.py
@@ -32,3 +32,7 @@ class AirflowCtlException(Exception):
class AirflowCtlNotFoundException(AirflowCtlException):
"""Raise when the requested object/resource is not available in the
system."""
+
+
+class AirflowCtlCredentialNotFoundException(AirflowCtlNotFoundException):
+ """Raise when a credential couldn't be found while performing an
operation."""