eXist-db / templating

HTML Templating Library for eXist-db
GNU Lesser General Public License v2.1
5 stars 8 forks source link

[BUG] Stacktrace for 'dynamic' issues not usable for diagnostics #27

Open dizzzz opened 2 months ago

dizzzz commented 2 months ago

The following short example (app.xqm) shows the problem:

declare function app:test2($node as element()) as element() {
    "just a text"
};

declare function app:test($node as node(), $model as map(*)) {
    let $a := app:test2("aaaa")
    return
    <p>Dummy template output generated by function app:test at {current-dateTime()}. The templating
        function was triggered by the class attribute <code>class="app:test"</code>.</p>
};

Issue happen when the arity/type of functions (parameters, return values) do not match the expectations of the caller.

Expected behavior

An readable error/showing what the problem is.

Actual behavior

An unusable generic trace:

An error has occurred

An error has been generated by the application.

templates:NotFound No template function found for call app:test2 (Max arity of 20 has been exceeded in searching for this template function. If needed, adjust $templates:MAX_ARITY in the templates.xql module.) [at line 191, column 85, source: /myPath/exist-distribution-6.2.0/etc/../data/expathrepo/templating-1.1.0/content/templates.xqm]
In function:
    templates:call(item(), element(), map(*)) [137:36:/myPath/exist-distribution-6.2.0/etc/../data/expathrepo/templating-1.1.0/content/templates.xqm]
    templates:process(node()*, map(*)) [148:81:/myPath/exist-distribution-6.2.0/etc/../data/expathrepo/templating-1.1.0/content/templates.xqm]
    templates:process(node()*, map(*)) [148:81:/myPath/exist-distribution-6.2.0/etc/../data/expathrepo/templating-1.1.0/content/templates.xqm]
    templates:process(node()*, map(*)) [148:81:/myPath/exist-distribution-6.2.0/etc/../data/expathrepo/templating-1.1.0/content/templates.xqm]
    templates:process(node()*, map(*)) [148:81:/myPath/exist-distribution-6.2.0/etc/../data/expathrepo/templating-1.1.0/content/templates.xqm]
    templates:process(node()*, map(*)) [148:81:/myPath/exist-distribution-6.2.0/etc/../data/expathrepo/templating-1.1.0/content/templates.xqm]
    templates:process(node()*, map(*)) [148:81:/myPath/exist-distribution-6.2.0/etc/../data/expathrepo/templating-1.1.0/content/templates.xqm]
    templates:process(node()*, map(*)) [148:81:/myPath/exist-distribution-6.2.0/etc/../data/expathrepo/templating-1.1.0/content/templates.xqm]
    templates:process(node()*, map(*)) [148:81:/myPath/exist-distribution-6.2.0/etc/../data/expathrepo/templating-1.1.0/content/templates.xqm]
    templates:process(node()*, map(*)) [435:17:/myPath/exist-distribution-6.2.0/etc/../data/expathrepo/templating-1.1.0/content/templates.xqm]
    templates:process-output(element(), map(*), item()*) [230:9:/myPath/exist-distribution-6.2.0/etc/../data/expathrepo/templating-1.1.0/content/templates.xqm]
    templates:process-output(element(), map(*), item()*, element(function)) [211:9:/myPath/exist-distribution-6.2.0/etc/../data/expathrepo/templating-1.1.0/content/templates.xqm]
    templates:call-by-introspection(element(), map(*), map(*), function(*)) [189:28:/myPath/exist-distribution-6.2.0/etc/../data/expathrepo/templating-1.1.0/content/templates.xqm]
    templates:call(item(), element(), map(*)) [137:36:/myPath/exist-distribution-6.2.0/etc/../data/expathrepo/templating-1.1.0/content/templates.xqm]
    templates:process(node()*, map(*)) [133:51:/myPath/exist-distribution-6.2.0/etc/../data/expathrepo/templating-1.1.0/content/templates.xqm]
    templates:process(node()*, map(*)) [90:9:/myPath/exist-distribution-6.2.0/etc/../data/expathrepo/templating-1.1.0/content/templates.xqm]
    templates:apply(node()+, function(*), map(*)?, map(*)?) [50:5:/myPath/exist-distribution-6.2.0/etc/../data/expathrepo/templating-1.1.0/content/templates.xqm]

Reproduction steps

see attached xar file

Please provide the following

joewiz commented 2 months ago

@dizzzz The error description is correct:

No template function found for call app:test2

Perhaps it could go further in explaining that in order to be a valid "template function", a function referenced in a template must have at least the 2 required parameters: $node as node(), $model as map(*). Or, as the comment in app.xqm states:

(:~
 : This is a sample templating function. It will be called by the templating module if
 : it encounters an HTML element with an attribute: data-template="app:test" or class="app:test" (deprecated).
 : The function has to take 2 default parameters. Additional parameters are automatically mapped to
 : any matching request or function parameter.
 :
 : @param $node the HTML node with the attribute which triggered this call
 : @param $model a map containing arbitrary data - used to pass information between template calls
 :)

... the key phrase here being:

The function has to take 2 default parameters.

... i.e., the 2 @param entries listed in the xqdoc.

So the cause of the error is that an element in index.html, <div data-template="app:test2"/>, causes the templating library to look for a function (a) named app:test2 and (b) bearing the 2 required parameters. When the templating library failed to find such a function, it threw the error. (The app.xqm library does contain a function app:test2, but this function doesn't have the 2 required parameters.

Would a fuller description along these lines have helped you debug the issue? Or did you have something different in mind?

adamretter commented 2 months ago
  1. Perhaps just remove the need for the function to take 2+ arguments. If the user doesn't need the templating context information, it doesn't make sense to force it on them IMHO.

  2. Isn't this part of the error message Max arity of 20 has been exceeded in searching for this template function. If needed, adjust $templates:MAX_ARITY in the templates.xql module. now actually wrong? If I recall we switched from using some proprietary eXist-db function to resolve functions, to using the standard fn:function-lookup (which doesn't have a limit on arity).

joewiz commented 2 months ago

@dizzzz When you have a chance could you add a test that demonstrates the issue?