bug fix: 修复由于语法错误导致预处理脚本或后处理脚本执行报错,而报错的行号是错误的问题

This commit is contained in:
azhengzz 2021-09-27 23:52:20 +08:00
parent ab9443fed4
commit 3518a9a19a
1 changed files with 12 additions and 6 deletions

View File

@ -29,20 +29,26 @@ def exec_script(source, project_id, log, script_type, glb=None, loc=None):
loc = {}
loc['vars'] = vars
loc['log'] = log
loc['__name__'] = '__main__'
try:
exec(source, glb, loc)
except Exception as e:
tb = sys.exc_info()[-1]
script_tb = tb.tb_next
if isinstance(e, SyntaxError):
lineno = e.lineno
else:
tb = sys.exc_info()[-1]
while tb.tb_next:
tb = tb.tb_next
lineno = tb.tb_lineno
if script_type == 'PREPROCESSOR_SCRIPT':
failure_message = '预处理脚本执行异常: m行号: %s, 错误信息: %s, 错误类型: %s' % (script_tb.tb_lineno, e.args[0], type(e))
script_exception = PreScriptExecException(script_tb.tb_lineno, e.args[0], failure_message)
failure_message = '预处理脚本执行异常: m行号: %s, 错误信息: %s, 错误类型: %s' % (lineno, e.args[0], type(e))
script_exception = PreScriptExecException(lineno, e.args[0], failure_message)
log.error('预处理脚本执行异常: 行号: %s, 错误信息: %s' % (script_exception.line_no, script_exception.value))
raise script_exception
elif script_type == 'POSTPROCESSOR_SCRIPT':
loc['failure'] = True # 后处理脚本异常时,将断言结果置为失败
failure_message = '后处理脚本执行异常: 行号: %s, 错误信息: %s, 错误类型: %s' % (script_tb.tb_lineno, e.args[0], type(e))
script_exception = PostScriptExecException(script_tb.tb_lineno, e.args[0], failure_message)
failure_message = '后处理脚本执行异常: 行号: %s, 错误信息: %s, 错误类型: %s' % (lineno, e.args[0], type(e))
script_exception = PostScriptExecException(lineno, e.args[0], failure_message)
loc['failure_message'] = failure_message
log.error(failure_message)
raise script_exception