This is an automated email from the ASF dual-hosted git repository.
shahar1 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 ab39e956969 Escape error text in airflowctl safe_call_command messages
(#76264)
ab39e956969 is described below
commit ab39e9569698a9eaf9e21ed31c86ddf1c1a98f93
Author: Rishabh <[email protected]>
AuthorDate: Tue Nov 3 22:43:42 2026 +0530
Escape error text in airflowctl safe_call_command messages (#76264)
safe_call_command interpolated exception and server error text straight
into Rich markup. A message containing a tag-like fragment such as
"[/x]" made rich.print raise MarkupError, so the user saw a traceback
instead of the real failure and the exit code was no longer 1.
Escape the text in the AirflowCtl, protocol, timeout, server response
and HTTP status handlers.
---
airflow-ctl/src/airflowctl/ctl/cli_config.py | 12 ++++-----
.../tests/airflow_ctl/ctl/test_cli_config.py | 31 ++++++++++++++++++++++
2 files changed, 37 insertions(+), 6 deletions(-)
diff --git a/airflow-ctl/src/airflowctl/ctl/cli_config.py
b/airflow-ctl/src/airflowctl/ctl/cli_config.py
index a3f70533bc5..b19b5d20b5e 100755
--- a/airflow-ctl/src/airflowctl/ctl/cli_config.py
+++ b/airflow-ctl/src/airflowctl/ctl/cli_config.py
@@ -95,20 +95,20 @@ def safe_call_command(function: Callable, args:
Iterable[Arg]) -> None:
function(args)
except AirflowCtlException as e:
# The base class, so a new AirflowCtl error never regresses to a raw
traceback.
- rich.print(f"command failed due to {e}", file=sys.stderr)
+ rich.print(f"command failed due to {escape(str(e))}", file=sys.stderr)
sys.exit(1)
except (httpx.RemoteProtocolError, httpx.ReadError, httpx.WriteError) as e:
- rich.print(f"[red]Remote protocol error: {e}[/red]", file=sys.stderr)
+ rich.print(f"[red]Remote protocol error: {escape(str(e))}[/red]",
file=sys.stderr)
if "Server disconnected without sending a response." in str(e):
rich.print(
- f"[red]Server response error: {e}. "
+ f"[red]Server response error: {escape(str(e))}. "
"Please check if the server is running and the API URL is
correct.[/red]",
file=sys.stderr,
)
_warn_if_write_may_have_applied(e)
sys.exit(1)
except httpx.TimeoutException as e:
- rich.print(f"[red]Request timeout error: {e}[/red]", file=sys.stderr)
+ rich.print(f"[red]Request timeout error: {escape(str(e))}[/red]",
file=sys.stderr)
if "timed out" in str(e):
rich.print(
"[red]Please check if the server is running and the API ready
to accept calls.[/red]",
@@ -117,7 +117,7 @@ def safe_call_command(function: Callable, args:
Iterable[Arg]) -> None:
_warn_if_write_may_have_applied(e)
sys.exit(1)
except ServerResponseError as e:
- rich.print(f"Server response error: {e}", file=sys.stderr)
+ rich.print(f"Server response error: {escape(str(e))}", file=sys.stderr)
if "Client error message:" in str(e):
rich.print(
"[red]Client error, [/red] "
@@ -131,7 +131,7 @@ def safe_call_command(function: Callable, args:
Iterable[Arg]) -> None:
# turn into a ``ServerResponseError`` -- a 3xx, or a 4xx/5xx whose body is
not JSON -- reach us
# as the bare httpx error.
except httpx.HTTPStatusError as e:
- rich.print(f"[red]Server response error: {e}[/red]", file=sys.stderr)
+ rich.print(f"[red]Server response error: {escape(str(e))}[/red]",
file=sys.stderr)
if e.response.is_redirect:
rich.print(
"[red]The server answered with a redirect, which airflowctl
does not follow. "
diff --git a/airflow-ctl/tests/airflow_ctl/ctl/test_cli_config.py
b/airflow-ctl/tests/airflow_ctl/ctl/test_cli_config.py
index 1005ac29ad4..47f979543ef 100644
--- a/airflow-ctl/tests/airflow_ctl/ctl/test_cli_config.py
+++ b/airflow-ctl/tests/airflow_ctl/ctl/test_cli_config.py
@@ -841,6 +841,37 @@ class TestCliConfigMethods:
for text in expected:
assert text in err
+ @pytest.mark.parametrize(
+ "raised_exception",
+ [
+ AirflowCtlNotFoundException("not found [/bogus]"),
+ httpx.RemoteProtocolError("protocol error [/bogus]"),
+ httpx.ReadTimeout("timed out [/bogus]"),
+ httpx.HTTPStatusError(
+ "status error [/bogus]",
+ request=httpx.Request("GET",
"http://localhost:8080/api/v2/dags"),
+ response=httpx.Response(302),
+ ),
+ ServerResponseError(
+ "server error [/bogus]",
+ request=httpx.Request("GET",
"http://localhost:8080/api/v2/dags"),
+ response=httpx.Response(500, json={"detail": "boom"}),
+ ),
+ ],
+ ids=["airflowctl", "protocol", "timeout", "http-status",
"server-response"],
+ )
+ def test_safe_call_command_prints_error_text_containing_markup(self,
raised_exception, capsys):
+ def raise_error(_args):
+ raise raised_exception
+
+ with pytest.raises(SystemExit) as ctx:
+ safe_call_command(raise_error, args=argparse.Namespace())
+
+ assert ctx.value.code == 1
+ captured = capsys.readouterr()
+ assert captured.out == ""
+ assert "[/bogus]" in captured.err
+
@pytest.mark.parametrize(
("response", "hint_expected"),
[