Improve error messages

This commit is contained in:
pietervdvn 2022-06-09 16:50:53 +02:00
parent adcdf4f18a
commit b9cc56bbc5
5 changed files with 43 additions and 36 deletions

View file

@ -216,11 +216,11 @@ export class TagUtils {
* *
* TagUtils.Tag("xyz<5").matchesProperties({xyz: 4}) // => true * TagUtils.Tag("xyz<5").matchesProperties({xyz: 4}) // => true
* TagUtils.Tag("xyz<5").matchesProperties({xyz: 5}) // => false * TagUtils.Tag("xyz<5").matchesProperties({xyz: 5}) // => false
* *
* // RegexTags must match values with newlines * // RegexTags must match values with newlines
* TagUtils.Tag("note~.*aed.*").matchesProperties({note: "Hier bevindt zich wss een defibrillator. \\n\\n De aed bevindt zich op de 5de verdieping"}) // => true * TagUtils.Tag("note~.*aed.*").matchesProperties({note: "Hier bevindt zich wss een defibrillator. \\n\\n De aed bevindt zich op de 5de verdieping"}) // => true
* TagUtils.Tag("note~i~.*aed.*").matchesProperties({note: "Hier bevindt zich wss een defibrillator. \\n\\n De AED bevindt zich op de 5de verdieping"}) // => true * TagUtils.Tag("note~i~.*aed.*").matchesProperties({note: "Hier bevindt zich wss een defibrillator. \\n\\n De AED bevindt zich op de 5de verdieping"}) // => true
* *
* // Must match case insensitive * // Must match case insensitive
* TagUtils.Tag("name~i~somename").matchesProperties({name: "SoMeName"}) // => true * TagUtils.Tag("name~i~somename").matchesProperties({name: "SoMeName"}) // => true
*/ */
@ -286,7 +286,7 @@ export class TagUtils {
private static TagUnsafe(json: AndOrTagConfigJson | string, context: string = ""): TagsFilter { private static TagUnsafe(json: AndOrTagConfigJson | string, context: string = ""): TagsFilter {
if (json === undefined) { if (json === undefined) {
throw `Error while parsing a tag: 'json' is undefined in ${context}. Make sure all the tags are defined and at least one tag is present in a complex expression` throw new Error(`Error while parsing a tag: 'json' is undefined in ${context}. Make sure all the tags are defined and at least one tag is present in a complex expression`)
} }
if (typeof (json) != "string") { if (typeof (json) != "string") {
if (json.and !== undefined && json.or !== undefined) { if (json.and !== undefined && json.or !== undefined) {
@ -335,7 +335,7 @@ export class TagUtils {
return new ComparingTag(split[0], f, operator + val) return new ComparingTag(split[0], f, operator + val)
} }
} }
if (tag.indexOf("~~") >= 0) { if (tag.indexOf("~~") >= 0) {
const split = Utils.SplitFirst(tag, "~~"); const split = Utils.SplitFirst(tag, "~~");
if (split[1] === "*") { if (split[1] === "*") {
@ -346,9 +346,9 @@ export class TagUtils {
new RegExp("^" + split[1] + "$", "s") new RegExp("^" + split[1] + "$", "s")
); );
} }
const withRegex = TagUtils.parseRegexOperator(tag) const withRegex = TagUtils.parseRegexOperator(tag)
if(withRegex != null) { if (withRegex != null) {
if (withRegex.value === "*" && withRegex.invert) { if (withRegex.value === "*" && withRegex.invert) {
throw `Don't use 'key!~*' - use 'key=' instead (empty string as value (in the tag ${tag} while parsing ${context})` throw `Don't use 'key!~*' - use 'key=' instead (empty string as value (in the tag ${tag} while parsing ${context})`
} }
@ -362,7 +362,7 @@ export class TagUtils {
} }
return new RegexTag( return new RegexTag(
withRegex.key, withRegex.key,
new RegExp("^"+value+"$", "s"+withRegex.modifier), new RegExp("^" + value + "$", "s" + withRegex.modifier),
withRegex.invert withRegex.invert
); );
} }
@ -459,35 +459,35 @@ export class TagUtils {
/** /**
* Returns 'true' is opposite tags are detected. * Returns 'true' is opposite tags are detected.
* Note that this method will never work perfectly * Note that this method will never work perfectly
* *
* // should be false for some simple cases * // should be false for some simple cases
* TagUtils.ContainsOppositeTags([new Tag("key", "value"), new Tag("key0", "value")]) // => false * TagUtils.ContainsOppositeTags([new Tag("key", "value"), new Tag("key0", "value")]) // => false
* TagUtils.ContainsOppositeTags([new Tag("key", "value"), new Tag("key", "value0")]) // => false * TagUtils.ContainsOppositeTags([new Tag("key", "value"), new Tag("key", "value0")]) // => false
* *
* // should detect simple cases * // should detect simple cases
* TagUtils.ContainsOppositeTags([new Tag("key", "value"), new RegexTag("key", "value", true)]) // => true * TagUtils.ContainsOppositeTags([new Tag("key", "value"), new RegexTag("key", "value", true)]) // => true
* TagUtils.ContainsOppositeTags([new Tag("key", "value"), new RegexTag("key", /value/, true)]) // => true * TagUtils.ContainsOppositeTags([new Tag("key", "value"), new RegexTag("key", /value/, true)]) // => true
*/ */
public static ContainsOppositeTags(tags: (TagsFilter)[]) : boolean{ public static ContainsOppositeTags(tags: (TagsFilter)[]): boolean {
for (let i = 0; i < tags.length; i++){ for (let i = 0; i < tags.length; i++) {
const tag = tags[i]; const tag = tags[i];
if(!(tag instanceof Tag || tag instanceof RegexTag)){ if (!(tag instanceof Tag || tag instanceof RegexTag)) {
continue continue
} }
for (let j = i + 1; j < tags.length; j++){ for (let j = i + 1; j < tags.length; j++) {
const guard = tags[j]; const guard = tags[j];
if(!(guard instanceof Tag || guard instanceof RegexTag)){ if (!(guard instanceof Tag || guard instanceof RegexTag)) {
continue continue
} }
if(guard.key !== tag.key) { if (guard.key !== tag.key) {
// Different keys: they can _never_ be opposites // Different keys: they can _never_ be opposites
continue continue
} }
if((guard.value["source"] ?? guard.value) !== (tag.value["source"] ?? tag.value)){ if ((guard.value["source"] ?? guard.value) !== (tag.value["source"] ?? tag.value)) {
// different values: the can _never_ be opposites // different values: the can _never_ be opposites
continue continue
} }
if( (guard["invert"] ?? false) !== (tag["invert"] ?? false) ) { if ((guard["invert"] ?? false) !== (tag["invert"] ?? false)) {
// The 'invert' flags are opposite, the key and value is the same for both // The 'invert' flags are opposite, the key and value is the same for both
// This means we have found opposite tags! // This means we have found opposite tags!
return true return true
@ -502,28 +502,28 @@ export class TagUtils {
* Returns a filtered version of 'listToFilter'. * Returns a filtered version of 'listToFilter'.
* For a list [t0, t1, t2], If `blackList` contains an equivalent (or broader) match of any `t`, this respective `t` is dropped from the returned list * For a list [t0, t1, t2], If `blackList` contains an equivalent (or broader) match of any `t`, this respective `t` is dropped from the returned list
* Ignores nested ORS and ANDS * Ignores nested ORS and ANDS
* *
* TagUtils.removeShadowedElementsFrom([new Tag("key","value")], [new Tag("key","value"), new Tag("other_key","value")]) // => [new Tag("other_key","value")] * TagUtils.removeShadowedElementsFrom([new Tag("key","value")], [new Tag("key","value"), new Tag("other_key","value")]) // => [new Tag("other_key","value")]
*/ */
public static removeShadowedElementsFrom(blacklist: TagsFilter[], listToFilter: TagsFilter[] ) : TagsFilter[] { public static removeShadowedElementsFrom(blacklist: TagsFilter[], listToFilter: TagsFilter[]): TagsFilter[] {
return listToFilter.filter(tf => !blacklist.some(guard => guard.shadows(tf))) return listToFilter.filter(tf => !blacklist.some(guard => guard.shadows(tf)))
} }
/** /**
* Returns a filtered version of 'listToFilter', where no duplicates and no equivalents exists. * Returns a filtered version of 'listToFilter', where no duplicates and no equivalents exists.
* *
* TagUtils.removeEquivalents([new RegexTag("key", /^..*$/), new Tag("key","value")]) // => [new Tag("key", "value")] * TagUtils.removeEquivalents([new RegexTag("key", /^..*$/), new Tag("key","value")]) // => [new Tag("key", "value")]
*/ */
public static removeEquivalents( listToFilter: (Tag | RegexTag)[]) : TagsFilter[] { public static removeEquivalents(listToFilter: (Tag | RegexTag)[]): TagsFilter[] {
const result: TagsFilter[] = [] const result: TagsFilter[] = []
outer: for (let i = 0; i < listToFilter.length; i++){ outer: for (let i = 0; i < listToFilter.length; i++) {
const tag = listToFilter[i]; const tag = listToFilter[i];
for (let j = 0; j < listToFilter.length; j++){ for (let j = 0; j < listToFilter.length; j++) {
if(i === j){ if (i === j) {
continue continue
} }
const guard = listToFilter[j]; const guard = listToFilter[j];
if(guard.shadows(tag)) { if (guard.shadows(tag)) {
// the guard 'kills' the tag: we continue the outer loop without adding the tag // the guard 'kills' the tag: we continue the outer loop without adding the tag
continue outer; continue outer;
} }
@ -532,7 +532,7 @@ export class TagUtils {
} }
return result return result
} }
/** /**
* Returns `true` if at least one element of the 'guards' shadows one element of the 'listToFilter'. * Returns `true` if at least one element of the 'guards' shadows one element of the 'listToFilter'.
* *
@ -540,7 +540,7 @@ export class TagUtils {
* TagUtils.containsEquivalents([new Tag("key","value")], [ new Tag("other_key","value")]) // => false * TagUtils.containsEquivalents([new Tag("key","value")], [ new Tag("other_key","value")]) // => false
* TagUtils.containsEquivalents([new Tag("key","value")], [ new Tag("key","other_value")]) // => false * TagUtils.containsEquivalents([new Tag("key","value")], [ new Tag("key","other_value")]) // => false
*/ */
public static containsEquivalents( guards: TagsFilter[], listToFilter: TagsFilter[] ) : boolean { public static containsEquivalents(guards: TagsFilter[], listToFilter: TagsFilter[]): boolean {
return listToFilter.some(tf => guards.some(guard => guard.shadows(tf))) return listToFilter.some(tf => guards.some(guard => guard.shadows(tf)))
} }

View file

@ -226,7 +226,7 @@ export class Fuse<T> extends DesugaringStep<T> {
break; break;
} }
}catch(e){ }catch(e){
console.error("Step "+step.name+" failed due to "+e); console.error("Step "+step.name+" failed due to ",e,e.stack);
throw e throw e
} }
} }

View file

@ -321,8 +321,9 @@ export class DetectShadowedMappings extends DesugaringStep<QuestionableTagRender
for (const calculatedTagName of this._calculatedTagNames) { for (const calculatedTagName of this._calculatedTagNames) {
defaultProperties[calculatedTagName] = "some_calculated_tag_value_for_"+calculatedTagName defaultProperties[calculatedTagName] = "some_calculated_tag_value_for_"+calculatedTagName
} }
const parsedConditions = json.mappings.map(m => { const parsedConditions = json.mappings.map((m,i) => {
const ifTags = TagUtils.Tag(m.if); const ctx = `${context}.mappings[${i}]`
const ifTags = TagUtils.Tag(m.if, ctx);
if(m.hideInAnswer !== undefined && m.hideInAnswer !== false && m.hideInAnswer !== true){ if(m.hideInAnswer !== undefined && m.hideInAnswer !== false && m.hideInAnswer !== true){
let conditionTags = TagUtils.Tag( m.hideInAnswer) let conditionTags = TagUtils.Tag( m.hideInAnswer)
// Merge the condition too! // Merge the condition too!
@ -407,7 +408,7 @@ export class DetectMappingsWithImages extends DesugaringStep<TagRenderingConfigJ
const mapping = json.mappings[i] const mapping = json.mappings[i]
const ignore = mapping["#"]?.indexOf(ignoreToken) >=0 const ignore = mapping["#"]?.indexOf(ignoreToken) >=0
const images = Utils.Dedup(Translations.T(mapping.then).ExtractImages()) const images = Utils.Dedup(Translations.T(mapping.then)?.ExtractImages() ?? [])
const ctx = `${context}.mappings[${i}]` const ctx = `${context}.mappings[${i}]`
if (images.length > 0) { if (images.length > 0) {
if(!ignore){ if(!ignore){

View file

@ -287,7 +287,7 @@ export interface LayerConfigJson {
*/ */
tagRenderings?: tagRenderings?:
(string (string
| { builtin: string, override: any } | { builtin: string | string[], override: any }
| QuestionableTagRenderingConfigJson | QuestionableTagRenderingConfigJson
| RewritableConfigJson<(string | { builtin: string, override: any } | QuestionableTagRenderingConfigJson)[]> | RewritableConfigJson<(string | { builtin: string, override: any } | QuestionableTagRenderingConfigJson)[]>
) [], ) [],

View file

@ -172,15 +172,21 @@ export default class TagRenderingConfig {
this.mappings = json.mappings.map((mapping, i) => { this.mappings = json.mappings.map((mapping, i) => {
const ctx = `${translationKey}.mappings.${i}` const ctx = `${translationKey}.mappings.${i}`
if (mapping.if === undefined) {
throw `${ctx}: Invalid mapping: "if" is not defined in ${JSON.stringify(mapping)}`
}
if (mapping.then === undefined) { if (mapping.then === undefined) {
throw `${ctx}: Invalid mapping: if without body` if(mapping["render"] !== undefined){
throw `${ctx}: Invalid mapping: no 'then'-clause found. You might have typed 'render' instead of 'then', change it in ${JSON.stringify(mapping)}`
}
throw `${ctx}: Invalid mapping: no 'then'-clause found in ${JSON.stringify(mapping)}`
} }
if (mapping.ifnot !== undefined && !this.multiAnswer) { if (mapping.ifnot !== undefined && !this.multiAnswer) {
throw `${ctx}: Invalid mapping: ifnot defined, but the tagrendering is not a multianswer` throw `${ctx}: Invalid mapping: 'ifnot' is defined, but the tagrendering is not a multianswer. Either remove ifnot or set 'multiAnswer:true' to enable checkboxes instead of radiobuttons`
} }
if (mapping.if === undefined) { if(mapping["render"] !== undefined){
throw `${ctx}: Invalid mapping: "if" is not defined, but the tagrendering is not a multianswer` throw `${ctx}: Invalid mapping: a 'render'-key is present, this is probably a bug: ${JSON.stringify(mapping)}`
} }
if (typeof mapping.if !== "string" && mapping.if["length"] !== undefined) { if (typeof mapping.if !== "string" && mapping.if["length"] !== undefined) {
throw `${ctx}: Invalid mapping: "if" is defined as an array. Use {"and": <your conditions>} or {"or": <your conditions>} instead` throw `${ctx}: Invalid mapping: "if" is defined as an array. Use {"and": <your conditions>} or {"or": <your conditions>} instead`