mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05:00
Fixed error handling for missing admin templates
- If the admin templates default.html or default-prod.html are missing, don't throw a 500 - Instead throw a well considered 400 error with extra help for what to do to fix it
This commit is contained in:
parent
0c4c9ce553
commit
2af9e2e125
1 changed files with 35 additions and 12 deletions
|
@ -1,10 +1,20 @@
|
||||||
const debug = require('@tryghost/debug')('web:admin:controller');
|
const debug = require('@tryghost/debug')('web:admin:controller');
|
||||||
|
const errors = require('@tryghost/errors');
|
||||||
|
const tpl = require('@tryghost/tpl');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
const config = require('../../../shared/config');
|
const config = require('../../../shared/config');
|
||||||
const updateCheck = require('../../update-check');
|
const updateCheck = require('../../update-check');
|
||||||
|
|
||||||
|
const messages = {
|
||||||
|
templateError: {
|
||||||
|
message: 'Unable to find admin template file {templatePath}',
|
||||||
|
context: 'These template files are generated as part of the build process',
|
||||||
|
help: 'Please see {link}'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description Admin controller to handle /ghost/ requests.
|
* @description Admin controller to handle /ghost/ requests.
|
||||||
*
|
*
|
||||||
|
@ -23,18 +33,31 @@ module.exports = function adminController(req, res) {
|
||||||
const templatePath = path.resolve(config.get('paths').adminViews, defaultTemplate);
|
const templatePath = path.resolve(config.get('paths').adminViews, defaultTemplate);
|
||||||
const headers = {};
|
const headers = {};
|
||||||
|
|
||||||
// Generate our own ETag header
|
try {
|
||||||
// `sendFile` by default uses filesize+lastmod date to generate an etag.
|
// Generate our own ETag header
|
||||||
// That doesn't work for admin templates because the filesize doesn't change between versions
|
// `sendFile` by default uses filesize+lastmod date to generate an etag.
|
||||||
// and `npm pack` sets a fixed lastmod date for every file meaning the default etag never changes
|
// That doesn't work for admin templates because the filesize doesn't change between versions
|
||||||
const fileBuffer = fs.readFileSync(templatePath);
|
// and `npm pack` sets a fixed lastmod date for every file meaning the default etag never changes
|
||||||
const hashSum = crypto.createHash('md5');
|
const fileBuffer = fs.readFileSync(templatePath);
|
||||||
hashSum.update(fileBuffer);
|
const hashSum = crypto.createHash('md5');
|
||||||
headers.ETag = hashSum.digest('hex');
|
hashSum.update(fileBuffer);
|
||||||
|
headers.ETag = hashSum.digest('hex');
|
||||||
|
|
||||||
if (config.get('adminFrameProtection')) {
|
if (config.get('adminFrameProtection')) {
|
||||||
headers['X-Frame-Options'] = 'sameorigin';
|
headers['X-Frame-Options'] = 'sameorigin';
|
||||||
|
}
|
||||||
|
|
||||||
|
res.sendFile(templatePath, {headers});
|
||||||
|
} catch (error) {
|
||||||
|
if (error.code === 'ENOENT') {
|
||||||
|
throw new errors.IncorrectUsageError({
|
||||||
|
message: tpl(messages.templateError.message, {templatePath}),
|
||||||
|
context: tpl(messages.templateError.context),
|
||||||
|
help: tpl(messages.templateError.help, {link: 'https://ghost.org/docs/install/source/'}),
|
||||||
|
error: error
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
res.sendFile(templatePath, {headers});
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue