From fdbe633b02be6adfe6956a3c08347dcf193d06cb Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Thu, 3 Oct 2024 12:43:57 +0100 Subject: [PATCH] Get errors working on the client side as well. --- .../bindings/EvaluationSidePanel.svelte | 27 ++++++++++++------- packages/string-templates/src/index.ts | 16 +++++++++++ 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/packages/builder/src/components/common/bindings/EvaluationSidePanel.svelte b/packages/builder/src/components/common/bindings/EvaluationSidePanel.svelte index 2c4e6a0991..3b71fad715 100644 --- a/packages/builder/src/components/common/bindings/EvaluationSidePanel.svelte +++ b/packages/builder/src/components/common/bindings/EvaluationSidePanel.svelte @@ -8,22 +8,29 @@ export let evaluating = false export let expression = null - $: error = expressionResult === "Error while executing JS" + $: error = expressionResult && expressionResult.error != null $: empty = expression == null || expression?.trim() === "" $: success = !error && !empty $: highlightedResult = highlight(expressionResult) - const highlight = json => { - if (json == null) { + const highlight = result => { + if (result == null) { return "" } - // Attempt to parse and then stringify, in case this is valid JSON - try { - json = JSON.stringify(JSON.parse(json), null, 2) - } catch (err) { - // Ignore + + let str + if (result.error) { + str = result.error.toString() + } else { + // Attempt to parse and then stringify, in case this is valid result + try { + str = JSON.stringify(JSON.parse(result.result), null, 2) + } catch (err) { + // Ignore + } } - return formatHighlight(json, { + + return formatHighlight(str, { keyColor: "#e06c75", numberColor: "#e5c07b", stringColor: "#98c379", @@ -34,7 +41,7 @@ } const copy = () => { - let clipboardVal = expressionResult + let clipboardVal = expressionResult.result if (typeof clipboardVal === "object") { clipboardVal = JSON.stringify(clipboardVal, null, 2) } diff --git a/packages/string-templates/src/index.ts b/packages/string-templates/src/index.ts index 8d5fe4c16d..69ca05ef76 100644 --- a/packages/string-templates/src/index.ts +++ b/packages/string-templates/src/index.ts @@ -463,6 +463,22 @@ export function defaultJSSetup() { setTimeout: undefined, } createContext(context) + + js = ` + result = { + result: null, + error: null, + }; + + try { + result.result = ${js}; + } catch (e) { + result.error = e.toString(); + } + + result; + ` + return runInNewContext(js, context, { timeout: 1000 }) }) } else {