Work out error handling

This commit is contained in:
jos 2020-01-04 11:11:09 +01:00
parent 40d312b987
commit 32871f85fd
1 changed files with 38 additions and 24 deletions

View File

@ -191,9 +191,6 @@ export function showTransformModal (
}
}
// initialize with empty query
query.value = createQuery(json, {})
function preprocessPath (path) {
return (path === '')
? '@'
@ -202,6 +199,40 @@ export function showTransformModal (
: path
}
function updatePreview () {
try {
const transformed = executeQuery(value, query.value)
preview.className = 'jsoneditor-transform-preview'
preview.value = stringifyPartial(transformed, 2, MAX_PREVIEW_CHARACTERS)
ok.disabled = false
} catch (err) {
preview.className = 'jsoneditor-transform-preview jsoneditor-error'
preview.value = err.toString()
ok.disabled = true
}
}
const debouncedUpdatePreview = debounce(updatePreview, 300)
function tryCreateQuery (json, queryOptions) {
try {
query.value = createQuery(json, queryOptions)
ok.disabled = false
debouncedUpdatePreview()
} catch (err) {
const message = 'Error: an error happened when executing "createQuery": ' + (err.message || err.toString())
query.value = ''
ok.disabled = true
preview.className = 'jsoneditor-transform-preview jsoneditor-error'
preview.value = message
}
}
function generateQueryFromWizard () {
const queryOptions = {}
@ -234,30 +265,10 @@ export function showTransformModal (
}
}
query.value = createQuery(json, queryOptions)
debouncedUpdatePreview()
tryCreateQuery(json, queryOptions)
}
function updatePreview () {
try {
const transformed = executeQuery(value, query.value)
preview.className = 'jsoneditor-transform-preview'
preview.value = stringifyPartial(transformed, 2, MAX_PREVIEW_CHARACTERS)
ok.disabled = false
} catch (err) {
preview.className = 'jsoneditor-transform-preview jsoneditor-error'
preview.value = err.toString()
ok.disabled = true
}
}
var debouncedUpdatePreview = debounce(updatePreview, 300)
query.oninput = debouncedUpdatePreview
debouncedUpdatePreview()
ok.onclick = event => {
event.preventDefault()
@ -268,6 +279,9 @@ export function showTransformModal (
onTransform(query.value)
}
// initialize with empty query
tryCreateQuery(json, {})
setTimeout(() => {
query.select()
query.focus()