From 5bb7dbfb192471f6b1c670b41d0ea414393a61a8 Mon Sep 17 00:00:00 2001 From: Dan Date: Mon, 22 Nov 2021 14:46:33 -0500 Subject: [PATCH] Improve the error messages returned from jinja template processing. Try to find the actual line with an error in it. --- crc/api/common.py | 3 +-- crc/services/workflow_service.py | 24 +++++++++++++++++++----- tests/test_verify_end_event.py | 8 ++++++-- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/crc/api/common.py b/crc/api/common.py index 12f7ec10..18ba37aa 100644 --- a/crc/api/common.py +++ b/crc/api/common.py @@ -3,6 +3,7 @@ import json from SpiffWorkflow import WorkflowException from SpiffWorkflow.exceptions import WorkflowTaskExecException from flask import g +from jinja2 import TemplateError from werkzeug.exceptions import InternalServerError from crc import ma, app @@ -111,8 +112,6 @@ class ApiError(Exception): return ApiError.from_task_spec(code, message, exp.sender) - - class ApiErrorSchema(ma.Schema): class Meta: fields = ("code", "message", "workflow_name", "file_name", "task_name", "task_id", diff --git a/crc/services/workflow_service.py b/crc/services/workflow_service.py index 00305f6f..93147508 100755 --- a/crc/services/workflow_service.py +++ b/crc/services/workflow_service.py @@ -1,6 +1,8 @@ import copy import json import string +import sys +import traceback from datetime import datetime import random import string @@ -758,12 +760,24 @@ class WorkflowService(object): try: return JinjaService.get_content(raw_doc, spiff_task.data) - except jinja2.exceptions.TemplateError as ue: - raise ApiError.from_task(code="template_error", message="Error processing template for task %s: %s" % - (spiff_task.task_spec.name, str(ue)), task=spiff_task) + except jinja2.exceptions.TemplateSyntaxError as tse: + error_line = documentation.splitlines()[tse.lineno - 1] + raise ApiError.from_task(code="template_error", message="Jinja Template Error: %s" % str(tse), + task=spiff_task, line_number=tse.lineno, error_line=error_line) + except jinja2.exceptions.TemplateError as te: + # Figure out the line number in the template that caused the error. + cl, exc, tb = sys.exc_info() + line_number = None + error_line = None + for frameSummary in traceback.extract_tb(tb): + if frameSummary.filename == '