{"record":{"id":"75d92224db3c14c3","repo":"Budibase/budibase","slug":"result-error","errorCode":null,"errorMessage":"result.error","messagePattern":"result\\.error","errorType":"exception","errorClass":"UserScriptError","httpStatus":null,"severity":"error","filePath":"packages/string-templates/src/index.ts","lineNumber":743,"sourceCode":"  }\n  js += \"`;\"\n  return `${varBlock}${js}`\n}\n\nexport { JsTimeoutError, UserScriptError } from \"./errors\"\n\nexport function browserJSSetup() {\n  // tests are in jest - we need to use node VM for these\n  const jsSandbox = isTest() ? vm : browserVM\n  // Use polyfilled vm to run JS scripts in a browser Env\n  setJSRunner((js: string, context: Record<string, any>) => {\n    jsSandbox.createContext(context)\n\n    const wrappedJs = frontendWrapJS(js)\n\n    const result = jsSandbox.runInNewContext(wrappedJs, context)\n    if (result.error) {\n      throw new UserScriptError(result.error)\n    }\n    return result.result\n  })\n}\n\nexport function defaultJSSetup() {\n  if (!isBackendService()) {\n    browserJSSetup()\n  } else {\n    removeJSRunner()\n  }\n}\ndefaultJSSetup()\n","sourceCodeStart":725,"sourceCodeEnd":757,"githubUrl":"https://github.com/Budibase/budibase/blob/a81a902e9a8fe55b467d106765f6638f12e35c49/packages/string-templates/src/index.ts#L725-L757","documentation":"User scripts run in a sandboxed JS context via jsSandbox.runInNewContext. The sandbox captures runtime errors instead of throwing natively, returning them as result.error. string-templates wraps that value in a UserScriptError so the caller sees the script's own failure message.","triggerScenarios":"A JS binding/script evaluated at runtime throws inside the sandbox: referencing an undefined variable, calling a non-existent method, throwing explicitly, or an unhandled runtime error during execution.","commonSituations":"App builders write JS bindings with typos, use APIs that don't exist in the sandbox context, or scripts that assume data present at design time but absent at render time (e.g. a row field is null).","solutions":["Read the UserScriptError message to find the failing line/identifier in the script","Fix the script: guard against null/undefined inputs before use","Verify every referenced helper/variable exists in the sandbox context (defaultJSSetup provides the available helpers)","Reproduce the expression with sample data to confirm the fix before deploying"],"exampleFix":"// before\nreturn data.row.someField.toUpperCase()\n// after\nreturn data.row?.someField ? data.row.someField.toUpperCase() : \"\"","handlingStrategy":"try-catch","validationCode":"// before evaluating\nfunction validateScriptUsable(js: string, ctxKeys: string[]) {\n  if (!js || !js.trim()) throw new Error(\"Empty script\")\n  // optionally regex-scan for identifiers not present in ctxKeys\n}","typeGuard":"function hasScriptError(r: { error?: unknown; result?: unknown }): r is { error: unknown } {\n  return r != null && r.error != null\n}","tryCatchPattern":"try {\n  const out = await processString(js)\n} catch (err) {\n  if (err instanceof UserScriptError) {\n    // surface err.message to the user as a script-authoring problem\n  } else { throw err }\n}","preventionTips":["Guard null/undefined data inside the script before use","Only reference helpers provided by the sandbox context (defaultJSSetup)","Test bindings with empty/edge-case data at design time","Keep scripts small and single-purpose so failures are easy to localize"],"tags":["javascript","sandbox","user-script","runtime-error"],"backgroundTag":"sandbox-script-runtime-error","analyzedSha":"a81a902e9a8fe55b467d106765f6638f12e35c49","analyzedAt":"2026-08-29T01:03:10.972Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}