mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-04-08 02:52:39 -05:00
Simplified yaml parser to have fewer dependencies
refs https://linear.app/tryghost/issue/CORE-35/refactor-route-and-redirect-settings
refs 5715aa2155 (diff-48644be82a9b957e5e627bf7b0f2f73cdb1d63851ffad68c7c178c5886495bb8R52-R57)
- Simplified the yaml parser implementation to take in a single parameter, this move will allove to simplify the logic in the route settings + opens a door to unify handling with redirects yaml parsing!
- We loose the "filename" from the error information but that was a generic "routes.yaml" anyway and would be thrown only when somebody uploaded a routes.yaml file (no real added value).
- The debug statement should be moved to contain related filepath+other info to the calling module instead
- An additional error handler was borrowed from the redirects yaml parsing logic that was introduced in a referenced commit - it still makes sense to keep it for routes.yaml configuration
This commit is contained in:
parent
2649f32dd2
commit
99a2f12cb7
2 changed files with 27 additions and 14 deletions
|
@ -1,36 +1,49 @@
|
|||
const yaml = require('js-yaml');
|
||||
const debug = require('@tryghost/debug')('frontend:services:settings:yaml-parser');
|
||||
const tpl = require('@tryghost/tpl');
|
||||
const errors = require('@tryghost/errors');
|
||||
|
||||
const messages = {
|
||||
error: 'Could not parse {file}: {context}.',
|
||||
help: 'Check your {file} file for typos and fix the named issues.'
|
||||
error: 'Could not parse provided YAML file: {context}.',
|
||||
help: 'Check provided file for typos and fix the named issues.',
|
||||
yamlParse: 'YAML input cannot be a plain string. Check the format of your YAML file.',
|
||||
yamlParseHelp: 'https://ghost.org/docs/themes/routing/'
|
||||
};
|
||||
|
||||
/**
|
||||
* Takes a YAML file, parses it and returns a JSON Object
|
||||
* @param {YAML} file the YAML file utf8 encoded
|
||||
* @param {String} fileName the name of the file incl. extension
|
||||
* Takes a YAML formatted string and parses it and returns a JSON Object
|
||||
* @param {String} file the YAML file utf8 encoded
|
||||
* @returns {Object} parsed
|
||||
*/
|
||||
module.exports = function parseYaml(file, fileName) {
|
||||
module.exports = function parseYaml(file) {
|
||||
try {
|
||||
const parsed = yaml.load(file);
|
||||
|
||||
debug('YAML settings file parsed:', fileName);
|
||||
// yaml.load passes almost every yaml code.
|
||||
// Because of that, it's hard to detect if there's an error in the file.
|
||||
// But one of the obvious errors is the plain string output.
|
||||
// Here we check if the user made this mistake.
|
||||
if (typeof parsed === 'string') {
|
||||
throw new errors.BadRequestError({
|
||||
message: messages.yamlParse,
|
||||
help: messages.yamlParseHelp
|
||||
});
|
||||
}
|
||||
|
||||
return parsed;
|
||||
} catch (error) {
|
||||
if (errors.utils.isIgnitionError(error)) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
// CASE: parsing failed, `js-yaml` tells us exactly what and where in the
|
||||
// `reason` property as well as in the message.
|
||||
// As the file uploaded is invalid, the person uploading must fix this - it's a 4xx error
|
||||
throw new errors.IncorrectUsageError({
|
||||
message: tpl(messages.error, {file: fileName, context: error.reason}),
|
||||
message: tpl(messages.error, {context: error.reason}),
|
||||
code: 'YAML_PARSER_ERROR',
|
||||
context: error.message,
|
||||
err: error,
|
||||
help: tpl(messages.help, {file: fileName})
|
||||
help: messages.help
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
|
@ -20,7 +20,7 @@ describe('UNIT > Settings Service yaml parser:', function () {
|
|||
it('parses correct yaml file', function () {
|
||||
const file = fs.readFileSync(path.join(__dirname, '../../../utils/fixtures/settings/', 'goodroutes.yaml'), 'utf8');
|
||||
|
||||
const result = yamlParser(file, 'goodroutes.yaml');
|
||||
const result = yamlParser(file);
|
||||
should.exist(result);
|
||||
result.should.be.an.Object().with.properties('routes', 'collections', 'taxonomies');
|
||||
yamlSpy.calledOnce.should.be.true();
|
||||
|
@ -30,13 +30,13 @@ describe('UNIT > Settings Service yaml parser:', function () {
|
|||
const file = fs.readFileSync(path.join(__dirname, '../../../utils/fixtures/settings/', 'badroutes.yaml'), 'utf8');
|
||||
|
||||
try {
|
||||
const result = yamlParser(file, 'badroutes.yaml');
|
||||
const result = yamlParser(file);
|
||||
should.not.exist(result);
|
||||
} catch (error) {
|
||||
should.exist(error);
|
||||
error.message.should.eql('Could not parse badroutes.yaml: bad indentation of a mapping entry.');
|
||||
error.message.should.eql('Could not parse provided YAML file: bad indentation of a mapping entry.');
|
||||
error.context.should.containEql('bad indentation of a mapping entry (5:14)');
|
||||
error.help.should.eql('Check your badroutes.yaml file for typos and fix the named issues.');
|
||||
error.help.should.eql('Check provided file for typos and fix the named issues.');
|
||||
yamlSpy.calledOnce.should.be.true();
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue