mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05:00
e060a4f811
no issue 🎨 simplify loader - use loadOneTheme for init - use loadOneTheme for init - move updateThemeList to the one place that it is used - this just reduces the surface area of the loader 🎨 Move init up to index temporarily - need to figure out what stuff goes in here as well as loading themes - will move it again later once I've got it figured out 🎨 Reorder & cleanup theme middleware - move the order in blog/app.js so that theme middleware isn't called for shared assets - add comments & cleanup in the middleware itself, for clarity 🎨 Simplify the logic in themes middleware - Separate out config dependent on settings changing and config dependent on request - Move blogApp.set('views') - no reason why this isn't in the theme activation method as it's actually simpler if it is there, we already know the active theme exists & can remove the if-guard 🎨 Improve error handling for missing theme - ensure we display a warning - don't have complex logic for handling errors - move loading of an empty hbs object into the error-handler as this will support more cases 🐛 Fix assetHash clearing bug on theme switch - asset hash wasn't correctly being set on theme switch 🎨 Remove themes.read & test loader instead - Previously, we've simplified loader & improved error handling - We are now able to completely remove theme.read as it's nothing more than a wrapper for package.read - This also means we can change our tests from testing the theme reader to loader
160 lines
4.9 KiB
JavaScript
160 lines
4.9 KiB
JavaScript
var _ = require('lodash'),
|
|
path = require('path'),
|
|
hbs = require('express-hbs'),
|
|
config = require('../config'),
|
|
errors = require('../errors'),
|
|
i18n = require('../i18n'),
|
|
_private = {},
|
|
errorHandler = {};
|
|
|
|
/**
|
|
* 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) {
|
|
// @TODO re-implement custom error templates see #8079
|
|
var defaultTemplate = path.resolve(config.get('paths').adminViews, 'user-error.hbs'),
|
|
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', hbs.express3());
|
|
}
|
|
|
|
res.render(defaultTemplate, 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>' + hbs.handlebars.Utils.escapeExpression(err.message || err) + '</pre>' +
|
|
'<br ><p>' + i18n.t('errors.errors.whilstTryingToRender') + '</p>' +
|
|
err.statusCode + ' ' + '<pre>' + hbs.handlebars.Utils.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;
|