Skip to content

Commit 0b39e7d

Browse files
author
Alexis Girault
committed
Improve error handling
1 parent 40b0fcc commit 0b39e7d

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

examples/Dicom/src/index.js

Lines changed: 1 addition & 1 deletion
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

Lines changed: 38 additions & 18 deletions
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,21 @@ class DICOMSerie extends DICOMEntity {
160156
}
161157
}
162158

163-
const parseDicomFiles = async (fileList) => {
164-
var patientDict = {}
159+
class ParseDicomError extends Error {
160+
constructor(failures) {
161+
const message =
162+
`Failed at parsing ${failures.length} DICOM file(s). ` +
163+
`Find the list of files and associated errors in the ` +
164+
`"failures" property of the thrown error, or ignore the ` +
165+
`errors by calling "parseDicomFiles(fileList, true)".`
166+
super(message)
167+
this.failures = failures
168+
}
169+
}
170+
171+
const parseDicomFiles = async (fileList, ignoreFailedFiles = false) => {
172+
const patientDict = {}
173+
const failures = []
165174

166175
const parseFile = async (file) => {
167176
// Read
@@ -182,13 +191,24 @@ const parseDicomFiles = async (fileList) => {
182191
patient.parseMetaData(dicomMetaData, file)
183192
}
184193

194+
// Set up promises
195+
const parseFiles = [...fileList].map((file) => {
196+
const promise = parseFile(file)
197+
return promise.catch((error) => {
198+
failures.push({ file, error })
199+
})
200+
})
201+
185202
// Parse all files and populate patientDict
186-
const parseFiles = [...fileList].map(parseFile)
187203
const logName = `Parsed ${fileList.length} DICOM files in`
188204
console.time(logName)
189-
await Promise.all(parseFiles)
205+
await Promise.all(parseFiles).then(() => {
206+
if (!ignoreFailedFiles && failures.length > 0) {
207+
throw new ParseDicomError(failures)
208+
}
209+
})
190210
console.timeEnd(logName)
191-
return patientDict
211+
return { patientDict, failures }
192212
}
193213

194214
export default parseDicomFiles

0 commit comments

Comments
 (0)