Merge pull request #13012 from Budibase/chore/allow-logging-js-execution-errors

Add environment variable to log JS errors in bindings
This commit is contained in:
Michael Drury 2024-02-12 13:11:34 +00:00 committed by GitHub
commit bbc2a7142e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 16 additions and 2 deletions

View File

@ -97,6 +97,7 @@ const environment = {
APP_MIGRATION_TIMEOUT: parseIntSafe(process.env.APP_MIGRATION_TIMEOUT), APP_MIGRATION_TIMEOUT: parseIntSafe(process.env.APP_MIGRATION_TIMEOUT),
JS_RUNNER_MEMORY_LIMIT: JS_RUNNER_MEMORY_LIMIT:
parseIntSafe(process.env.JS_RUNNER_MEMORY_LIMIT) || 64, parseIntSafe(process.env.JS_RUNNER_MEMORY_LIMIT) || 64,
LOG_JS_ERRORS: process.env.LOG_JS_ERRORS,
} }
// threading can cause memory issues with node-ts in development // threading can cause memory issues with node-ts in development

View File

@ -1,7 +1,7 @@
import vm from "vm" import vm from "vm"
import env from "../environment" import env from "../environment"
import { setJSRunner } from "@budibase/string-templates" import { setJSRunner, setOnErrorLog } from "@budibase/string-templates"
import { context, timers } from "@budibase/backend-core" import { context, logging, timers } from "@budibase/backend-core"
import tracer from "dd-trace" import tracer from "dd-trace"
type TrackerFn = <T>(f: () => T) => T type TrackerFn = <T>(f: () => T) => T
@ -58,4 +58,10 @@ export function init() {
) )
}) })
}) })
if (env.LOG_JS_ERRORS) {
setOnErrorLog((error: Error) => {
logging.logWarn(JSON.stringify(error))
})
}
} }

View File

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

View File

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

View File

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