You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
i have unpredictable data objects going into ejs and it seems if its missing a var it throws a "path":"" error and fails to render the template. is there a way to get ejs to ignore errors? as its a template system i would prefer it just to output nothing if a variable doesnt exist and move on with the rest of the template. not to die and cancel rendering the entire template.
it would also be useful if it could output something more descriptive than "path":"" :)
Our use case: I'm evaluating whether we can use EJS for an app that allows users to enter custom templates. For example, users can configure the system to send emails, and they should be able to enter a template for the email's content. Users make mistakes and we need to expect users to enter invalid templates. The entire template should not fail when a single expression in a <%= ... %> is invalid. Syntax errors can be checked at compile time, but semantic errors cannot be:
Hello <%= usr.gender === 'female' ? 'Mrs' : 'Mr' %] <% user.firstName %> <%= user.lastName %>
...content of the email...
The form of address will fail at runtime (usr instead of user), and currently, this will result in the entire template failing. But we need the app to be more resilient to these errors, the form of address may be missing, but it's still very useful to send the email with the remainder of the content. We may need to log an error and tell the user to fix the template, but a single mistake should not completely break the template.
Not sure what the best solution here is. I took a quick look at what could be done, and one thing I realized is that catching errors is possible only for expression (e.g. <%= ... %>), not for statements. Statements can be partial JS code, such as <% if (...) { %> more stuff in between <% } %>. Automatically wrapping these in a try-catch may be possible, but not really feasible, as you'd have to parse the code (and would you catch all errors, or only parts etc.?)
Perhaps you could introduce an option like errorFn. When set, EJS automatically catches errors from expressions and calls the errorFn with the error? Could be used like this:
constcompiled=ejs.compile("...",{errorFn: e=>{console.error("Unhandled error during template evaluation",e);// Insert this value into the template insteadreturn"ERROR!"},});
Then a template like [% data.valid %] -- [%= data.invalid.bar %] would render to ValidData -- ERROR!.
A quick hacky diff I used to test whether this idea could even work:
Activity
blutorange commentedon Nov 27, 2021
I am going to second this.
Our use case: I'm evaluating whether we can use EJS for an app that allows users to enter custom templates. For example, users can configure the system to send emails, and they should be able to enter a template for the email's content. Users make mistakes and we need to expect users to enter invalid templates. The entire template should not fail when a single expression in a
<%= ... %>is invalid. Syntax errors can be checked at compile time, but semantic errors cannot be:The form of address will fail at runtime (
usrinstead ofuser), and currently, this will result in the entire template failing. But we need the app to be more resilient to these errors, the form of address may be missing, but it's still very useful to send the email with the remainder of the content. We may need to log an error and tell the user to fix the template, but a single mistake should not completely break the template.Not sure what the best solution here is. I took a quick look at what could be done, and one thing I realized is that catching errors is possible only for expression (e.g.
<%= ... %>), not for statements. Statements can be partial JS code, such as<% if (...) { %> more stuff in between <% } %>. Automatically wrapping these in a try-catch may be possible, but not really feasible, as you'd have to parse the code (and would you catch all errors, or only parts etc.?)Perhaps you could introduce an option like
errorFn. When set, EJS automatically catches errors from expressions and calls theerrorFnwith the error? Could be used like this:Then a template like
[% data.valid %] -- [%= data.invalid.bar %]would render toValidData -- ERROR!.A quick hacky diff I used to test whether this idea could even work:
mde commentedon Nov 27, 2021
I would definitely consider a PR that implements this.
Implements mde#575 Better error handling via the new options 'error' …
mde#575 Add case for error option to benchmarkRender
Implements mde#575 Better error handling via the new options 'error' …
mde#575 Add case for error option to benchmarkRender
mde commentedon Jan 17, 2026
This issue has been closed as outdated.
If you believe this issue is still relevant, please:
Thank you for your contribution!