Get errors working on the client side as well.
This commit is contained in:
parent
9bb6855967
commit
fdbe633b02
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
Loading…
Reference in New Issue