From 15076b7ca4daa527110c2e4d9910f8520885ac70 Mon Sep 17 00:00:00 2001 From: Justin Pettit <47164813+jmpettit@users.noreply.github.com> Date: Thu, 21 Mar 2024 08:41:45 -0500 Subject: [PATCH] Include full stack trace in error logging --- nornir_nautobot/plugins/tasks/dispatcher/default.py | 6 +++--- nornir_nautobot/utils/helpers.py | 3 +-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/nornir_nautobot/plugins/tasks/dispatcher/default.py b/nornir_nautobot/plugins/tasks/dispatcher/default.py index e49d34e..c02fea9 100644 --- a/nornir_nautobot/plugins/tasks/dispatcher/default.py +++ b/nornir_nautobot/plugins/tasks/dispatcher/default.py @@ -25,7 +25,7 @@ from nornir_netmiko.tasks import netmiko_send_command from nornir_nautobot.exceptions import NornirNautobotException -from nornir_nautobot.utils.helpers import make_folder, format_jinja_stack_trace +from nornir_nautobot.utils.helpers import make_folder, get_stack_trace _logger = logging.getLogger(__name__) @@ -174,7 +174,7 @@ def generate_config( jinja_env=jinja_env, )[0].result except NornirSubTaskError as exc: - stack_trace = format_jinja_stack_trace(exc.result.exception) + stack_trace = get_stack_trace(exc.result.exception) error_mapping = { jinja2.exceptions.UndefinedError: ("E1010", "Undefined variable in Jinja2 template"), @@ -189,7 +189,7 @@ def generate_config( logger.error(error_msg, extra={"object": obj}) raise NornirNautobotException(error_msg) - error_msg = f"`E1014:` Unknown error - `{exc.result.exception}`" + error_msg = f"`E1014:` Unknown error - `{exc.result.exception}`\n```\n{stack_trace}\n```" logger.error(error_msg, extra={"object": obj}) raise NornirNautobotException(error_msg) diff --git a/nornir_nautobot/utils/helpers.py b/nornir_nautobot/utils/helpers.py index 46f4a00..ea2df8e 100644 --- a/nornir_nautobot/utils/helpers.py +++ b/nornir_nautobot/utils/helpers.py @@ -33,8 +33,7 @@ def import_string(dotted_path): except (ModuleNotFoundError, AttributeError): return None -def format_jinja_stack_trace(exc: Exception) -> str: +def get_stack_trace(exc: Exception) -> str: """Generate and format a stack trace string for a given Jinja exception.""" stack_trace_lines = traceback.format_exception(type(exc), exc, exc.__traceback__) - stack_trace_lines = [line for line in stack_trace_lines if '.j2' in line or '{{' in line] return "\n".join(stack_trace_lines)