Allow loging js execution errors

This commit is contained in:
Adria Navarro 2024-02-12 14:00:32 +01:00
parent 7aa1176a36
commit b27ca57e1a
3 changed files with 7 additions and 0 deletions

View File

@ -8,6 +8,9 @@ const { getJsHelperList } = require("./list")
let runJS
module.exports.setJSRunner = runner => (runJS = runner)
let onErrorLog
module.exports.setOnErrorLog = delegate => (onErrorLog = delegate)
// Helper utility to strip square brackets from a value
const removeSquareBrackets = value => {
if (!value || typeof value !== "string") {
@ -56,6 +59,8 @@ module.exports.processJS = (handlebars, context) => {
const res = { data: runJS(js, sandboxContext) }
return `{{${LITERAL_MARKER} js_result-${JSON.stringify(res)}}}`
} catch (error) {
onErrorLog && onErrorLog(error)
if (error.code === "ERR_SCRIPT_EXECUTION_TIMEOUT") {
return "Timed out while executing JS"
}

View File

@ -365,6 +365,7 @@ module.exports.doesContainString = (template, string) => {
}
module.exports.setJSRunner = javascript.setJSRunner
module.exports.setOnErrorLog = javascript.setOnErrorLog
module.exports.convertToJS = hbs => {
const blocks = exports.findHBSBlocks(hbs)

View File

@ -20,6 +20,7 @@ export const disableEscaping = templates.disableEscaping
export const findHBSBlocks = templates.findHBSBlocks
export const convertToJS = templates.convertToJS
export const setJSRunner = templates.setJSRunner
export const setOnErrorLog = templates.setOnErrorLog
export const FIND_ANY_HBS_REGEX = templates.FIND_ANY_HBS_REGEX
export const helpersToRemoveForJs = templates.helpersToRemoveForJs