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 { Icon, ProgressCircle, notifications } from "@budibase/bbui"
import { copyToClipboard } from "@budibase/bbui/helpers" import { copyToClipboard } from "@budibase/bbui/helpers"
import { fade } from "svelte/transition" import { fade } from "svelte/transition"
import { UserScriptError } from "@budibase/string-templates"
export let expressionResult export let expressionResult
export let expressionError export let expressionError
@ -15,7 +16,7 @@
$: highlightedResult = highlight(expressionResult) $: highlightedResult = highlight(expressionResult)
const formatError = err => { const formatError = err => {
if (err.code === "USER_SCRIPT_ERROR") { if (err.code === UserScriptError.code) {
return err.userScriptError.toString() return err.userScriptError.toString()
} }
return err.toString() return err.toString()

View File

@ -7,27 +7,13 @@ import querystring from "querystring"
import { BundleType, loadBundle } from "../bundles" import { BundleType, loadBundle } from "../bundles"
import { Snippet, VM } from "@budibase/types" import { Snippet, VM } from "@budibase/types"
import { iifeWrapper } from "@budibase/string-templates" import {
iifeWrapper,
JsErrorTimeout,
UserScriptError,
} from "@budibase/string-templates"
import environment from "../../environment" 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 { export class IsolatedVM implements VM {
private isolate: ivm.Isolate private isolate: ivm.Isolate
private vm: ivm.Context private vm: ivm.Context
@ -223,7 +209,7 @@ export class IsolatedVM implements VM {
if (this.isolateAccumulatedTimeout) { if (this.isolateAccumulatedTimeout) {
const cpuMs = Number(this.isolate.cpuTime) / 1e6 const cpuMs = Number(this.isolate.cpuTime) / 1e6
if (cpuMs > this.isolateAccumulatedTimeout) { if (cpuMs > this.isolateAccumulatedTimeout) {
throw new ExecutionTimeoutError( throw new JsErrorTimeout(
`CPU time limit exceeded (${cpuMs}ms > ${this.isolateAccumulatedTimeout}ms)` `CPU time limit exceeded (${cpuMs}ms > ${this.isolateAccumulatedTimeout}ms)`
) )
} }

View File

@ -1,5 +1,5 @@
import { AutoFieldDefaultNames } from "../../constants" import { AutoFieldDefaultNames } from "../../constants"
import { processStringSync } from "@budibase/string-templates" import { processStringSync, UserScriptError } from "@budibase/string-templates"
import { import {
AutoColumnFieldMetadata, AutoColumnFieldMetadata,
FieldSchema, FieldSchema,
@ -84,7 +84,7 @@ export async function processFormulas<T extends Row | Row[]>(
try { try {
return processStringSync(formula, context) return processStringSync(formula, context)
} catch (err: any) { } catch (err: any) {
if (err.code === "USER_SCRIPT_ERROR") { if (err.code === UserScriptError.code) {
return err.userScriptError.toString() return err.userScriptError.toString()
} }
throw err throw err

View File

@ -1,9 +1,9 @@
export class JsErrorTimeout extends Error { export class JsErrorTimeout extends Error {
code = "ERR_SCRIPT_EXECUTION_TIMEOUT" static code = "ERR_SCRIPT_EXECUTION_TIMEOUT"
} }
export class UserScriptError extends Error { export class UserScriptError extends Error {
code = "USER_SCRIPT_ERROR" static code = "USER_SCRIPT_ERROR"
constructor(readonly userScriptError: Error) { constructor(readonly userScriptError: Error) {
super( super(

View File

@ -3,6 +3,7 @@ import cloneDeep from "lodash/fp/cloneDeep"
import { LITERAL_MARKER } from "../helpers/constants" import { LITERAL_MARKER } from "../helpers/constants"
import { getJsHelperList } from "./list" import { getJsHelperList } from "./list"
import { iifeWrapper } from "../iife" import { iifeWrapper } from "../iife"
import { JsErrorTimeout, UserScriptError } from "../errors"
// The method of executing JS scripts depends on the bundle being built. // 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). // 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) { } catch (error: any) {
onErrorLog && onErrorLog(error) onErrorLog && onErrorLog(error)
if (error.code === "ERR_SCRIPT_EXECUTION_TIMEOUT") { if (error.code === JsErrorTimeout.code) {
return "Timed out while executing JS" return "Timed out while executing JS"
} }
if (error.name === "ExecutionTimeoutError") { if (error.code === UserScriptError.code) {
return "Request JS execution limit hit"
}
if (error.code === "USER_SCRIPT_ERROR") {
throw error throw error
} }
return "Error while executing JS" return "Error while executing JS"

View File

@ -231,7 +231,7 @@ export function processStringSync(
return process(string) return process(string)
} }
} catch (err: any) { } catch (err: any) {
if (err.code === "USER_SCRIPT_ERROR") { if (err.code === UserScriptError.code) {
throw err throw err
} }
return input return input
@ -468,7 +468,7 @@ export function defaultJSSetup() {
} }
createContext(context) createContext(context)
js = ` const wrappedJs = `
result = { result = {
result: null, result: null,
error: null, error: null,
@ -483,7 +483,7 @@ export function defaultJSSetup() {
result; result;
` `
const result = runInNewContext(js, context, { timeout: 1000 }) const result = runInNewContext(wrappedJs, context, { timeout: 1000 })
if (result.error) { if (result.error) {
throw new UserScriptError(result.error) throw new UserScriptError(result.error)
} }