From 646fc5e6bdde971b5c08b62bb0b529022740f9de Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Thu, 3 Oct 2024 15:56:19 +0100 Subject: [PATCH] Respond to PR comment. --- .../bindings/EvaluationSidePanel.svelte | 3 ++- .../server/src/jsRunner/vm/isolated-vm.ts | 26 +++++-------------- .../src/utilities/rowProcessor/utils.ts | 4 +-- packages/string-templates/src/errors.ts | 4 +-- .../src/helpers/javascript.ts | 8 +++--- packages/string-templates/src/index.ts | 6 ++--- 6 files changed, 18 insertions(+), 33 deletions(-) diff --git a/packages/builder/src/components/common/bindings/EvaluationSidePanel.svelte b/packages/builder/src/components/common/bindings/EvaluationSidePanel.svelte index 2c5c1d4587..ffb8f45297 100644 --- a/packages/builder/src/components/common/bindings/EvaluationSidePanel.svelte +++ b/packages/builder/src/components/common/bindings/EvaluationSidePanel.svelte @@ -3,6 +3,7 @@ import { Icon, ProgressCircle, notifications } from "@budibase/bbui" import { copyToClipboard } from "@budibase/bbui/helpers" import { fade } from "svelte/transition" + import { UserScriptError } from "@budibase/string-templates" export let expressionResult export let expressionError @@ -15,7 +16,7 @@ $: highlightedResult = highlight(expressionResult) const formatError = err => { - if (err.code === "USER_SCRIPT_ERROR") { + if (err.code === UserScriptError.code) { return err.userScriptError.toString() } return err.toString() diff --git a/packages/server/src/jsRunner/vm/isolated-vm.ts b/packages/server/src/jsRunner/vm/isolated-vm.ts index 0a9e93f475..87478a2f8a 100644 --- a/packages/server/src/jsRunner/vm/isolated-vm.ts +++ b/packages/server/src/jsRunner/vm/isolated-vm.ts @@ -7,27 +7,13 @@ import querystring from "querystring" import { BundleType, loadBundle } from "../bundles" import { Snippet, VM } from "@budibase/types" -import { iifeWrapper } from "@budibase/string-templates" +import { + iifeWrapper, + JsErrorTimeout, + UserScriptError, +} from "@budibase/string-templates" import environment from "../../environment" -class ExecutionTimeoutError extends Error { - code = "ERR_SCRIPT_EXECUTION_TIMEOUT" - constructor(message: string) { - super(message) - this.name = "ExecutionTimeoutError" - } -} - -class UserScriptError extends Error { - code = "USER_SCRIPT_ERROR" - constructor(readonly userScriptError: Error) { - super( - `error while running user-supplied JavaScript: ${userScriptError.toString()}`, - { cause: userScriptError } - ) - } -} - export class IsolatedVM implements VM { private isolate: ivm.Isolate private vm: ivm.Context @@ -223,7 +209,7 @@ export class IsolatedVM implements VM { if (this.isolateAccumulatedTimeout) { const cpuMs = Number(this.isolate.cpuTime) / 1e6 if (cpuMs > this.isolateAccumulatedTimeout) { - throw new ExecutionTimeoutError( + throw new JsErrorTimeout( `CPU time limit exceeded (${cpuMs}ms > ${this.isolateAccumulatedTimeout}ms)` ) } diff --git a/packages/server/src/utilities/rowProcessor/utils.ts b/packages/server/src/utilities/rowProcessor/utils.ts index 3a1ce08ece..29a5de221f 100644 --- a/packages/server/src/utilities/rowProcessor/utils.ts +++ b/packages/server/src/utilities/rowProcessor/utils.ts @@ -1,5 +1,5 @@ import { AutoFieldDefaultNames } from "../../constants" -import { processStringSync } from "@budibase/string-templates" +import { processStringSync, UserScriptError } from "@budibase/string-templates" import { AutoColumnFieldMetadata, FieldSchema, @@ -84,7 +84,7 @@ export async function processFormulas( try { return processStringSync(formula, context) } catch (err: any) { - if (err.code === "USER_SCRIPT_ERROR") { + if (err.code === UserScriptError.code) { return err.userScriptError.toString() } throw err diff --git a/packages/string-templates/src/errors.ts b/packages/string-templates/src/errors.ts index d68461561e..7a300ac863 100644 --- a/packages/string-templates/src/errors.ts +++ b/packages/string-templates/src/errors.ts @@ -1,9 +1,9 @@ export class JsErrorTimeout extends Error { - code = "ERR_SCRIPT_EXECUTION_TIMEOUT" + static code = "ERR_SCRIPT_EXECUTION_TIMEOUT" } export class UserScriptError extends Error { - code = "USER_SCRIPT_ERROR" + static code = "USER_SCRIPT_ERROR" constructor(readonly userScriptError: Error) { super( diff --git a/packages/string-templates/src/helpers/javascript.ts b/packages/string-templates/src/helpers/javascript.ts index e1d7f3e8a3..059c8a95f5 100644 --- a/packages/string-templates/src/helpers/javascript.ts +++ b/packages/string-templates/src/helpers/javascript.ts @@ -3,6 +3,7 @@ import cloneDeep from "lodash/fp/cloneDeep" import { LITERAL_MARKER } from "../helpers/constants" import { getJsHelperList } from "./list" import { iifeWrapper } from "../iife" +import { JsErrorTimeout, UserScriptError } from "../errors" // The method of executing JS scripts depends on the bundle being built. // This setter is used in the entrypoint (either index.js or index.mjs). @@ -94,13 +95,10 @@ export function processJS(handlebars: string, context: any) { } catch (error: any) { onErrorLog && onErrorLog(error) - if (error.code === "ERR_SCRIPT_EXECUTION_TIMEOUT") { + if (error.code === JsErrorTimeout.code) { return "Timed out while executing JS" } - if (error.name === "ExecutionTimeoutError") { - return "Request JS execution limit hit" - } - if (error.code === "USER_SCRIPT_ERROR") { + if (error.code === UserScriptError.code) { throw error } return "Error while executing JS" diff --git a/packages/string-templates/src/index.ts b/packages/string-templates/src/index.ts index 8fd2e7bfc4..fe2b4c261c 100644 --- a/packages/string-templates/src/index.ts +++ b/packages/string-templates/src/index.ts @@ -231,7 +231,7 @@ export function processStringSync( return process(string) } } catch (err: any) { - if (err.code === "USER_SCRIPT_ERROR") { + if (err.code === UserScriptError.code) { throw err } return input @@ -468,7 +468,7 @@ export function defaultJSSetup() { } createContext(context) - js = ` + const wrappedJs = ` result = { result: null, error: null, @@ -483,7 +483,7 @@ export function defaultJSSetup() { result; ` - const result = runInNewContext(js, context, { timeout: 1000 }) + const result = runInNewContext(wrappedJs, context, { timeout: 1000 }) if (result.error) { throw new UserScriptError(result.error) }