From b27ca57e1aabfac5c9bd4d585ea2775a7b89f179 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 12 Feb 2024 14:00:32 +0100 Subject: [PATCH] Allow loging js execution errors --- packages/string-templates/src/helpers/javascript.js | 5 +++++ packages/string-templates/src/index.js | 1 + packages/string-templates/src/index.mjs | 1 + 3 files changed, 7 insertions(+) diff --git a/packages/string-templates/src/helpers/javascript.js b/packages/string-templates/src/helpers/javascript.js index 99d7df10f7..4a7f602690 100644 --- a/packages/string-templates/src/helpers/javascript.js +++ b/packages/string-templates/src/helpers/javascript.js @@ -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" } diff --git a/packages/string-templates/src/index.js b/packages/string-templates/src/index.js index 3636c0a456..f370b67272 100644 --- a/packages/string-templates/src/index.js +++ b/packages/string-templates/src/index.js @@ -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) diff --git a/packages/string-templates/src/index.mjs b/packages/string-templates/src/index.mjs index bdded04b02..5ac7981fee 100644 --- a/packages/string-templates/src/index.mjs +++ b/packages/string-templates/src/index.mjs @@ -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