Skip to content
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

TwigException for invalid syntax crashes process, no way to catch error???? #705

Open
r3wt opened this issue Dec 24, 2019 · 6 comments
Open

Comments

@r3wt
Copy link

r3wt commented Dec 24, 2019

trying to render preview of email template, but i noticed anytime there was a twig syntax error process crashes.

so i created a template with invalid syntax and set out to try and capture the invalid syntax exception and handle the error appropriately, but it seems this is not possible for some reason?

my code is:

const fs = require('fs');
const Twig = require('twig');
Twig.cache(false);

function emailPreview(req,res){
    log('request for email template at /email/%s',req.params.template);
    var tpl = utils.basepath(config.mail.templatePath +'/'+ req.params.template);
    if(!utils.ext(tpl)){
        tpl +='.twig';
    }
    fs.access(tpl,(err)=>{
        if(err){
            return res.err(new Error('the template specified does not exist.'));
        }
        try{
            Twig.renderFile(tpl,config.mail.templateArguments,(err,html)=>{
                if(err){
                    log.error(err);
                    return res.err(err);
                }
                res.html(html);
            });    
        }
        catch(e){
            log.error(e);
            res.err(e);
        }    
    })
}
{# this template exists to test error handling for email preview #}
<html>
    <head>
        {# syntax on the following line is invalid #}
        <title>{{title||'test'}}</title>
    </head>
    <body>Test</body>
</html>

It seems no matter what i do, i am unable to capture the error and the process exits with error code. Please advise

williamdes added a commit to wdes/changelog that referenced this issue Jan 26, 2020
@williamdes
Copy link

Same here
https://github.com/wdes/changelog/blob/e5cee6a17041b614c5e7f633303fcd2d7c9c78a1/src/changelog.js#L123

err is null and no way to catch the error..
blocking me for 100% coverage 👎

Here is an example: wdes/changelog@e5c6ee2

cc @RobLoach

@rafipiccolo
Copy link

rafipiccolo commented Sep 19, 2021

i openned this file : node_modules/twig/twig.js
and it looks like it should be this :
throw new Twig.Error
everywhere it is (2 places) :
throw Twig.Error

Replacing it does throw a valid error now instead of crashing the process

but since the project is dead, how do we get this corrected in the package without hacking the node_modules folder ? :)

sample test file

const Twig = require('twig');

Twig.renderFile('./views/testtwig.html.twig', {foo:'bar'}, (err, html) => {
  console.log(html);
});

process.on('uncaughtException', function(err) {
    console.log('err', err)
})

@willrowe
Copy link
Collaborator

willrowe commented Aug 1, 2022

Does it work if you set rethrow to true in the app twig options?

@rafipiccolo
Copy link

rafipiccolo commented Aug 12, 2022

it changes behavior in the sens it emits an uncaughtException with this version (syntax is crazy :))

import Twig from "twig";

try {
    Twig.renderFile('./views/shit.html.twig', { settings: { 'twig options': {rethrow: true} }, foo: 'bar' }, (err, html) => {
        // no err !!!
        console.log(html);
    });
} catch(err) {
    // not catched neither !!!
}

process.on('uncaughtException', function (err) {
    // but it gets here. hence still causing an app reboot
    console.log('uncaughtException', err)
})

This other version results in a local catch (way better IMHO)

try {
    var template = Twig.twig({
        rethrow: true,
       data: `{# this template exists to test error handling for email preview #}
        <html>
           <head>
               {# syntax on the following line is invalid #}
                <title>{{title||'test'}}</title>
            </head>
            <body>Test</body>
        </html>`,
   }).render();
    console.log('template', template);
} catch(err) {
   // catched here !!!
   console.log('catched', err);
}

process.on('uncaughtException', function (err) {
    // nothing here
    console.log('uncaughtException', err)
})

with express it also results in uncaughtexception whatever i try. which is very bad.

    app.set('twig options', {
        rethrow: true,
    });

    router.get('/test', async (req, res, next) => {
        try {
            // causes a uncaughtexception
            res.render('test');

           // same
           res.render('shit', function(err, data) {
              // no err here !!!!
              res.send(data);
           });
        } catch (err) {
            res.send('catched'+err.message)
        }
    });

@willrowe
Copy link
Collaborator

Thanks for the additional details, this appears to be an issue with the Express specific render functions.

@williamdes
Copy link

Same here https://github.com/wdes/changelog/blob/e5cee6a17041b614c5e7f633303fcd2d7c9c78a1/src/changelog.js#L123

Here is an example: wdes/changelog@e5c6ee2

This is still a problem for me, I can not catch the error. That said with rethrow it's better but I still can not catch it nicely.
This does "work": process.on('uncaughtException'

See my commit where I added an unit test and rethrow: wdes/changelog@317ded3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants