Skip to content

Commit 8325b8c

Browse files
80avinbrettz9
authored andcommitted
chore: add error reporting in demo for failing expressions
1 parent 34281ac commit 8325b8c

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

demo/index.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,42 @@
1313

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

16+
const jsonpathEl = $('#jsonpath');
1617
const updateResults = () => {
1718
const jsonSample = $('#jsonSample');
1819
const reportValidity = () => {
1920
// Doesn't work without a timeout
2021
setTimeout(() => {
2122
jsonSample.reportValidity();
23+
jsonpathEl.reportValidity();
2224
});
2325
};
2426
let json;
27+
jsonSample.setCustomValidity('');
28+
jsonpathEl.setCustomValidity('');
29+
reportValidity();
2530
try {
2631
json = JSON.parse(jsonSample.value);
27-
jsonSample.setCustomValidity('');
28-
reportValidity();
2932
} catch (err) {
3033
jsonSample.setCustomValidity('Error parsing JSON: ' + err.toString());
3134
reportValidity();
3235
return;
3336
}
34-
const result = new JSONPath.JSONPath({
35-
path: $('#jsonpath').value,
36-
json,
37-
eval: $('#eval').value === 'false' ? false : $('#eval').value,
38-
ignoreEvalErrors: $('#ignoreEvalErrors').value === 'true'
39-
});
40-
41-
$('#results').value = JSON.stringify(result, null, 2);
37+
try {
38+
const result = new JSONPath.JSONPath({
39+
path: jsonpathEl.value,
40+
json,
41+
eval: $('#eval').value === 'false' ? false : $('#eval').value,
42+
ignoreEvalErrors: $('#ignoreEvalErrors').value === 'true'
43+
});
44+
$('#results').value = JSON.stringify(result, null, 2);
45+
} catch (err) {
46+
jsonpathEl.setCustomValidity(
47+
'Error executing JSONPath: ' + err.toString()
48+
);
49+
reportValidity();
50+
$('#results').value = '';
51+
}
4252
};
4353

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

6070
window.addEventListener('load', updateResults);
61-

0 commit comments

Comments
 (0)