Ghost/core/server/middleware/error-handler.js
Hannah Wolfe a413d70313 Asset amends (#8294)
refs #8221

🔥 Remove ghost=true concept from asset url helper

 💯 Introduce CSS minification with cssnano
- add new grunt-cssnano dependency
- wire up grunt task to minify public/ghost.css

🎨 Rename minification config & hash params
- Change minifyInProduction -> hasMinFile
  - this means this asset should have a .min file available
- Change minifyAssets -> useMinFiles
  - this means that in this env we want to serve .min files if available

🎨 Update public/ghost.css to serve .min for prod
- add the new `hasMinFile` property

🎨 Move minified asset handling to asset_url util
- this logic should be in the util, not the asset helper
- updated tests

📖 Error handler always needs asset helper
- this removes the TODO and adds a more sensible comment
- we also need to update our theme documentation around error templates

🔥 Don't use asset helper in ghost head
- use getAssetUrl util instead!
- removed TODO

📖 Update proxy docs
🎨 Simplify asset helper & add tests
- this refactor is a step prior to moving this from metadata to being a url util
- needed to skip some new tests

🐛 Add missing handler for css file
2017-04-10 11:30:21 +02:00

171 lines
5.2 KiB
JavaScript

var _ = require('lodash'),
hbs = require('express-hbs'),
config = require('../config'),
errors = require('../errors'),
i18n = require('../i18n'),
templates = require('../controllers/frontend/templates'),
escapeExpression = hbs.Utils.escapeExpression,
_private = {},
errorHandler = {};
/**
* This is a bare minimum setup, which allows us to render the error page
* It uses the {{asset}} helper, and nothing more
*/
_private.createHbsEngine = function createHbsEngine() {
var engine = hbs.create();
engine.registerHelper('asset', require('../helpers/asset'));
return engine.express4();
};
/**
* This function splits the stack into pieces, that are then rendered using the following handlebars code:
* ```
* {{#each stack}}
* <li>
* at
* {{#if function}}<em class="error-stack-function">{{function}}</em>{{/if}}
* <span class="error-stack-file">({{at}})</span>
* </li>
* {{/each}}
* ```
* @TODO revisit whether this is useful as part of #7491
*/
_private.parseStack = function parseStack(stack) {
if (!_.isString(stack)) {
return stack;
}
var stackRegex = /\s*at\s*(\w+)?\s*\(([^\)]+)\)\s*/i;
return (
stack
.split(/[\r\n]+/)
.slice(1)
.map(function (line) {
var parts = line.match(stackRegex);
if (!parts) {
return null;
}
return {
function: parts[1],
at: parts[2]
};
})
.filter(function (line) {
return !!line;
})
);
};
/**
* Get an error ready to be shown the the user
*
* @TODO: support multiple errors within one single error, see https://github.com/TryGhost/Ghost/issues/7116#issuecomment-252231809
*/
_private.prepareError = function prepareError(err, req, res, next) {
if (_.isArray(err)) {
err = err[0];
}
if (!errors.utils.isIgnitionError(err)) {
// We need a special case for 404 errors
// @TODO look at adding this to the GhostError class
if (err.statusCode && err.statusCode === 404) {
err = new errors.NotFoundError({
err: err
});
} else {
err = new errors.GhostError({
err: err,
statusCode: err.statusCode
});
}
}
// used for express logging middleware see core/server/app.js
req.err = err;
// alternative for res.status();
res.statusCode = err.statusCode;
// never cache errors
res.set({
'Cache-Control': 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'
});
next(err);
};
_private.JSONErrorRenderer = function JSONErrorRenderer(err, req, res, /*jshint unused:false */ next) {
// @TODO: jsonapi errors format (http://jsonapi.org/format/#error-objects)
res.json({
errors: [{
message: err.message,
errorType: err.errorType,
errorDetails: err.errorDetails
}]
});
};
_private.HTMLErrorRenderer = function HTMLErrorRender(err, req, res, /*jshint unused:false */ next) {
var templateData = {
message: err.message,
code: err.statusCode
};
if (err.statusCode === 500 && config.get('printErrorStack')) {
templateData.stack = err.stack;
}
// It can be that something went wrong with the theme or otherwise loading handlebars
// This ensures that no matter what res.render will work here
if (_.isEmpty(req.app.engines)) {
req.app.engine('hbs', _private.createHbsEngine());
}
res.render(templates.error(err.statusCode), templateData, function renderResponse(err, html) {
if (!err) {
return res.send(html);
}
// And then try to explain things to the user...
// Cheat and output the error using handlebars escapeExpression
return res.status(500).send(
'<h1>' + i18n.t('errors.errors.oopsErrorTemplateHasError') + '</h1>' +
'<p>' + i18n.t('errors.errors.encounteredError') + '</p>' +
'<pre>' + escapeExpression(err.message || err) + '</pre>' +
'<br ><p>' + i18n.t('errors.errors.whilstTryingToRender') + '</p>' +
err.statusCode + ' ' + '<pre>' + escapeExpression(err.message || err) + '</pre>'
);
});
};
errorHandler.resourceNotFound = function resourceNotFound(req, res, next) {
// TODO, handle unknown resources & methods differently, so that we can also produce
// 405 Method Not Allowed
next(new errors.NotFoundError({message: i18n.t('errors.errors.resourceNotFound')}));
};
errorHandler.pageNotFound = function pageNotFound(req, res, next) {
next(new errors.NotFoundError({message: i18n.t('errors.errors.pageNotFound')}));
};
errorHandler.handleJSONResponse = [
// Make sure the error can be served
_private.prepareError,
// Render the error using JSON format
_private.JSONErrorRenderer
];
errorHandler.handleHTMLResponse = [
// Make sure the error can be served
_private.prepareError,
// Render the error using HTML format
_private.HTMLErrorRenderer
];
module.exports = errorHandler;