Skip to content

Commit

Permalink
Merge pull request #164 from hed-standard/redo-internal-error-reporting
Browse files Browse the repository at this point in the history
Use BidsHedIssue and Issue for internal error reporting
  • Loading branch information
VisLab authored Aug 6, 2024
2 parents e8a9fe6 + 3a50b8f commit 98acab5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
35 changes: 29 additions & 6 deletions bids/types/issues.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IssueError } from '../../common/issues/issues'
import { generateIssue, IssueError } from '../../common/issues/issues'

const bidsHedErrorCodes = new Set([104, 106, 107])

Expand Down Expand Up @@ -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)]
}
}

Expand All @@ -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))
}
Expand Down
5 changes: 5 additions & 0 deletions common/issues/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 98acab5

Please sign in to comment.