Better error message for invalid rewrites

This commit is contained in:
pietervdvn 2022-02-14 02:39:33 +01:00
parent 1c418e5a49
commit 776387a640

View file

@ -157,7 +157,7 @@ class ExpandGroupRewrite extends Conversion<{
{ {
rewrite: rewrite:
{ sourceString: string; into: string[] }[]; renderings: (string | { builtin: string; override: any } | TagRenderingConfigJson)[] { sourceString: string; into: string[] }[]; renderings: (string | { builtin: string; override: any } | TagRenderingConfigJson)[]
} | TagRenderingConfigJson, context: string): { result: TagRenderingConfigJson[]; errors: string[]; warnings: string[] } { } | TagRenderingConfigJson, context: string): { result: TagRenderingConfigJson[]; errors: string[]; warnings?: string[] } {
if (json["rewrite"] === undefined) { if (json["rewrite"] === undefined) {
return {result: [<TagRenderingConfigJson>json], errors: [], warnings: []} return {result: [<TagRenderingConfigJson>json], errors: [], warnings: []}
@ -168,20 +168,33 @@ class ExpandGroupRewrite extends Conversion<{
renderings: (string | { builtin: string; override: any } | TagRenderingConfigJson)[] renderings: (string | { builtin: string; override: any } | TagRenderingConfigJson)[]
}>json; }>json;
{ {
const errors = [] const errors = []
if(!Array.isArray(config.rewrite.sourceString)){
let extra = "";
if(typeof config.rewrite.sourceString === "string"){
extra=`<br/>Try <span class='literal-code'>"sourceString": [ "${config.rewrite.sourceString}" ] </span> instead (note the [ and ])`
}
const msg = context+"<br/>Invalid format: a rewrite block is defined, but the 'sourceString' should be an array of strings, but it is a "+typeof config.rewrite.sourceString + extra
errors.push(msg)
}
const expectedLength = config.rewrite.sourceString.length const expectedLength = config.rewrite.sourceString.length
for (let i = 0; i < config.rewrite.into.length; i++){ for (let i = 0; i < config.rewrite.into.length; i++){
const targets = config.rewrite.into[i]; const targets = config.rewrite.into[i];
if(targets.length !== expectedLength){ if(!Array.isArray(targets)){
errors.push(context+".rewrite.into["+i+"]: expected "+expectedLength+" values, but got "+targets.length) errors.push(`${context}.rewrite.into[${i}] should be an array of values, but it is a `+typeof targets)
} } else if(targets.length !== expectedLength){
errors.push(`${context}.rewrite.into[${i}]:<br/>The rewrite specified ${config.rewrite.sourceString} as sourcestring, which consists of ${expectedLength} values. The target ${JSON.stringify(targets)} has ${targets.length} items`)
if(typeof targets[0] !== "string"){ if(typeof targets[0] !== "string"){
errors.push(context+".rewrite.into["+i+"]: expected a string as first rewrite value values, but got "+targets[0]) errors.push(context+".rewrite.into["+i+"]: expected a string as first rewrite value values, but got "+targets[0])
} }
} }
}
if (errors.length > 0) { if (errors.length > 0) {
return { return {