diff --git a/netbox/extras/api/mixins.py b/netbox/extras/api/mixins.py index 91d1add0b..795d50af8 100644 --- a/netbox/extras/api/mixins.py +++ b/netbox/extras/api/mixins.py @@ -45,10 +45,10 @@ class ConfigTemplateRenderMixin: try: output = configtemplate.render(context=context) except Exception as e: - return Response( - {'detail': configtemplate.format_render_error(e)}, - status=HTTP_500_INTERNAL_SERVER_ERROR, - ) + detail = configtemplate.format_render_error(e) + if request.accepted_renderer.format == 'txt': + return Response(detail, status=HTTP_500_INTERNAL_SERVER_ERROR) + return Response({'detail': detail}, status=HTTP_500_INTERNAL_SERVER_ERROR) # If the client has requested "text/plain", return the raw content. if request.accepted_renderer.format == 'txt': diff --git a/netbox/extras/models/configs.py b/netbox/extras/models/configs.py index 9f028fc61..477fa2e03 100644 --- a/netbox/extras/models/configs.py +++ b/netbox/extras/models/configs.py @@ -313,12 +313,11 @@ class ConfigTemplate( def format_render_error(self, exc): """ Return a formatted error string for a rendering exception. When debug is enabled, the full - traceback is returned. Otherwise, a concise, user-facing message is returned. - - Must be called from within the except block so that traceback.format_exc() is valid. + traceback for the provided exception is returned. Otherwise, a concise, user-facing message + is returned. """ if self.debug: - return traceback.format_exc() + return ''.join(traceback.format_exception(exc)) if isinstance(exc, TemplateError): parts = [f"{type(exc).__name__}: {exc}"] if getattr(exc, 'name', None):