Respond to PR comment.

This commit is contained in:
Sam Rose 2024-10-03 15:56:19 +01:00
parent 795b10c4c9
commit 646fc5e6bd
No known key found for this signature in database
6 changed files with 18 additions and 33 deletions

View File

@ -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()

View File

@ -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)`
)
}

View File

@ -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<T extends Row | Row[]>(
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

View File

@ -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(

View File

@ -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"

View File

@ -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)
}