Skip to content

Commit 44094b2

Browse files
Fix airflowctl tracebacks on redirects and non-JSON error bodies (#73307)
airflowctl funnels every command through a single handler whose job is to turn a failure into one line the operator can act on. That handler only recognised ServerResponseError, which the API client builds solely for 4xx/5xx responses carrying a JSON body -- everything else leaves the response hook raising the bare httpx.HTTPStatusError that ServerResponseError subclasses, so no clause caught it. The responses that fall outside that narrow definition are precisely the ones an operator has least context for: an SSO proxy answering with a redirect, or an ingress returning its own HTML error page. Those never reached the Airflow API server at all, and a traceback tells the operator nothing about why.
1 parent 86ff1dc commit 44094b2

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

‎airflow-ctl/src/airflowctl/ctl/cli_config.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,17 @@ def safe_call_command(function: Callable, args: Iterable[Arg]) -> None:
106106
"If you need help, run the command with --help."
107107
)
108108
sys.exit(1)
109+
# Must stay below ``ServerResponseError``, which subclasses it. Responses the client could not
110+
# turn into a ``ServerResponseError`` -- a 3xx, or a 4xx/5xx whose body is not JSON -- reach us
111+
# as the bare httpx error.
112+
except httpx.HTTPStatusError as e:
113+
rich.print(f"[red]Server response error: {e}[/red]")
114+
if e.response.is_redirect:
115+
rich.print(
116+
"[red]The server answered with a redirect, which airflowctl does not follow. "
117+
"Please check that the API URL you logged in with points at the Airflow API server.[/red]"
118+
)
119+
sys.exit(1)
109120

110121

111122
class DefaultHelpParser(argparse.ArgumentParser):

‎airflow-ctl/tests/airflow_ctl/ctl/test_cli_config.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,43 @@ def raise_error(_args):
536536

537537
assert ctx.value.code == 1
538538

539+
@pytest.mark.parametrize(
540+
("response", "hint_expected"),
541+
[
542+
pytest.param(
543+
httpx.Response(302, headers={"location": "https://sso.example.com/login"}),
544+
True,
545+
id="redirect",
546+
),
547+
pytest.param(
548+
httpx.Response(502, headers={"content-type": "text/html"}, content=b"<html>nope</html>"),
549+
False,
550+
id="non-json-server-error",
551+
),
552+
pytest.param(
553+
httpx.Response(401, headers={"content-type": "text/html"}, content=b"<html>nope</html>"),
554+
False,
555+
id="non-json-client-error",
556+
),
557+
],
558+
)
559+
def test_safe_call_command_exits_non_zero_for_bare_http_status_error(
560+
self, response, hint_expected, capsys
561+
):
562+
response.request = httpx.Request("GET", "http://localhost:8080/api/v2/dags")
563+
564+
def raise_error(_args):
565+
response.raise_for_status()
566+
567+
with pytest.raises(SystemExit) as ctx:
568+
safe_call_command(raise_error, args=argparse.Namespace())
569+
570+
assert ctx.value.code == 1
571+
# Rich hard-wraps at the console width, so normalise before matching on a phrase.
572+
out = " ".join(capsys.readouterr().out.split())
573+
assert "Server response error:" in out
574+
assert ("does not follow" in out) is hint_expected
575+
539576
def test_add_to_parser_drops_type_for_boolean_optional_action(self):
540577
"""Test add_to_parser removes type for BooleanOptionalAction."""
541578
parser = argparse.ArgumentParser()

0 commit comments

Comments
 (0)