From 3a50b8f2262747718a9254f567407d2b1b969645 Mon Sep 17 00:00:00 2001 From: Alexander Jones Date: Tue, 6 Aug 2024 04:54:45 -0500 Subject: [PATCH] Use BidsHedIssue and Issue for internal error reporting This will provide useful error messages for BIDS validation output. --- bids/types/issues.js | 35 +++++++++++++++++++++++++++++------ common/issues/data.js | 5 +++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/bids/types/issues.js b/bids/types/issues.js index 97f4c24e..bdd23d7b 100644 --- a/bids/types/issues.js +++ b/bids/types/issues.js @@ -1,4 +1,4 @@ -import { IssueError } from '../../common/issues/issues' +import { generateIssue, IssueError } from '../../common/issues/issues' const bidsHedErrorCodes = new Set([104, 106, 107]) @@ -44,8 +44,8 @@ export class BidsIssue { return issues.some((issue) => issue.isError()) } - static generateInternalErrorPromise(error, errorFile) { - return Promise.resolve([new BidsIssue(106, errorFile, error.message)]) + static async generateInternalErrorPromise(error, errorFile) { + return [new BidsHedIssue(generateIssue('internalError', { message: error.message }), errorFile)] } } @@ -56,25 +56,48 @@ export class BidsHedIssue extends BidsIssue { */ hedIssue + /** + * Constructor. + * + * @param {Issue} hedIssue The HED issue object to be wrapped. + * @param {Object} file The file this error occurred in. + */ constructor(hedIssue, file) { - super(hedIssue.level === 'warning' ? 105 : 104, file, hedIssue.message) + super(BidsHedIssue._determineBidsIssueCode(hedIssue), file, hedIssue.message) this.hedIssue = hedIssue } + /** + * Determine the BIDS issue code for this issue. + * + * @param {Issue} hedIssue The HED issue object to be wrapped. + * @returns {number} The BIDS issue code for this issue. + * @private + */ + static _determineBidsIssueCode(hedIssue) { + if (hedIssue.internalCode === 'internalError' || hedIssue.internalCode === 'internalConsistencyError') { + return 106 + } + if (hedIssue.level === 'warning') { + return 105 + } + return 104 + } + /** * Convert one or more HED issues into BIDS-compatible issues. * * @param {Error|Issue[]} hedIssues One or more HED-format issues. * @param {Object} file A BIDS-format file object used to generate {@link BidsHedIssue} objects. * @param {Object?} extraParameters Any extra parameters to inject into the {@link Issue} objects. - * @returns {BidsIssue[]} The passed issue(s) in BIDS-compatible format. + * @returns {BidsHedIssue[]} The passed issue(s) in BIDS-compatible format. */ static fromHedIssues(hedIssues, file, extraParameters) { if (hedIssues instanceof IssueError) { return [BidsHedIssue.fromHedIssue(hedIssues.issue, file, extraParameters)] } else if (hedIssues instanceof Error) { - return [new BidsIssue(106, file ?? null, hedIssues.message)] + return [new BidsHedIssue(generateIssue('internalError', { message: hedIssues.message }), file ?? null)] } else { return hedIssues.map((hedIssue) => BidsHedIssue.fromHedIssue(hedIssue, file, extraParameters)) } diff --git a/common/issues/data.js b/common/issues/data.js index 115cfd93..397648e9 100644 --- a/common/issues/data.js +++ b/common/issues/data.js @@ -314,6 +314,11 @@ export default { level: 'error', message: stringTemplate`Unknown HED error "${'internalCode'}" - parameters: "${'parameters'}".`, }, + internalError: { + hedCode: 'GENERIC_ERROR', + level: 'error', + message: stringTemplate`Internal error - message: "${'message'}".`, + }, internalConsistencyError: { hedCode: 'GENERIC_ERROR', level: 'error',