adobe / htlengine

An HTL (Sightly) Interpreter/Compiler for Node.js
Apache License 2.0
47 stars 20 forks source link

Improve error logs to help locating the issue in the HTL template #16

Open kptdobe opened 6 years ago

kptdobe commented 6 years ago

When there is something wrong in the htl template (like trying to access it.dummy.property which does not exist for exampl), the error log you get in the whole Petridish setup (and probably the same in the production setup) is something like:

[hlx] error: Error while rendering the resource: TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)
    at lengthOf (../project-helix/modules/helix-helpx/.hlx/build/html.js:295:53)
    at ../project-helix/modules/helix-helpx/.hlx/build/html.js:442:28
    at Generator.next (<anonymous>)
    at onFulfilled (../project-helix/modules/helix-helpx/node_modules/co/index.js:65:19)
    at ../project-helix/modules/helix-helpx/node_modules/co/index.js:54:5
    at new Promise (<anonymous>)
    at co (../project-helix/modules/helix-helpx/node_modules/co/index.js:50:10)
    at Runtime.run (../project-helix/modules/helix-helpx/node_modules/@adobe/htlengine/src/runtime/Runtime.js:47:12)
    at run (../project-helix/modules/helix-helpx/.hlx/build/html.js:305:20)
    at main (../project-helix/modules/helix-helpx/.hlx/build/html.js:482:12)
    at getContextPath.catch.then (../project-helix/modules/helix-helpx/.hlx/build/html.js:240:19)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:182:7)

Which gives not hint on what is wrong in the template. It is crucial for development to output a meaningful error that helps locating the issue.

trieloff commented 6 years ago

I think there are two issues here:

  1. htlengine should handle missing properties more gracefully
  2. htlengine could really benefit from getting access to the logger and making use of it
ThaNarie commented 5 years ago

The error from the fist comment is from: https://github.com/adobe/htlengine/blob/bb6475fd6438c00e488026e77aacc0a8d3ec990a/src/compiler/JSRuntimeTemplate.js#L18

Adding something like if (!c) return 0; would already nicely address the issue. (what I've done by using withCodeTemplate and a custom template with that fix). But some more proper checks would be better.

Is this Issue also related to parser errors (since the points in the above comments talk about the rendering phase, although the Title is not specific about them) ?

Currently parsing errors throw an error: https://github.com/adobe/htlengine/blob/bb6475fd6438c00e488026e77aacc0a8d3ec990a/src/parser/htl/ThrowingErrorListener.js#L18

However, there is not a lot of context (the line numbers are from the input string, not the complete file), so finding the issue based on the outputted characters is very hard.

line 1:10 token recognition error at: '/'

Adding console.log(offendingSymbol.source[1].strdata) to the output before throwing would at least show the parser input, and using the line and column info you could isolate the correct line, and add ^^^^ characters to point to the right column:

${foo.bar</p>
          ^

And/or include some more information in the Error that is thrown, so others could do something similar.

I hope this helps! :)