Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Exceptions to Bubble Up #148

Closed
jdrew82 opened this issue Mar 19, 2024 · 3 comments
Closed

Allow Exceptions to Bubble Up #148

jdrew82 opened this issue Mar 19, 2024 · 3 comments

Comments

@jdrew82
Copy link
Contributor

jdrew82 commented Mar 19, 2024

When working with the generate_config function the current behavior has any Exceptions from Jinja2 being masked by raising a NornirNautobotException with log message. This means that any logging that is done in a Job is unable to see the full traceback. If we instead re-raised the same Exception we'd have access to the traceback and could log it. The traceback is essential with config generation as it allows quick identification of where the error lies. For example:

The full traceback is this:

[ERROR/ForkPoolWorker-6] Host 'jcy-spine-01.infra.ntc.com': task 'template_file' failed with traceback:
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/nornir/core/task.py", line 99, in start
    r = self.task(self, **self.params)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/nornir_jinja2/plugins/tasks/template_file.py", line 43, in template_file
    text = t.render(host=task.host, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "/opt/nautobot/git/templates/cisco_nxos.j2", line 103, in top-level template code
    {% include './nxos/vlans.j2' %}
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/nautobot/git/templates/nxos/vlans.j2", line 1, in top-level template code
    {% for vlan in site["vlans"] %}
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/jinja2/sandbox.py", line 303, in getitem
    return obj[argument]
           ~~~^^^^^^^^^^
jinja2.exceptions.UndefinedError: 'site' is undefined

but the Job logs only show E1010: There was a jinja2.exceptions.UndefinedError error: 'site' is undefined. You have to look at the STDERR logs to find the traceback. It'd be much more useful if we could log the traceback as that's where the important information is.

@itdependsnetworks
Copy link
Contributor

I am not sure what to do, this contract with upstream is expecting there to be a NornirNautobotException raised, so there are implications. In my humble opinion, this is where this should be handled nautobot/nautobot#2331, otherwise it is a game of whack-a-mole in which we will never win.

@jdrew82
Copy link
Contributor Author

jdrew82 commented Mar 20, 2024

From our discussion I attempted to just log the traceback and unfortunately the closest you can get is the Traceback object itself but that doesn't appear to contain the full traceback, just the start of it and we need the end. We can ignore this request and consider it a not gonna work.

@jdrew82 jdrew82 closed this as completed Mar 20, 2024
@jdrew82
Copy link
Contributor Author

jdrew82 commented Mar 21, 2024

Looks like @jmpettit figured out how to get the traceback logged in #149!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants