Handle js timeouts
This commit is contained in:
parent
73c977d6fb
commit
37033dd468
|
@ -1,6 +1,6 @@
|
||||||
import ivm from "isolated-vm"
|
import ivm from "isolated-vm"
|
||||||
import env from "./environment"
|
import env from "./environment"
|
||||||
import { setJSRunner } from "@budibase/string-templates"
|
import { setJSRunner, JsErrorTimeout } from "@budibase/string-templates"
|
||||||
import { context } from "@budibase/backend-core"
|
import { context } from "@budibase/backend-core"
|
||||||
import tracer from "dd-trace"
|
import tracer from "dd-trace"
|
||||||
import fs from "fs"
|
import fs from "fs"
|
||||||
|
@ -22,6 +22,7 @@ class ExecutionTimeoutError extends Error {
|
||||||
export function init() {
|
export function init() {
|
||||||
setJSRunner((js: string, ctx: Record<string, any>) => {
|
setJSRunner((js: string, ctx: Record<string, any>) => {
|
||||||
return tracer.trace("runJS", {}, span => {
|
return tracer.trace("runJS", {}, span => {
|
||||||
|
try {
|
||||||
const bbCtx = context.getCurrentContext()!
|
const bbCtx = context.getCurrentContext()!
|
||||||
|
|
||||||
const isolateRefs = bbCtx.isolateRefs
|
const isolateRefs = bbCtx.isolateRefs
|
||||||
|
@ -130,6 +131,13 @@ export function init() {
|
||||||
})
|
})
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
} catch (error: any) {
|
||||||
|
if (error.message === "Script execution timed out.") {
|
||||||
|
throw new JsErrorTimeout()
|
||||||
|
}
|
||||||
|
|
||||||
|
throw error
|
||||||
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
class JsErrorTimeout extends Error {
|
||||||
|
code = "ERR_SCRIPT_EXECUTION_TIMEOUT"
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
super()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
JsErrorTimeout,
|
||||||
|
}
|
|
@ -35,3 +35,8 @@ if (!process.env.NO_JS) {
|
||||||
return vm.run(js)
|
return vm.run(js)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const errors = require("./errors")
|
||||||
|
for (const error in errors) {
|
||||||
|
module.exports[error] = errors[error]
|
||||||
|
}
|
||||||
|
|
|
@ -394,3 +394,8 @@ module.exports.convertToJS = hbs => {
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.FIND_ANY_HBS_REGEX = FIND_ANY_HBS_REGEX
|
module.exports.FIND_ANY_HBS_REGEX = FIND_ANY_HBS_REGEX
|
||||||
|
|
||||||
|
const errors = require("./errors")
|
||||||
|
for (const error in errors) {
|
||||||
|
module.exports[error] = errors[error]
|
||||||
|
}
|
||||||
|
|
|
@ -37,3 +37,5 @@ if (process && !process.env.NO_JS) {
|
||||||
return vm.runInNewContext(js, context, { timeout: 1000 })
|
return vm.runInNewContext(js, context, { timeout: 1000 })
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export * from "./errors.js"
|
||||||
|
|
Loading…
Reference in New Issue