Преглед изворни кода

Fixes #12167: Catch and report on exceptions raised when rendering a config template

jeremystretch пре 2 година
родитељ
комит
1b5f926e17
2 измењених фајлова са 9 додато и 1 уклоњено
  1. 2 0
      docs/release-notes/version-3.5.md
  2. 7 1
      netbox/extras/api/mixins.py

+ 2 - 0
docs/release-notes/version-3.5.md

@@ -80,6 +80,8 @@ Two new webhook trigger events have been introduced: `job_start` and `job_end`.
 * [#12144](https://github.com/netbox-community/netbox/issues/12144) - Ensure consistent treatment of context data when rendering config templates via UI & API
 * [#12145](https://github.com/netbox-community/netbox/issues/12145) - Employ `HTMXSelect` widget to fix inclusion of `<select>` field values during form regeneration
 * [#12146](https://github.com/netbox-community/netbox/issues/12146) - Do not display object selector for disabled fields
+* [#12151](https://github.com/netbox-community/netbox/issues/12151) - Remove incorrect OpenAPI string mapping for choice fields
+* [#12167](https://github.com/netbox-community/netbox/issues/12167) - Catch and report on exceptions raised when rendering a config template
 
 ### Other Changes
 

+ 7 - 1
netbox/extras/api/mixins.py

@@ -1,3 +1,4 @@
+from jinja2.exceptions import TemplateError
 from rest_framework.response import Response
 
 from .nested_serializers import NestedConfigTemplateSerializer
@@ -32,7 +33,12 @@ class ConfigContextQuerySetMixin:
 class ConfigTemplateRenderMixin:
 
     def render_configtemplate(self, request, configtemplate, context):
-        output = configtemplate.render(context=context)
+        try:
+            output = configtemplate.render(context=context)
+        except TemplateError as e:
+            return Response({
+                'detail': f"An error occurred while rendering the template (line {e.lineno}): {e}"
+            }, status=500)
 
         # If the client has requested "text/plain", return the raw content.
         if request.accepted_renderer.format == 'txt':