Ghost/core/server/apps/subscribers/index.js
Katharina Irrgang 1882278b5b 🎨 configurable logging with bunyan (#7431)
- 🛠  add bunyan and prettyjson, remove morgan

-   add logging module
  - GhostLogger class that handles setup of bunyan
  - PrettyStream for stdout

-   config for logging
  - @TODO: testing level fatal?

-   log each request via GhostLogger (express middleware)
  - @TODO: add errors to output

- 🔥  remove errors.updateActiveTheme
  - we can read the value from config

- 🔥  remove 15 helper functions in core/server/errors/index.js
  - all these functions get replaced by modules:
    1. logging
    2. error middleware handling for html/json
    3. error creation (which will be part of PR #7477)

-   add express error handler for html/json
  - one true error handler for express responses
  - contains still some TODO's, but they are not high priority for first implementation/integration
  - this middleware only takes responsibility of either rendering html responses or return json error responses

- 🎨  use new express error handler in middleware/index
  - 404 and 500 handling

- 🎨  return error instead of error message in permissions/index.js
  - the rule for error handling should be: if you call a unit, this unit should return a custom Ghost error

- 🎨  wrap serve static module
  - rule: if you call a module/unit, you should always wrap this error
  - it's always the same rule
  - so the caller never has to worry about what comes back
  - it's always a clear error instance
  - in this case: we return our notfounderror if serve static does not find the resource
  - this avoid having checks everywhere

- 🎨  replace usages of errors/index.js functions and adapt tests
  - use logging.error, logging.warn
  - make tests green
  - remove some usages of logging and throwing api errors -> because when a request is involved, logging happens automatically

- 🐛  return errorDetails to Ghost-Admin
  - errorDetails is used for Theme error handling

- 🎨  use 500er error for theme is missing error in theme-handler

- 🎨  extend file rotation to 1w
2016-10-04 16:33:43 +01:00

84 lines
3.2 KiB
JavaScript

var _ = require('lodash'),
path = require('path'),
hbs = require('express-hbs'),
router = require('./lib/router'),
// Dirty requires
config = require('../../config'),
logging = require('../../logging'),
i18n = require('../../i18n'),
labs = require('../../utils/labs'),
template = require('../../helpers/template'),
utils = require('../../helpers/utils'),
globalUtils = require('../../utils'),
params = ['error', 'success', 'email'],
/**
* This helper script sets the referrer and current location if not existent.
*
* document.querySelector['.location']['value'] = document.querySelector('.location')['value'] || window.location.href;
*/
subscribeScript =
'<script type="text/javascript">' +
'(function(g,h,o,s,t){' +
'h[o](\'.location\')[s]=h[o](\'.location\')[s] || g.location.href;' +
'h[o](\'.referrer\')[s]=h[o](\'.referrer\')[s] || h.referrer;' +
'})(window,document,\'querySelector\',\'value\');' +
'</script>';
function makeHidden(name, extras) {
return utils.inputTemplate({
type: 'hidden',
name: name,
className: name,
extras: extras
});
}
function subscribeFormHelper(options) {
var root = options.data.root,
data = _.merge({}, options.hash, _.pick(root, params), {
action: path.join('/', globalUtils.url.getSubdir(), config.get('routeKeywords').subscribe, '/'),
script: new hbs.handlebars.SafeString(subscribeScript),
hidden: new hbs.handlebars.SafeString(
makeHidden('confirm') +
makeHidden('location', root.subscribed_url ? 'value=' + root.subscribed_url : '') +
makeHidden('referrer', root.subscribed_referrer ? 'value=' + root.subscribed_referrer : '')
)
});
return template.execute('subscribe_form', data, options);
}
module.exports = {
activate: function activate(ghost) {
var err;
// Correct way to register a helper from an app
ghost.helpers.register('subscribe_form', function labsEnabledHelper() {
if (labs.isSet('subscribers') === true) {
return subscribeFormHelper.apply(this, arguments);
}
err = new Error();
err.message = i18n.t('warnings.helpers.helperNotAvailable', {helperName: 'subscribe_form'});
err.context = i18n.t('warnings.helpers.apiMustBeEnabled', {helperName: 'subscribe_form', flagName: 'subscribers'});
err.help = i18n.t('warnings.helpers.seeLink', {url: 'http://support.ghost.org/subscribers-beta/'});
logging.error(err);
return new hbs.handlebars.SafeString('<script>console.error(' + JSON.stringify(err) + ');</script>');
});
},
setupRoutes: function setupRoutes(blogRouter) {
blogRouter.use('/' + config.get('routeKeywords').subscribe + '/', function labsEnabledRouter(req, res, next) {
if (labs.isSet('subscribers') === true) {
return router.apply(this, arguments);
}
next();
});
}
};