Get errors working on the client side as well.

This commit is contained in:
Sam Rose 2024-10-03 12:43:57 +01:00
parent 9bb6855967
commit fdbe633b02
No known key found for this signature in database
2 changed files with 33 additions and 10 deletions

View File

@ -8,22 +8,29 @@
export let evaluating = false export let evaluating = false
export let expression = null export let expression = null
$: error = expressionResult === "Error while executing JS" $: error = expressionResult && expressionResult.error != null
$: empty = expression == null || expression?.trim() === "" $: empty = expression == null || expression?.trim() === ""
$: success = !error && !empty $: success = !error && !empty
$: highlightedResult = highlight(expressionResult) $: highlightedResult = highlight(expressionResult)
const highlight = json => { const highlight = result => {
if (json == null) { if (result == null) {
return "" return ""
} }
// Attempt to parse and then stringify, in case this is valid JSON
try { let str
json = JSON.stringify(JSON.parse(json), null, 2) if (result.error) {
} catch (err) { str = result.error.toString()
// Ignore } 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", keyColor: "#e06c75",
numberColor: "#e5c07b", numberColor: "#e5c07b",
stringColor: "#98c379", stringColor: "#98c379",
@ -34,7 +41,7 @@
} }
const copy = () => { const copy = () => {
let clipboardVal = expressionResult let clipboardVal = expressionResult.result
if (typeof clipboardVal === "object") { if (typeof clipboardVal === "object") {
clipboardVal = JSON.stringify(clipboardVal, null, 2) clipboardVal = JSON.stringify(clipboardVal, null, 2)
} }

View File

@ -463,6 +463,22 @@ export function defaultJSSetup() {
setTimeout: undefined, setTimeout: undefined,
} }
createContext(context) 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 }) return runInNewContext(js, context, { timeout: 1000 })
}) })
} else { } else {