简体   繁体   中英

Node.js Express - Handle Error Differently Based on Error Type

A few details... I'm using Express 4, Node 0.12.2 and Express-Handlebars as my view engine.

I'm trying to render a partial via AJAX call and would like to handle different errors types differently. The err object passed on the callback invocation doesn't seem to have much useful info to work with, just an error message. Is there something I'm missing here?

// Search (Partial)
router.get('/search-products', function (req, res, next) {
    res.render('partials/search/products', {layout: 'ajax'}, function(err, html){
        if(err) {
            // return res.status(404).send(err);
            // return res.status(500).send(err);
        }
        res.send(html);
    });
});

That error object isn't going to have a status code because it's an error object generated by the view engine. If that error is populated you can pretty much assume it's a 500 error because the view engine failed to compile a view for some reason.

For example, if you're using Jade, that error object will be populated if you try to have Jade compile a template that has invalid Jade syntax. The error there is never going to be HTTP related which means it's always just a server-side error and status code 500 should suffice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM