fontoxpath
fontoxpath copied to clipboard
Improve error message when evaluating a query AST
When we evaluate a query AST we don't get as good error messaging as with normal string queries.
I've reproduced this in the ./demo/main.ts by changing the runNormalXPath to
async function runNormalXPath(script: string, asXQuery: boolean) {
const queryAst = fontoxpath.parseScript(
script,
{
language: fontoxpath.Language.XQUERY_3_1_LANGUAGE,
debug: false,
},
new Document()
);
const raw = [];
const it = fontoxpath.evaluateXPathToAsyncIterator(queryAst, xmlDoc, null, null, {
debug: true,
disableCache: true,
language: asXQuery
? fontoxpath.evaluateXPath.XQUERY_3_1_LANGUAGE
: fontoxpath.evaluateXPath.XPATH_3_1_LANGUAGE,
logger: {
trace: (m) => {
traceOutput.textContent = m;
console.log(m);
},
},
});
...
I know that I do set the debug flag to false when building the AST, but the evaluate function throws an invalid array length error when I set it to true instead.
Using this, when I execute the following XQuery if ((1, 2)) then '(^∀^●)ノシ' else '╰(‵□′)╯' I get the following error message
FORG0006: A wrong argument type was specified in a function call.
While I was expecting the error message as I would get it when treating the query as a string (pass the script to evaluateXPathToAsyncIterator instead of queryAst)
1: if ((1, 2)) then '(^∀^●)ノシ' else '╰(‵□′)╯'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2:
Error: FORG0006: A wrong argument type was specified in a function call.
at <ifThenElseExpr>:1:1 - 1:43
I think we should just document that we place the query in a comment at the start of the AST (or at least we do in Fonto; just do it in FontoXPath instead). From there we can make harder assumptions it's there and print it in the error.