<commit_before>var invs = require('../services/invitations');

exports.login = function (req, res, next) {
    var invitationId;

    try {
        invitationId = invs.decipherId(req.params.invitation);
    } catch (ex) {
        delete req.session.invitation;
        return res.send(401);
    }

    req.session.invitation = invitationId;
    res.redirect('/rsvp/');
},

exports.edit = function (req, res, next) {
    var invitationId = req.session.invitation;

    if (!invitationId) {
        return res.render('rsvp/description');
    }

    invs.loadInvitation(invitationId, function (err, invitation) {
        if (err) { return next(err); }

        if (!invitation) {
            return res.send(404, 'Could not find invitation.');
        }

        res.render('rsvp/edit', {
            csrf      : req.session._csrf,
            invitation: invitation
        });
    });
};<commit_msg>Improve `rsvp` route to use error views properly<commit_after>var STATUS_CODES = require('http').STATUS_CODES,

    invs = require('../services/invitations');

exports.login = [invIdFromUrl, loadInvitation, setSessionInvId];
exports.edit  = [invIdFromSession, loadInvitation, renderInvitation];

function invIdFromSession(req, res, next) {
    var invitationId = req.session.invitation;

    if (invitationId) {
        req.invitationId = invitationId;
        next();
    } else {
        return res.render('rsvp/description');
    }
}

function invIdFromUrl(req, res, next) {
    var invitationId;

    try {
        invitationId = invs.decipherId(req.params.invitation);
    } catch (ex) {
        delete req.session.invitation;
        return res.status(401).render('error', {status: STATUS_CODES[401]});
    }

    req.invitationId = invitationId;
    next();
}

function loadInvitation(req, res, next) {
    invs.loadInvitation(req.invitationId, function (err, invitation) {
        if (err) { return next(err); }

        if (!invitation) {
            res.locals.message = 'Could not find invitation.';
            return res.status(404).render('error', {status: STATUS_CODES[404]});
        }

        req.invitation = invitation;
        next();
    });
}

function renderInvitation(req, res) {
    res.render('rsvp/edit', {
        csrf      : req.session._csrf,
        invitation: req.invitation
    });
}

function setSessionInvId(req, res) {
    req.session.invitation = req.invitationId;
    res.redirect('/rsvp/');
}
{'fraction_non_alphanumeric': 0.13574660633484162, 'fraction_numerical': 0.007404360345536816, 'mean_word_length': 2.696048632218845, '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': '5130043', 'n_tokens_mistral': 772, 'n_tokens_neox': 755, 'n_words': 150}