-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
_.template syntax errors too hard to debug #666
Comments
Hi @ianb! You're right, syntax errors from
Hope these help! |
Note that syntax errors are a little different than runtime errors. I should have given an example: There's no successful template object created because just constructing the Function object fails. lodash tried to address this in this commit: lodash/lodash@10886be |
Ah. Yes, of course, though the first point still stands. :)
Why is it helpful to defer throwing the error? It seems to me this would lead to silent template compilation failures. |
lodash's change is not the one I'd have thought of, though in the particular case I encountered it would have been helpful. In cases where you aren't aggressively precompiling templates I think lodash's approach is sensible – deferring the error for a couple lines of code doesn't make it harder to debug. Annotating or rewriting the exception object is more what I'd expect. Also as it currently stands the first point would be helpful if I could get my hands on the syntactically invalid generated code, but right now I can't. |
This way the template and its source are debuggable. Normally, for non-syntax error related failures compiled templates that error when passed a data object, either through
Because errors are deferred until execution rather than creation it allows it to be debugged in the same way you suggested. |
You mean that you can still do |
Yap. |
I see how that's handy, but it poses a problem when precompiling templates. The code below would spit out code with a syntax error without warning, which I think is undesirable. // Logs code with a syntax error.
console.log(_.template('<% syntax error %>').source); |
They would be debugging it because it threw an error in the first place. The warning bells will have already been sounded. |
I meant precompiling server-side before delivering to the browser, where the template would not be executed. |
If they precompile and use the I make heavy use of compiled and inlined templates during the runtime and build process of Lo-Dash. If I or any other dev runs into issues or annoyances with the safeguard I'll simply tweak it. I'm going to stick it out for now. |
This is roughly what I'd propose: https://github.com/ianb/underscore/tree/add-source-to-template-exceptions Documentation here would be important (but I'm not clear where that is), as no one will notice this |
The try/catch around e.source = source is defensive programming – there's nothing I hate more than an exception in my exception handler, and I'm not sure how writable all possible exceptions may be. |
Instead of attaching the garbled source, I've gone ahead and added the original body of the template to the error. |
But I already have the original body, it's the argument I passed in. It's the generated source I have no access to. This change doesn't add any new information. |
Ah, then I misunderstand the original problem. I thought you were having trouble figuring out which template was causing the syntax error. How does the generated source help you debug a template syntaxerror exactly? |
In part it's just another thing to stare at to try to figure out what's syntactically wrong. But I can also drop the generated source into an editor and get syntax feedback there, and I can see what the line numbers map to. As it stands, when I've gotten syntax errors in my templates the only approach I've found to fixing them is to look really hard at the complete template and try to see what I got wrong. |
Alright -- I'm not sure if we should document it, because it seems like a really pessimal debugging technique ... but I also don't see the harm, and I can imagine it being useful when dealing with somebody else's template on deadline ;) |
I only opened the bug because it happened to me, so there is at least anecdotal evidence of this being useful ;) |
If you have a syntax error in your generated template, you just get an exception from
var render = new Function(settings.variable || 'obj', '_', source);
– not very helpful, and you can't view the source of the template.I'm not quite sure what to do, though try/catch around that might help. The exception itself will always be kind of bad. So maybe:
And that would have to be documented, I guess, otherwise you'd never notice that. And I'm not sure if all errors can have attributes added to them. But... something would be helpful.
The text was updated successfully, but these errors were encountered: