Skip to content

Commit

Permalink
Add airflow error-guide command
Browse files Browse the repository at this point in the history
  • Loading branch information
omkar-foss committed Feb 20, 2025
1 parent 43e3f6a commit afcae09
Show file tree
Hide file tree
Showing 4 changed files with 826 additions and 0 deletions.
15 changes: 15 additions & 0 deletions airflow/cli/cli_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,15 @@ def string_lower_type(val):
ARG_CONN_PORT,
]

# error-guide
ARG_ERROR_CODE = Arg(
("--error-code",), default="", type=str, help="Specify the Airflow Error Code for the guide."
)
ARG_LIST_EXCEPTIONS = Arg(
("--list-exceptions",), action="store_true", help="Lists all available exception types in the guide."
)
ARG_LIST_GUIDE = Arg(("--list-guide",), action="store_true", help="Lists entire guide as yaml.")


class ActionCommand(NamedTuple):
"""Single CLI command."""
Expand Down Expand Up @@ -2016,6 +2025,12 @@ class GroupCommand(NamedTuple):
func=lazy_load_command("airflow.cli.commands.local_commands.standalone_command.standalone"),
args=(),
),
ActionCommand(
name="error-guide",
help="Provides guidance with an Airflow error code",
func=lazy_load_command("airflow.cli.commands.local_commands.error_guide_command.show_error_guide"),
args=(ARG_ERROR_CODE, ARG_LIST_EXCEPTIONS, ARG_LIST_GUIDE),
),
]


Expand Down
71 changes: 71 additions & 0 deletions airflow/cli/commands/local_commands/error_guide_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"""Error Guide Command."""

from __future__ import annotations

import logging

from airflow.cli.constants.error_guide import error_guide_dict
from airflow.cli.simple_table import AirflowConsole
from airflow.utils import cli as cli_utils

log = logging.getLogger(__name__)

template = """
The error you're facing would be with this message: "{error_message}".
As per our observations, a possible cause could be as follows:
{description}
To resolve this, as first step, you can try the following:
{first_steps}
If this doesn't resolve your problem, you can check out the docs for more info:
{documentation}
You may also ask your questions on the Airflow Slack #user-troubleshooting channel:
https://apache-airflow.slack.com/messages/user-troubleshooting
Happy Debugging! 🐞
"""


@cli_utils.action_cli
def show_error_guide(args):
"""Show the Error Guide."""
console = AirflowConsole()
if args.list_exception_types:
exception_types: list[str] = list(
sorted({payload["exception_type"] for _, payload in error_guide_dict.items()})
)
console.print_as(data=[{"Exception Type": item} for item in exception_types], output="table")
elif args.error_code:
error_code = str(args.error_code).upper()
payload = error_guide_dict.get(error_code)
if not payload:
console.print(
f"Invalid error code specified: '{error_code}'\n\n"
"Please verify the error code, and if you're sure that it's correct, "
"kindly report this as a bug through GitHub (https://github.com/apache/airflow/issues)"
)
else:
console.print(template.format(**payload))
elif args.list_guide_as_yaml:
console.print_as_yaml(error_guide_dict)
else:
raise SystemExit("No arguments passed.")
17 changes: 17 additions & 0 deletions airflow/cli/constants/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
Loading

0 comments on commit afcae09

Please sign in to comment.