Skip to content

Commit

Permalink
πŸ› Fixed 500 errors for invalid theme layouts (TryGhost#19848)
Browse files Browse the repository at this point in the history
ref ENG-742
ref https://linear.app/tryghost/issue/ENG-742

We don't do any parsing of layouts in gscan, which means themes can be
uploaded which use non-existent files for their layout.

We can catch the error in the res.render call, and wrap it, just like we
do for missing templates (e.g. the StaticRoutesRouter)
  • Loading branch information
allouis authored Mar 14, 2024
1 parent f16d980 commit aaa19a5
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion ghost/core/core/frontend/services/rendering/renderer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
const path = require('path');
const debug = require('@tryghost/debug')('services:routing:renderer:renderer');
const {IncorrectUsageError} = require('@tryghost/errors');
const setContext = require('./context');
const templates = require('./templates');
const tpl = require('@tryghost/tpl');
const messages = {
couldNotReadFile: 'Could not read file {file}'
};

/**
* @description Helper function to finally render the data.
Expand All @@ -26,5 +32,17 @@ module.exports = function renderer(req, res, data) {
}

// Render Call
res.render(res._template, data);
res.render(res._template, data, function (err, html) {
if (err) {
if (err.code === 'ENOENT') {
return req.next(
new IncorrectUsageError({
message: tpl(messages.couldNotReadFile, {file: path.basename(err.path)})
})
);
}
req.next(err);
}
res.send(html);
});
};

0 comments on commit aaa19a5

Please sign in to comment.