@@ -33,30 +33,26 @@ class DICOMEntity {
33
33
const name = this . constructor . name
34
34
const tags = this . constructor . tags
35
35
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
+ }
40
39
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
+ }
45
43
} )
46
44
}
47
45
48
46
extractTags ( dicomMetaData ) {
47
+ const name = this . constructor . name
49
48
const tags = this . constructor . tags
50
49
const primaryTag = this . constructor . primaryTag
51
50
tags . forEach ( ( tag ) => {
52
51
const value = dicomMetaData . string ( DICOM_DICTIONARY [ tag ] )
53
52
if ( this [ tag ] === undefined ) {
54
53
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 } ".` )
60
56
}
61
57
} )
62
58
}
@@ -160,8 +156,21 @@ class DICOMSerie extends DICOMEntity {
160
156
}
161
157
}
162
158
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 = [ ]
165
174
166
175
const parseFile = async ( file ) => {
167
176
// Read
@@ -182,13 +191,24 @@ const parseDicomFiles = async (fileList) => {
182
191
patient . parseMetaData ( dicomMetaData , file )
183
192
}
184
193
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
+
185
202
// Parse all files and populate patientDict
186
- const parseFiles = [ ...fileList ] . map ( parseFile )
187
203
const logName = `Parsed ${ fileList . length } DICOM files in`
188
204
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
+ } )
190
210
console . timeEnd ( logName )
191
- return patientDict
211
+ return { patientDict, failures }
192
212
}
193
213
194
214
export default parseDicomFiles
0 commit comments