1
0
Fork 0
mirror of synced 2024-08-17 02:51:55 +12:00

Handle js timeouts

This commit is contained in:
Adria Navarro 2024-01-24 18:03:09 +01:00
parent 73c977d6fb
commit 37033dd468
5 changed files with 111 additions and 80 deletions

View file

@ -1,6 +1,6 @@
import ivm from "isolated-vm"
import env from "./environment"
import { setJSRunner } from "@budibase/string-templates"
import { setJSRunner, JsErrorTimeout } from "@budibase/string-templates"
import { context } from "@budibase/backend-core"
import tracer from "dd-trace"
import fs from "fs"
@ -22,6 +22,7 @@ class ExecutionTimeoutError extends Error {
export function init() {
setJSRunner((js: string, ctx: Record<string, any>) => {
return tracer.trace("runJS", {}, span => {
try {
const bbCtx = context.getCurrentContext()!
const isolateRefs = bbCtx.isolateRefs
@ -130,6 +131,13 @@ export function init() {
})
return result
} catch (error: any) {
if (error.message === "Script execution timed out.") {
throw new JsErrorTimeout()
}
throw error
}
})
})
}

View file

@ -0,0 +1,11 @@
class JsErrorTimeout extends Error {
code = "ERR_SCRIPT_EXECUTION_TIMEOUT"
constructor() {
super()
}
}
module.exports = {
JsErrorTimeout,
}

View file

@ -35,3 +35,8 @@ if (!process.env.NO_JS) {
return vm.run(js)
})
}
const errors = require("./errors")
for (const error in errors) {
module.exports[error] = errors[error]
}

View file

@ -394,3 +394,8 @@ module.exports.convertToJS = hbs => {
}
module.exports.FIND_ANY_HBS_REGEX = FIND_ANY_HBS_REGEX
const errors = require("./errors")
for (const error in errors) {
module.exports[error] = errors[error]
}

View file

@ -37,3 +37,5 @@ if (process && !process.env.NO_JS) {
return vm.runInNewContext(js, context, { timeout: 1000 })
})
}
export * from "./errors.js"