Improve error message display; replicate changes for VMs

This commit is contained in:
Jeremy Stretch 2024-12-11 09:59:54 -05:00
parent 4cb6abfc18
commit c2d429f4d8
4 changed files with 52 additions and 33 deletions

View File

@ -2104,7 +2104,8 @@ class DeviceRenderConfigView(generic.ObjectView):
# If a direct export has been requested, return the rendered template content as a
# downloadable file.
if request.GET.get('export'):
response = HttpResponse(context['rendered_config'], content_type='text')
content = context['rendered_config'] or context['error_message']
response = HttpResponse(content, content_type='text')
filename = f"{instance.name or 'config'}.txt"
response['Content-Disposition'] = f'attachment; filename="{filename}"'
return response
@ -2122,18 +2123,18 @@ class DeviceRenderConfigView(generic.ObjectView):
# Render the config template
rendered_config = None
error_message = None
if config_template := instance.get_config_template():
try:
rendered_config = config_template.render(context=context_data)
except TemplateError as e:
msg = _("An error occurred while rendering the template: {error}").format(error=e)
messages.error(request, msg)
rendered_config = msg
error_message = _("An error occurred while rendering the template: {error}").format(error=e)
return {
'config_template': config_template,
'context_data': context_data,
'rendered_config': rendered_config,
'error_message': error_message,
}

View File

@ -5,7 +5,7 @@
{% block title %}{{ object }} - {% trans "Config" %}{% endblock %}
{% block content %}
<div class="row mb-3">
<div class="row">
<div class="col-5">
<div class="card">
<h2 class="card-header">{% trans "Config Template" %}</h2>
@ -48,19 +48,28 @@
</div>
<div class="row">
<div class="col">
<div class="card">
<h2 class="card-header d-flex justify-content-between">
{% trans "Rendered Config" %}
<a href="?export=True" class="btn btn-primary lh-1" role="button">
<i class="mdi mdi-download" aria-hidden="true"></i> {% trans "Download" %}
</a>
</h2>
{% if config_template %}
<pre class="card-body">{{ rendered_config }}</pre>
{% if config_template %}
{% if rendered_config %}
<div class="card">
<h2 class="card-header d-flex justify-content-between">
{% trans "Rendered Config" %}
<a href="?export=True" class="btn btn-primary lh-1" role="button">
<i class="mdi mdi-download" aria-hidden="true"></i> {% trans "Download" %}
</a>
</h2>
<pre class="card-body">{{ rendered_config }}</pre>
</div>
{% else %}
<div class="card-body text-muted">{% trans "No configuration template found" %}</div>
<div class="alert alert-warning">
<h4 class="alert-title mb-1">{% trans "Error rendering template" %}</h4>
{% trans error_message %}
</div>
{% endif %}
</div>
{% else %}
<div class="alert alert-info">
{% trans "No configuration template has been assigned for this device." %}
</div>
{% endif %}
</div>
</div>
{% endblock %}

View File

@ -5,7 +5,7 @@
{% block title %}{{ object }} - {% trans "Config" %}{% endblock %}
{% block content %}
<div class="row mb-3">
<div class="row">
<div class="col-5">
<div class="card">
<h2 class="card-header">{% trans "Config Template" %}</h2>
@ -48,19 +48,28 @@
</div>
<div class="row">
<div class="col">
<div class="card">
<h2 class="card-header d-flex justify-content-between">
{% trans "Rendered Config" %}
<a href="?export=True" class="btn btn-primary lh-1" role="button">
<i class="mdi mdi-download" aria-hidden="true"></i> {% trans "Download" %}
</a>
</h2>
{% if config_template %}
<pre class="card-body">{{ rendered_config }}</pre>
{% if config_template %}
{% if rendered_config %}
<div class="card">
<h2 class="card-header d-flex justify-content-between">
{% trans "Rendered Config" %}
<a href="?export=True" class="btn btn-primary lh-1" role="button">
<i class="mdi mdi-download" aria-hidden="true"></i> {% trans "Download" %}
</a>
</h2>
<pre class="card-body">{{ rendered_config }}</pre>
</div>
{% else %}
<div class="card-body text-muted">{% trans "No configuration template found" %}</div>
<div class="alert alert-warning">
<h4 class="alert-title mb-1">{% trans "Error rendering template" %}</h4>
{% trans error_message %}
</div>
{% endif %}
</div>
{% else %}
<div class="alert alert-info">
{% trans "No configuration template has been assigned for this virtual machine." %}
</div>
{% endif %}
</div>
</div>
{% endblock %}

View File

@ -1,5 +1,3 @@
import traceback
from django.contrib import messages
from django.db import transaction
from django.db.models import Prefetch, Sum
@ -425,7 +423,8 @@ class VirtualMachineRenderConfigView(generic.ObjectView):
# If a direct export has been requested, return the rendered template content as a
# downloadable file.
if request.GET.get('export'):
response = HttpResponse(context['rendered_config'], content_type='text')
content = context['rendered_config'] or context['error_message']
response = HttpResponse(content, content_type='text')
filename = f"{instance.name or 'config'}.txt"
response['Content-Disposition'] = f'attachment; filename="{filename}"'
return response
@ -443,17 +442,18 @@ class VirtualMachineRenderConfigView(generic.ObjectView):
# Render the config template
rendered_config = None
error_message = None
if config_template := instance.get_config_template():
try:
rendered_config = config_template.render(context=context_data)
except TemplateError as e:
messages.error(request, _("An error occurred while rendering the template: {error}").format(error=e))
rendered_config = traceback.format_exc()
error_message = _("An error occurred while rendering the template: {error}").format(error=e)
return {
'config_template': config_template,
'context_data': context_data,
'rendered_config': rendered_config,
'error_message': error_message,
}