Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add traceback to error messages
Browse files Browse the repository at this point in the history
Justin Pettit committed Mar 20, 2024
1 parent bab83ce commit 3cc6945
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions nornir_nautobot/plugins/tasks/dispatcher/default.py
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
import logging
import os
import socket
import traceback
from typing import Optional

import jinja2
@@ -174,29 +175,43 @@ def generate_config(
jinja_env=jinja_env,
)[0].result
except NornirSubTaskError as exc:
stack_trace = traceback.format_exc()
if isinstance(exc.result.exception, jinja2.exceptions.UndefinedError): # pylint: disable=no-else-raise
error_msg = (
f"`E1010:` There was a jinja2.exceptions.UndefinedError error: ``{str(exc.result.exception)}``"
f"\nStack trace: {stack_trace}"
)
logger.error(error_msg, extra={"object": obj})
raise NornirNautobotException(error_msg)

elif isinstance(exc.result.exception, jinja2.TemplateSyntaxError):
error_msg = (f"`E1011:` There was a jinja2.TemplateSyntaxError error: ``{str(exc.result.exception)}``",)
error_msg = (
f"`E1011:` There was a jinja2.TemplateSyntaxError error: ``{str(exc.result.exception)}``"
f"\nStack trace: {stack_trace}"
)
logger.error(error_msg, extra={"object": obj})
raise NornirNautobotException(error_msg)

elif isinstance(exc.result.exception, jinja2.TemplateNotFound):
error_msg = f"`E1012:` There was an issue finding the template and a jinja2.TemplateNotFound error was raised: ``{str(exc.result.exception)}``"
error_msg = (
f"`E1012:` There was an issue finding the template and a jinja2.TemplateNotFound error was raised: ``{str(exc.result.exception)}``"
f"\nStack trace: {stack_trace}"
)
logger.error(error_msg, extra={"object": obj})
raise NornirNautobotException(error_msg)

elif isinstance(exc.result.exception, jinja2.TemplateError):
error_msg = f"`E1013:` There was an issue general Jinja error: ``{str(exc.result.exception)}``"
error_msg = (
f"`E1013:` There was an issue general Jinja error: ``{str(exc.result.exception)}``"
f"\nStack trace: {stack_trace}"
)
logger.error(error_msg, extra={"object": obj})
raise NornirNautobotException(error_msg)

error_msg = f"`E1014:` Failed with an unknown issue. `{exc.result.exception}`"
error_msg = (
f"`E1014:` Failed with an unknown issue. `{exc.result.exception}`"
f"\nStack trace: {stack_trace}"
)
logger.error(error_msg, extra={"object": obj})
raise NornirNautobotException(error_msg)

0 comments on commit 3cc6945

Please sign in to comment.