Add information about parser/index to error messages when parsing configuration file content.

This commit is contained in:
David Anson 2021-11-30 21:28:59 -08:00
parent 925f9cd168
commit 7330ea4946
3 changed files with 10 additions and 6 deletions

View file

@ -1717,13 +1717,14 @@ function parseConfiguration(name, content, parsers) {
var config = null; var config = null;
var message = ""; var message = "";
var errors = []; var errors = [];
var index = 0;
// Try each parser // Try each parser
(parsers || [JSON.parse]).every(function (parser) { (parsers || [JSON.parse]).every(function (parser) {
try { try {
config = parser(content); config = parser(content);
} }
catch (error) { catch (error) {
errors.push(error.message); errors.push("Parser " + index++ + ": " + error.message);
} }
return !config; return !config;
}); });

View file

@ -896,12 +896,13 @@ function parseConfiguration(name, content, parsers) {
let config = null; let config = null;
let message = ""; let message = "";
const errors = []; const errors = [];
let index = 0;
// Try each parser // Try each parser
(parsers || [ JSON.parse ]).every((parser) => { (parsers || [ JSON.parse ]).every((parser) => {
try { try {
config = parser(content); config = parser(content);
} catch (error) { } catch (error) {
errors.push(error.message); errors.push(`Parser ${index++}: ${error.message}`);
} }
return !config; return !config;
}); });

View file

@ -1321,7 +1321,7 @@ test.cb("configBadHybrid", (t) => {
t.true(err instanceof Error, "Error not instance of Error."); t.true(err instanceof Error, "Error not instance of Error.");
t.truthy(err.message.match( t.truthy(err.message.match(
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
/^Unable to parse '[^']*'; Unexpected token \S+ in JSON at position \d+;/ /^Unable to parse '[^']*'; Parser \d+: Unexpected token \S+ in JSON at position \d+;/
), "Error message unexpected."); ), "Error message unexpected.");
t.true(!result, "Got result for bad child JSON."); t.true(!result, "Got result for bad child JSON.");
t.end(); t.end();
@ -1390,7 +1390,8 @@ test("configBadJsonSync", (t) => {
}, },
{ {
"message": "message":
/Unable to parse '[^']*'; Unexpected token \S+ in JSON at position \d+/ // eslint-disable-next-line max-len
/Unable to parse '[^']*'; Parser \d+: Unexpected token \S+ in JSON at position \d+/
}, },
"Did not get correct exception for bad JSON." "Did not get correct exception for bad JSON."
); );
@ -1404,7 +1405,8 @@ test("configBadChildJsonSync", (t) => {
}, },
{ {
"message": "message":
/Unable to parse '[^']*'; Unexpected token \S+ in JSON at position \d+/ // eslint-disable-next-line max-len
/Unable to parse '[^']*'; Parser \d+: Unexpected token \S+ in JSON at position \d+/
}, },
"Did not get correct exception for bad child JSON." "Did not get correct exception for bad child JSON."
); );
@ -1498,7 +1500,7 @@ test("configBadHybridSync", (t) => {
}, },
{ {
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
"message": /^Unable to parse '[^']*'; Unexpected token \S+ in JSON at position \d+;/ "message": /^Unable to parse '[^']*'; Parser \d+: Unexpected token \S+ in JSON at position \d+;/
}, },
"Did not get correct exception for bad content." "Did not get correct exception for bad content."
); );