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 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)
}

View File

@ -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 {