Rewritten HTMLErrorRenderer w/o asset helper & template

refs: 2af9e2e12

- This new HTMLErrorRenderer is borrowed heavily from finalHandler
   - This is the module that express uses to render errors if there is no custom errorhandler
- It just renders a really simple html page wrapping err.stack in a <pre>
   - This results in a nicely formatted, but unstyled error page
- I also updated BasicErrorRenderer to use the same res.statusCode + err.stack pattern rather than err.message

Note: This error renderer is _only_ used for renderering errors on the `/ghost/` route
 - In almost all cases, errors here are rendered by Ember
 - The only error that can be rendered here is a missing template error see: 2af9e2e12
This commit is contained in:
Hannah Wolfe 2021-11-24 12:32:22 +00:00
parent 2af9e2e125
commit 0799f02e80
No known key found for this signature in database
GPG Key ID: AB586C3B5AE5C037
2 changed files with 24 additions and 27 deletions

View File

@ -241,39 +241,36 @@ _private.ThemeErrorRenderer = (err, req, res, next) => {
});
};
_private.HTMLErrorRenderer = (err, req, res, next) => { // eslint-disable-line no-unused-vars
const data = {
message: err.message,
statusCode: err.statusCode,
errorDetails: err.errorDetails || []
};
/**
* Borrowed heavily from finalHandler
*/
// e.g. if you serve the admin /ghost and Ghost returns a 503 because it generates the urls at the moment.
// This ensures that no matter what res.render will work here
// @TODO: put to prepare error function?
if (_.isEmpty(req.app.engines)) {
res._template = 'error';
req.app.engine('hbs', _private.createHbsEngine());
req.app.set('view engine', 'hbs');
req.app.set('views', config.get('paths').defaultViews);
}
const DOUBLE_SPACE_REGEXP = /\x20{2}/g;
const NEWLINE_REGEXP = /\n/g;
res.render('error', data, (_err, html) => {
if (!_err) {
return res.send(html);
function createHtmlDocument(status, message) {
let body = escapeExpression(message)
.replace(NEWLINE_REGEXP, '<br>')
.replace(DOUBLE_SPACE_REGEXP, ' &nbsp;');
return `<!DOCTYPE html>\n
<html lang="en">\n
<head>\n
<meta charset="utf-8">\n
<title>${status} Error</title>\n
</head>\n
<body>\n
<pre>${status} ${body}</pre>\n
</body>\n
</html>\n`;
}
// re-attach new error e.g. error template has syntax error or misusage
req.err = _err;
// And then try to explain things to the user...
// Cheat and output the error using handlebars escapeExpression
return res.status(500).send(_private.ErrorFallbackMessage(_err));
});
_private.HTMLErrorRenderer = (err, req, res, next) => { // eslint-disable-line no-unused-vars
return res.send(createHtmlDocument(res.statusCode, err.stack));
};
_private.BasicErrorRenderer = (err, req, res, next) => { // eslint-disable-line no-unused-vars
return res.send(res.statusCode + ' ' + err.message);
return res.send(res.statusCode + ' ' + err.stack);
};
errorHandler.resourceNotFound = (req, res, next) => {

View File

@ -48,7 +48,7 @@ describe('Default Frontend routing', function () {
await request.get('/content/images/some/file/that/doesnt-exist.jpg')
.expect('Cache-Control', testUtils.cacheRules.private)
.expect(404)
.expect(/404 Image not found/)
.expect(/Image not found/)
.expect(assertCorrectFrontendHeaders);
});
});