Skip to content

Commit

Permalink
chore: add error reporting in demo for failing expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
80avin authored and brettz9 committed Oct 1, 2024
1 parent 34281ac commit 8325b8c
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,42 @@

const $ = (s) => document.querySelector(s);

const jsonpathEl = $('#jsonpath');
const updateResults = () => {
const jsonSample = $('#jsonSample');
const reportValidity = () => {
// Doesn't work without a timeout
setTimeout(() => {
jsonSample.reportValidity();
jsonpathEl.reportValidity();
});
};
let json;
jsonSample.setCustomValidity('');
jsonpathEl.setCustomValidity('');
reportValidity();
try {
json = JSON.parse(jsonSample.value);
jsonSample.setCustomValidity('');
reportValidity();
} catch (err) {
jsonSample.setCustomValidity('Error parsing JSON: ' + err.toString());
reportValidity();
return;
}
const result = new JSONPath.JSONPath({
path: $('#jsonpath').value,
json,
eval: $('#eval').value === 'false' ? false : $('#eval').value,
ignoreEvalErrors: $('#ignoreEvalErrors').value === 'true'
});

$('#results').value = JSON.stringify(result, null, 2);
try {
const result = new JSONPath.JSONPath({
path: jsonpathEl.value,
json,
eval: $('#eval').value === 'false' ? false : $('#eval').value,
ignoreEvalErrors: $('#ignoreEvalErrors').value === 'true'
});
$('#results').value = JSON.stringify(result, null, 2);
} catch (err) {
jsonpathEl.setCustomValidity(
'Error executing JSONPath: ' + err.toString()
);
reportValidity();
$('#results').value = '';
}
};

$('#jsonpath').addEventListener('input', () => {
Expand All @@ -58,4 +68,3 @@ $('#ignoreEvalErrors').addEventListener('change', () => {
});

window.addEventListener('load', updateResults);

0 comments on commit 8325b8c

Please sign in to comment.