Skip to content

Commit 9a5b752

Browse files
committed
cli: Add better JSON error output for validate
1 parent 9689ec1 commit 9a5b752

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

cli.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const fs = require('fs').promises;
2121
const path = require('path');
2222
const yaml = require('js-yaml');
2323

24-
const Template = require('./lib/template').Template;
2524
const FsTemplateProvider = require('./lib/template_provider').FsTemplateProvider;
2625
const guiUtils = require('./lib/gui_utils');
2726

@@ -82,11 +81,17 @@ const loadTemplate = (templatePath) => {
8281
const provider = new FsTemplateProvider(tsDir, [tsName]);
8382
return provider.fetch(`${tsName}/${tmplName}`)
8483
.catch((e) => {
85-
const validationErrors = Template.getValidationErrors();
86-
if (validationErrors !== 'null') {
87-
logger.error(validationErrors);
84+
const validationErrors = e.validationErrors;
85+
const errMsg = (validationErrors) ? 'template failed validation' : e.message;
86+
logger.error(`failed to load template: ${errMsg}`);
87+
if (validationErrors) {
88+
if (logger.isJSON) {
89+
logger.output.validationErrors = validationErrors;
90+
} else {
91+
logger.error('validation errors:');
92+
logger.error(validationErrors);
93+
}
8894
}
89-
logger.error(`failed to load template\n${e.stack}`);
9095
process.exit(1);
9196
});
9297
};
@@ -187,9 +192,22 @@ const validateTemplateSet = (tsPath) => {
187192
templateList.map(
188193
tmpl => provider.fetch(tmpl)
189194
.catch((e) => {
190-
logger.error(
191-
`Template "${tmpl}" failed validation:\n${e.stack}\n`
192-
);
195+
const validationErrors = e.validationErrors;
196+
const errMsg = `Template ${tmpl} failed validation: ${e.message}`;
197+
if (validationErrors) {
198+
if (logger.isJSON) {
199+
if (!logger.output.errors) {
200+
logger.output.errors = [];
201+
}
202+
logger.output.errors.push({
203+
message: errMsg,
204+
validationErrors
205+
});
206+
} else {
207+
logger.error(errMsg);
208+
logger.error(validationErrors);
209+
}
210+
}
193211
errorFound = true;
194212
})
195213

lib/template.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ validator.addFormat('mustache', {
7878
} catch (e) {
7979
_mustacheErrors.push({
8080
keyword: 'mustache',
81-
message: e.message
81+
dataPath: '',
82+
message: `invalid template text: ${e.message}`
8283
});
8384
return false;
8485
}
@@ -989,7 +990,13 @@ class Template {
989990
*/
990991
static validate(tmpldata) {
991992
if (!this.isValid(tmpldata)) {
992-
throw new Error(this.getValidationErrors());
993+
const validationErrors = this._parseValidationErrors([
994+
..._mustacheErrors,
995+
..._validateSchema.errors
996+
]);
997+
const err = Error('template failed validation');
998+
err.validationErrors = validationErrors;
999+
throw err;
9931000
}
9941001
}
9951002

@@ -1072,7 +1079,7 @@ class Template {
10721079
return Object.assign(defaults, mathExprResults);
10731080
}
10741081

1075-
_parseValidationErrors(errors) {
1082+
static _parseValidationErrors(errors) {
10761083
return errors.map((error) => {
10771084
const param = error.dataPath
10781085
.replace('.', ''); // strip leading dot
@@ -1102,7 +1109,7 @@ class Template {
11021109
validateParameters(parameters) {
11031110
const combParams = this.getCombinedParameters(parameters);
11041111
if (!this._parametersValidator(combParams)) {
1105-
const validationErrors = this._parseValidationErrors(this._parametersValidator.errors);
1112+
const validationErrors = Template._parseValidationErrors(this._parametersValidator.errors);
11061113
const err = Error('parameters failed validation');
11071114
err.validationErrors = validationErrors;
11081115
err.parameters = combParams;

0 commit comments

Comments
 (0)