Fix tests.

This commit is contained in:
Sam Rose 2024-10-03 16:16:42 +01:00
parent 646fc5e6bd
commit 03c514be4c
No known key found for this signature in database
6 changed files with 34 additions and 22 deletions

View File

@ -49,6 +49,7 @@ import * as uuid from "uuid"
import { Knex } from "knex" import { Knex } from "knex"
import { InternalTables } from "../../../db/utils" import { InternalTables } from "../../../db/utils"
import { withEnv } from "../../../environment" import { withEnv } from "../../../environment"
import { JsTimeoutError } from "@budibase/string-templates/src/errors"
const timestamp = new Date("2023-01-26T11:48:57.597Z").toISOString() const timestamp = new Date("2023-01-26T11:48:57.597Z").toISOString()
tk.freeze(timestamp) tk.freeze(timestamp)
@ -76,13 +77,13 @@ async function waitForEvent(
} }
describe.each([ describe.each([
["lucene", undefined], // ["lucene", undefined],
["sqs", undefined], ["sqs", undefined],
[DatabaseName.POSTGRES, getDatasource(DatabaseName.POSTGRES)], // [DatabaseName.POSTGRES, getDatasource(DatabaseName.POSTGRES)],
[DatabaseName.MYSQL, getDatasource(DatabaseName.MYSQL)], // [DatabaseName.MYSQL, getDatasource(DatabaseName.MYSQL)],
[DatabaseName.SQL_SERVER, getDatasource(DatabaseName.SQL_SERVER)], // [DatabaseName.SQL_SERVER, getDatasource(DatabaseName.SQL_SERVER)],
[DatabaseName.MARIADB, getDatasource(DatabaseName.MARIADB)], // [DatabaseName.MARIADB, getDatasource(DatabaseName.MARIADB)],
[DatabaseName.ORACLE, getDatasource(DatabaseName.ORACLE)], // [DatabaseName.ORACLE, getDatasource(DatabaseName.ORACLE)],
])("/rows (%s)", (providerType, dsProvider) => { ])("/rows (%s)", (providerType, dsProvider) => {
const isInternal = dsProvider === undefined const isInternal = dsProvider === undefined
const isLucene = providerType === "lucene" const isLucene = providerType === "lucene"
@ -3013,7 +3014,7 @@ describe.each([
let i = 0 let i = 0
for (; i < 10; i++) { for (; i < 10; i++) {
const row = rows[i] const row = rows[i]
if (row.formula !== "Timed out while executing JS") { if (row.formula !== JsTimeoutError.message) {
break break
} }
} }
@ -3027,7 +3028,7 @@ describe.each([
for (; i < 10; i++) { for (; i < 10; i++) {
const row = rows[i] const row = rows[i]
expect(row.text).toBe("foo") expect(row.text).toBe("foo")
expect(row.formula).toBe("Request JS execution limit hit") expect(row.formula).toStartWith("CPU time limit exceeded ")
} }
} }
} }

View File

@ -1,7 +1,7 @@
import { serializeError } from "serialize-error" import { serializeError } from "serialize-error"
import env from "../environment" import env from "../environment"
import { import {
JsErrorTimeout, JsTimeoutError,
setJSRunner, setJSRunner,
setOnErrorLog, setOnErrorLog,
} from "@budibase/string-templates" } from "@budibase/string-templates"
@ -40,7 +40,7 @@ export function init() {
return vm.withContext(rest, () => vm.execute(js)) return vm.withContext(rest, () => vm.execute(js))
} catch (error: any) { } catch (error: any) {
if (error.message === "Script execution timed out.") { if (error.message === "Script execution timed out.") {
throw new JsErrorTimeout() throw new JsTimeoutError()
} }
throw error throw error
} }

View File

@ -7,13 +7,14 @@ 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 { import { iifeWrapper, UserScriptError } from "@budibase/string-templates"
iifeWrapper,
JsErrorTimeout,
UserScriptError,
} from "@budibase/string-templates"
import environment from "../../environment" import environment from "../../environment"
export class JsRequestTimeoutError extends Error {
static code = "JS_REQUEST_TIMEOUT_ERROR"
code = JsRequestTimeoutError.code
}
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
@ -209,7 +210,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 JsErrorTimeout( throw new JsRequestTimeoutError(
`CPU time limit exceeded (${cpuMs}ms > ${this.isolateAccumulatedTimeout}ms)` `CPU time limit exceeded (${cpuMs}ms > ${this.isolateAccumulatedTimeout}ms)`
) )
} }

View File

@ -1,9 +1,16 @@
export class JsErrorTimeout extends Error { export class JsTimeoutError extends Error {
static code = "ERR_SCRIPT_EXECUTION_TIMEOUT" static message = "Timed out while executing JS"
static code = "JS_TIMEOUT_ERROR"
code: string = JsTimeoutError.code
constructor() {
super(JsTimeoutError.message)
}
} }
export class UserScriptError extends Error { export class UserScriptError extends Error {
static code = "USER_SCRIPT_ERROR" static code = "USER_SCRIPT_ERROR"
code: string = UserScriptError.code
constructor(readonly userScriptError: Error) { constructor(readonly userScriptError: Error) {
super( super(

View File

@ -3,7 +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" import { JsTimeoutError, 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).
@ -95,8 +95,11 @@ export function processJS(handlebars: string, context: any) {
} catch (error: any) { } catch (error: any) {
onErrorLog && onErrorLog(error) onErrorLog && onErrorLog(error)
if (error.code === JsErrorTimeout.code) { if (error.code === "JS_REQUEST_TIMEOUT_ERROR") {
return "Timed out while executing JS" return error.message
}
if (error.code === JsTimeoutError.code) {
return JsTimeoutError.message
} }
if (error.code === UserScriptError.code) { if (error.code === UserScriptError.code) {
throw error throw error

View File

@ -452,7 +452,7 @@ export function convertToJS(hbs: string) {
return `${varBlock}${js}` return `${varBlock}${js}`
} }
export { JsErrorTimeout, UserScriptError } from "./errors" export { JsTimeoutError, UserScriptError } from "./errors"
export function defaultJSSetup() { export function defaultJSSetup() {
if (!isBackendService()) { if (!isBackendService()) {