BorisMoore / jsrender

A lightweight, powerful and highly extensible templating engine. In the browser or on Node.js, with or without jQuery.
http://www.jsviews.com
MIT License
2.67k stars 339 forks source link

How to debug JsRender with Node.js, with break points and error messages #351

Closed jsturnio closed 4 years ago

jsturnio commented 4 years ago

Hi, I'm trying to use JsRender with Node to generate static source code, but I found that if I use a HTML page, everything is OK, if I use a tag that is not defined for instance, the error is shown in console.

Now if I use Node with JSRender (installed with: npm install jsrender --save ), then I tried the following example:

var $ = require('jsrender');
$.views.settings.debugMode(true);

function renderVueVar(value) {
   return "{{" + value + "}}";
}

// $.views.tags("mv", renderVueVar); // Provide just a render method

var tmpl = $.templates('Name: {{:name}}<br/> '); 
var html = tmpl.render({name: "Jim"}); // Render

console.log(html);

It works Ok, it prints: Name: Jim

But if I try to use a tag that does not exist, the template line is now: var tmpl = $.templates('Name: {{:name}}<br/> {{tagne}} '); I get the following output:

/home/jose/K/jsrender/wn/node_modules/jsrender/jsrender-node.js:1821
    throw new $sub.Err(message);
    ^
Error
    at /home/jose/K/jsrender/wn/node_modules/jsrender/jsrender-node.js:223:27
    at Object.<anonymous> (/home/jose/K/jsrender/wn/node_modules/jsrender/jsrender-node.js:2879:2)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (/home/jose/K/jsrender/wn/f.jsr:2:9)

How I can get the right error message? I tried also to test an example that use a custom tag, but I can not fix it (because I got the same error message), however, the same example is working fine an HTML I'm using node 10.15.2 and jsrender 1.0.5 Thanks in advance,

BorisMoore commented 4 years ago

I'm sorry I started on a reply to this back in October, but missed that I had not actually posted the reply...

If you launch your script from the command line using >node myscript.js then it will run with no attached debugger and so will not provide full debugging information. You will just get the call stack of any exception that is thrown, as you show above.

You can launch the script with a debugger, and it will pause at the exception, and you can see the error message. For example you can debug in chrome, by navigating to about:inspect and then clicking on "Open dedicated DevTools for Node".

Then you can launch with debugger from your command window:

 > node --inspect-brk myscript.js

If you don't want to run with a debugger, you can still get the full error message by modifying your script file and putting a try catch block around the code for which you want to see the error messages on any exceptions. In the catch block you can write the message back to the console:

var $ = require('jsrender');

try {
    var tmpl = $.templates('Name: {{:name}}<br/> {{tagne/}} ');
    var html = tmpl.render({name: "Jim"}); // Render
}
catch (e) {
    console.log(e)
}

console.log(html);

which will output

{ Error
    ... (call stack)
    at Object.<anonymous> (...mysript.js:1:9)
  name: 'JsRender Error',
  message:
   'Unknown tag: {{tagne}}'
}
BorisMoore commented 4 years ago

@jsturnio - I didn't hear back from you. Did the above suggestions work for you? Closing this issue.

jsturnio commented 4 years ago

Hi Boris, thank you about your clear suggestions!! I tested and work fine and sorry for my late answer.

jsturnio commented 4 years ago

And by the way, your documentation is very nice and clear!