<commit_before>var domain = require('domain');

module.exports = function (app, templateVarService, appUtil) {
    return {
        setup: function () {
            app.use(function (req, res) {
                templateVarService.setupLocals(req, res);
                res.status(404).render('not-found');
            });
            app.use(function (err, req, res, next) {
                templateVarService.setupLocals(req, res);
                if (err instanceof appUtil.ValidationError) {
                    res.status(403).json(err.fieldNameToMessage);
                } else if (err) {
                    console.error(err.stack);
                    res.locals.error = err;
                    if (req.url.indexOf('/api/') === 0) {
                        res.status(500).send(err.stack.toString());
                    } else {
                        res.status(500).render('error');
                    }
                }
            })
        }
    };
};<commit_msg>Fix handling of errors that are not the Error<commit_after>var domain = require('domain');
var _ = require('lodash');

module.exports = function (app, templateVarService, appUtil) {
    return {
        setup: function () {
            app.use(function (req, res) {
                templateVarService.setupLocals(req, res);
                res.status(404).render('not-found');
            });
            app.use(function (err, req, res, next) {
                templateVarService.setupLocals(req, res);
                if (err instanceof appUtil.ValidationError) {
                    res.status(403).json(err.fieldNameToMessage);
                } else if (err) {
                    console.error(err.stack);
                    res.locals.error = err;
                    if (req.url.indexOf('/api/') === 0) {
                        if (_.isError(err)) {
                            res.status(500).send(err.stack.toString());
                        } else {
                            res.status(500).send(err);
                        }
                    } else {
                        res.status(500).render('error');
                    }
                }
            })
        }
    };
};
{'fraction_non_alphanumeric': 0.1269986295111923, 'fraction_numerical': 0.013248058474189127, 'mean_word_length': 1.2346938775510203, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6121553', 'n_tokens_mistral': 594, 'n_tokens_neox': 542, 'n_words': 96}