javascript - Detect handlebars.js parsing/syntax errors. (client-side templating) -


i'm creating client-side handlebar's template editor. half of screen filled text editor , other half preview window. content text editor gets logged preview window after has been parsed handlebars.js, applying variables in process.

whenever make syntax error using handlebar's expression (like typing {name}} instead of {{name}}), console message appears in devtools. these messages might this:

parse error on line 4: ...m"><p>hola, {{name}</p></div></body> ---------------------^ expecting 'close_raw_block', 'close', 'close_unescaped', 'open_sexpr', 'close_sexpr', 'id', 'open_block_params', 'string', 'number', 'boolean', 'undefined', 'null', 'data', 'sep', got 'invalid' 

i want able detect whether errors fire or not, , display error message in order warn user. i've tried unsuccesfully rewrite javascript error() class, throws kind of errors , handlebars crashes. there way access these errors?

you catch error , display it. this:

var template = handlebars.compile('<p>hola, {{name}</p>');  try {    template();  } catch (error) {    var encodederror = error.message.replace(/[\u00a0-\u9999<>\&]/gim, function(i) {      return '&#' + i.charcodeat(0) + ';';    });    document.write('<pre>' + encodederror + '</pre>');  }
<script src="http://builds.handlebarsjs.com.s3.amazonaws.com/handlebars-v4.0.5.js"></script>

(btw, here's borrowed html entity encoding from.)


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -