Giter Club home page Giter Club logo

Comments (5)

BorisMoore avatar BorisMoore commented on August 17, 2024

Thanks for calling this out, Johan. I agree the error message is not ideal, and that the additional information would be helpful, but unfortunately it looks like a fix is difficult to achieve. If it were possible at all it would imply significant changes and additions to the the template parsing code, with corresponding cost in perf, code size, and potentially stability of current code.

So my current take is that this is a 'won't fix`.

In fact the parsed code is view.ctxPrm("unknown")() and the browser throws an error. For Chrome it is view.ctxPrm(...) is not a function. For Safari it is undefined is not a function... It would be nice if the browser gave the message view.ctxPrm("unknown") is not a function. But it doesn't, and we have no control over that...

from jsviews.

johan-ohrn avatar johan-ohrn commented on August 17, 2024

What I'm discovering is that I come back to a month old code or more and change something, perhaps a web api or something in a viewmodel which result in a template all of a sudden being linked against a new data structure. Bang I'm getting this kind of error. At best of times it's easy to find out which template expression is the culprit but it happens all to often that I have to remove a lot of template code and by process of elimination locate the template expression that is causing the error.

Perhaps a best effort would be enough. I'm noticing that the function buildCode(ast, tmpl, isLinkExpr) is passed tmpl which contains the expression tmpl.markup.
At the end of the function it builds up the dynamic javascript in the code var such as this:

// unnamed
var v,ret=""
+((v=view.ctxPrm("unknown")())!=null?v:"");
return ret;

What if instead the dynamic javascript was generated something like this instead?

// unnamed
try {
  var v,ret=""
  +((v=view.ctxPrm("unknown")())!=null?v:"");
  return ret;
} catch (err) {
  err.message = "template: '" + tmplName + "', markup: '" + tmpl.markup + "' threw the following " + err.message;
  throw err;
}

Instead of hijacking the error it might be better to throw a custom error. I seem to recall there being a template parser error..

This shouldn't incur any performance penalty, code size should be small and it would at least print out the template expression I should be looking out for.

from jsviews.

BorisMoore avatar BorisMoore commented on August 17, 2024

Ah - well if you want to include a try catch, I believe there is a perf cost, but you can do it with existing features. Just do the following, as an opt-in choice:

$.views.settings.debugMode(myOnErrorHandler);

function myOnErrorHandler(err, fallback, view) {
   throw "template: '" + view.tmpl.tmplName + "', markup: '" + view.tmpl.markup + "' threw the following " + err.message;
}

(It is opt-in because of the trade-off perf/debugging information)

See https://www.jsviews.com/#onerror@debugmode-function.

You can also set onError on the tag, to pass in anything you want via the fallback parameter of your handler. It can be a string, an object or a function, such as the template object:

{{:~unknown() onError=#tmpl}}
function myOnErrorHandler(err, template, view) {
   throw "template: '" + template.tmplName + "', markup: '" + template.markup + "' threw the following " + err.message;
}

from jsviews.

johan-ohrn avatar johan-ohrn commented on August 17, 2024

That first option does what I need. Thanks.

from jsviews.

BorisMoore avatar BorisMoore commented on August 17, 2024

Closing, since the requested feature is already available through existing feature: https://www.jsviews.com/#onerror@debugmode-function.

from jsviews.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.