0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00
ghost/core/server/apps/subscribers/lib/router.js
Katharina Irrgang d81bc91bd2 Error creation (#7477)
refs #7116, refs #2001

- Changes the way Ghost errors are implemented to benefit from proper inheritance
- Moves all error definitions into a single file
- Changes the error constructor to take an options object, rather than needing the arguments to be passed in the correct order.
- Provides a wrapper so that any errors that haven't already been converted to GhostErrors get converted before they are displayed.

Summary of changes:

* 🐛  set NODE_ENV in config handler
*   add GhostError implementation (core/server/errors.js)
  - register all errors in one file
  - inheritance from GhostError
  - option pattern
* 🔥  remove all error files
*   wrap all errors into GhostError in case of HTTP
* 🎨  adaptions
  - option pattern for errors
  - use GhostError when needed
* 🎨  revert debug deletion and add TODO for error id's
2016-10-06 13:27:35 +01:00

105 lines
2.8 KiB
JavaScript

var path = require('path'),
express = require('express'),
_ = require('lodash'),
subscribeRouter = express.Router(),
// Dirty requires
api = require('../../../api'),
errors = require('../../../errors'),
templates = require('../../../controllers/frontend/templates'),
postlookup = require('../../../controllers/frontend/post-lookup'),
setResponseContext = require('../../../controllers/frontend/context');
function controller(req, res) {
var defaultView = path.resolve(__dirname, 'views', 'subscribe.hbs'),
paths = templates.getActiveThemePaths(req.app.get('activeTheme')),
data = req.body;
setResponseContext(req, res);
if (paths.hasOwnProperty('subscribe.hbs')) {
return res.render('subscribe', data);
} else {
return res.render(defaultView, data);
}
}
function errorHandler(error, req, res, next) {
/*jshint unused:false */
if (error.statusCode !== 404) {
res.locals.error = error;
return controller(req, res);
}
next(error);
}
function honeyPot(req, res, next) {
if (!req.body.hasOwnProperty('confirm') || req.body.confirm !== '') {
return next(new Error('Oops, something went wrong!'));
}
// we don't need this anymore
delete req.body.confirm;
next();
}
function handleSource(req, res, next) {
req.body.subscribed_url = req.body.location;
req.body.subscribed_referrer = req.body.referrer;
delete req.body.location;
delete req.body.referrer;
postlookup(req.body.subscribed_url)
.then(function (result) {
if (result && result.post) {
req.body.post_id = result.post.id;
}
next();
})
.catch(function (err) {
if (err instanceof errors.NotFoundError) {
return next();
}
next(err);
});
}
function storeSubscriber(req, res, next) {
req.body.status = 'subscribed';
if (_.isEmpty(req.body.email)) {
return next(new errors.ValidationError({message: 'Email cannot be blank.'}));
}
return api.subscribers.add({subscribers: [req.body]}, {context: {external: true}})
.then(function () {
res.locals.success = true;
next();
})
.catch(function () {
// we do not expose any information
res.locals.success = true;
next();
});
}
// subscribe frontend route
subscribeRouter.route('/')
.get(
controller
)
.post(
honeyPot,
handleSource,
storeSubscriber,
controller
);
// configure an error handler just for subscribe problems
subscribeRouter.use(errorHandler);
module.exports = subscribeRouter;
module.exports.controller = controller;