Skip to content

Commit c41ac4c

Browse files
author
Alexis Girault
committed
Throw error instead of console.assert
1 parent 40b0fcc commit c41ac4c

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

examples/Dicom/src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const outputFileInformation = curry(async function outputFileInformation (output
1212
const files = event.target.files || dataTransfer.files
1313

1414
// Parse DICOM metadata
15-
const patientDict = await parseDicomFiles(files)
15+
const { patientDict, failures } = await parseDicomFiles(files, true)
1616

1717
// Select DICOM serie
1818
outputTextArea.textContent = "Please select serie..."

examples/Dicom/src/parseDicomFiles.js

+24-17
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,26 @@ class DICOMEntity {
3333
const name = this.constructor.name
3434
const tags = this.constructor.tags
3535
const primaryTag = this.constructor.primaryTag
36-
console.assert(
37-
tags.includes(primaryTag),
38-
`The primary tag of the ${name} class ("${primaryTag}") is not included in its list of tags ([${tags}]).`
39-
)
36+
if (!tags.includes(primaryTag)) {
37+
throw Error(`The primary tag of the ${name} class ("${primaryTag}") is not included in its list of tags ([${tags}]).`)
38+
}
4039
tags.forEach((tag) => {
41-
console.assert(
42-
tag in DICOM_DICTIONARY,
43-
`The tag "${tag}" associated with the ${name} class is not defined in DICOM_DICTIONARY.`
44-
)
40+
if (!(tag in DICOM_DICTIONARY)) {
41+
throw Error(`The tag "${tag}" associated with the ${name} class is not defined in DICOM_DICTIONARY.`)
42+
}
4543
})
4644
}
4745

4846
extractTags(dicomMetaData) {
47+
const name = this.constructor.name
4948
const tags = this.constructor.tags
5049
const primaryTag = this.constructor.primaryTag
5150
tags.forEach((tag) => {
5251
const value = dicomMetaData.string(DICOM_DICTIONARY[tag])
5352
if (this[tag] === undefined) {
5453
this[tag] = value
55-
} else if (value !== undefined) {
56-
console.assert(
57-
this[tag] === value,
58-
`Inconsistent value for ${tag} property of ${this[primaryTag]}`
59-
)
54+
} else if (value !== undefined && this[tag] !== value) {
55+
throw new Error(`Inconsistent value for the "${tag}" property of ${name} "${this[primaryTag]}": received "${this[tag]}" but already had "${value}".`)
6056
}
6157
})
6258
}
@@ -160,8 +156,9 @@ class DICOMSerie extends DICOMEntity {
160156
}
161157
}
162158

163-
const parseDicomFiles = async (fileList) => {
164-
var patientDict = {}
159+
const parseDicomFiles = async (fileList, skipOnFailure = false) => {
160+
const patientDict = {}
161+
const failures = []
165162

166163
const parseFile = async (file) => {
167164
// Read
@@ -182,13 +179,23 @@ const parseDicomFiles = async (fileList) => {
182179
patient.parseMetaData(dicomMetaData, file)
183180
}
184181

182+
// Set up promises
183+
const parseFiles = [...fileList].map((file) => {
184+
const promise = parseFile(file)
185+
if (skipOnFailure) {
186+
promise.catch((error) => {
187+
failures.push({ file, error })
188+
})
189+
}
190+
return promise
191+
})
192+
185193
// Parse all files and populate patientDict
186-
const parseFiles = [...fileList].map(parseFile)
187194
const logName = `Parsed ${fileList.length} DICOM files in`
188195
console.time(logName)
189196
await Promise.all(parseFiles)
190197
console.timeEnd(logName)
191-
return patientDict
198+
return { patientDict, failures }
192199
}
193200

194201
export default parseDicomFiles

0 commit comments

Comments
 (0)