diff --git a/hrgt/src/Resolver.ts b/hrgt/src/Resolver.ts index a8081a5..5a293ab 100644 --- a/hrgt/src/Resolver.ts +++ b/hrgt/src/Resolver.ts @@ -167,25 +167,25 @@ export class Resolver { for (const converter of converters) { try { const hrgEntry = converter.convert(profile) - if (hrgEntry == converter.getBlank()) { - throw new Error( - `Conversion of entry '${entry.termid}' using ${converter.name} did not fill in any expression` - ) - } else { - replacement += hrgEntry - } + replacement += hrgEntry } catch (err) { log.warn(`\t\t${err.message}`) + log.settings.minLevel = undefined // reset the log level set by {{log level="silent"}} + try { + const error = Converter.instances.find((i) => i.n === -1) + profile.err.message = err.message + if (error) { + replacement = error.convert(profile) + } + } catch (err) { + log.warn(`\t\t${err.message}`) + } } + log.settings.minLevel = undefined // reset the log level set by {{log level="silent"}} } } } else { - // If the MRG instance is not found, create the replacement using the error converter - const error = Converter.instances.find((i) => i.n === -1) - profile.err.message = `Something went wrong while retrieving the MRG file '${mrgref.hrg}'` - if (error) { - replacement = error.convert(profile) - } + report.onNotExistError(new Error(`Something went wrong while retrieving the MRG file '${mrgref.hrg}'`)) } // Only execute the replacement steps if the 'replacement' string is not empty diff --git a/mrgt/src/Generator.ts b/mrgt/src/Generator.ts index bcd064f..10816be 100644 --- a/mrgt/src/Generator.ts +++ b/mrgt/src/Generator.ts @@ -65,12 +65,11 @@ export class Generator { for (const instance of TuCBuilder.instances) { if (instance.tuc.cText) { log.info(`\x1b[1;37mProcessing synonymOf entries...\x1b[0m`) - // temporarily set the log level to 5 to suppress the output of the MRG files - log.settings.minLevel = 5 + log.settings.minLevel = 5 // temporarily set the log level to 5 to suppress the output of the MRG files this.handleSynonymOf(instance) } } - log.settings.minLevel = 1 + log.settings.minLevel = undefined // reset the log level } } diff --git a/trrt/src/Resolver.ts b/trrt/src/Resolver.ts index 6eab284..adbdc3c 100644 --- a/trrt/src/Resolver.ts +++ b/trrt/src/Resolver.ts @@ -147,9 +147,13 @@ export class Resolver { } catch (err) { error = err // store the error so it can be thrown after the replacement steps with converter[error] profile.err.cause = err.message - const converter = Converter.instances.find((i) => i.n === -1) // get the Converter instance where n = -1 (converter[error] option) - if (converter) { - replacement = converter.convert(profile) + try { + const converter = Converter.instances.find((i) => i.n === -1) // get the Converter instance where n = -1 (converter[error] option) + if (converter) { + replacement = converter.convert(profile) + } + } catch (err) { + error.message = `${error.message}, and: ${err.message}` } } @@ -193,7 +197,13 @@ export class Resolver { if (interpretation !== reference) { reference = `${reference}' > '${interpretation}` } - const message = `Term ref '${match[0]}' > '${reference}', ${err}` + + let message = `Term ref '${match[0]}' > '${reference}', ${err}` + // If the log level is set to 5 by {{log level="silent"}}, do not log the message + if (log.settings.minLevel === 5) { + message = undefined + log.settings.minLevel = undefined // reset the log level + } report.errors.push({ type: "TERM HELP", line, diff --git a/utils/src/classes/Handlebars.ts b/utils/src/classes/Handlebars.ts index 06d5418..0c9667b 100644 --- a/utils/src/classes/Handlebars.ts +++ b/utils/src/classes/Handlebars.ts @@ -5,12 +5,19 @@ import { ifValue } from "../helpers/ifValue.js" import { localize } from "../helpers/localize.js" import { noRefs } from "../helpers/noRefs.js" import { regularize } from "../helpers/regularize.js" +import { log } from "../helpers/log.js" Handlebars.registerHelper("capFirst", capFirst) Handlebars.registerHelper("ifValue", ifValue) Handlebars.registerHelper("localize", localize) Handlebars.registerHelper("noRefs", noRefs) Handlebars.registerHelper("regularize", regularize) +Handlebars.registerHelper("log", log) + +// Handlebars.log = function (level: number, ...message: string[]) { +// console.log(`Level: ${level}.`) +// console.log(`Message: ${message}`) +// } export { Handlebars } diff --git a/utils/src/classes/MRG.ts b/utils/src/classes/MRG.ts index 270a280..602fd0e 100644 --- a/utils/src/classes/MRG.ts +++ b/utils/src/classes/MRG.ts @@ -146,7 +146,7 @@ export function getEntry(entries: Entry[], origin: string, term: string, type: s if (matches.length === 1) { return matches[0] } else if (matches.length === 0) { - throw new Error(`could not be matched with an MRG entry in '${origin}`) + throw new Error(`could not be matched with an MRG entry in '${origin}'`) } else if (matches.length > 1) { const matchingTermIds = matches.map((entry) => entry.termid).join("', '") throw new Error(`has multiple matching MRG entries in '${origin}'. Matching termids: '${matchingTermIds}'`) diff --git a/utils/src/classes/Report.ts b/utils/src/classes/Report.ts index 4cc681f..5c1a416 100644 --- a/utils/src/classes/Report.ts +++ b/utils/src/classes/Report.ts @@ -26,10 +26,10 @@ class Report { console.log(`\x1B[0m`) console.log(` Resolution Report:`) - console.log(`\t\x1b[0mFiles modified: ${this.files.length}`) - console.log(`\t\x1b[0mTerms converted: ${this.terms.length}`) - console.log(`\t\x1b[0mTerm errors: ${termErrors.length}`) - console.log(`\t\x1b[0mMain errors: ${mainErrors.length}`) + console.log(`\t\x1b[0mFiles modified: ${this.files.length}`) + console.log(`\t\x1b[0mTerms converted: ${this.terms.length}`) + console.log(`\t\x1b[0mTerm errors \x1b[3m(silenced)\x1b[0m: ${termErrors.length}`) + console.log(`\t\x1b[0mMain error occurences: ${mainErrors.length}`) if (termErrors.length > 0) { console.log(`\n \x1b[1;37mTerm Errors:\x1b[0m`) @@ -37,6 +37,9 @@ class Report { const groupedTermErrors = new Map>() for (const err of termErrors) { + if (!err.cause) { + continue + } const key = err.cause.toString() if (groupedTermErrors.has(key)) { diff --git a/utils/src/helpers/log.ts b/utils/src/helpers/log.ts new file mode 100644 index 0000000..9f5e1bc --- /dev/null +++ b/utils/src/helpers/log.ts @@ -0,0 +1,31 @@ +import { type HelperOptions } from "handlebars" +import { log as logger } from "../classes/Report.js" + +export function log(...args: unknown[]) { + const options: Partial = args[args.length - 1] + args.pop() + let level = undefined + if (options.hash.level != null) { + level = options.hash.level + } + logger.settings.minLevel = undefined // reset the log level + + if (level === "silent") { + // temporarily up the log level to suppress the output of conversion related warnings + logger.settings.minLevel = 5 + } else if (level === "silly") { + logger.silly(...args) + } else if (level === "trace") { + logger.trace(...args) + } else if (level === "debug") { + logger.debug(...args) + } else if (level === "info") { + logger.info(...args) + } else if (level === "error") { + logger.error(...args) + } else if (level === "fatal") { + logger.fatal(...args) + } else { + logger.warn(...args) + } +}