Skip to content

Commit badead6

Browse files
committed
fix: improve itk-wasm pipeline error messages shown to users
itk-wasm WASM modules throw raw numeric pointers when they crash, and stderr output only goes to console.error, not the thrown error. This made load failures show meaningless numbers or empty messages. - Wrap readDicomTags errors in handleDicomFile and handleDicomStream with human-readable messages, preserving originals as cause - Check runTask returnValue/stderr for non-crash pipeline failures - Add getErrorDetail utility for extracting meaningful error messages
1 parent d9c563a commit badead6

4 files changed

Lines changed: 37 additions & 4 deletions

File tree

src/io/dicom.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,23 @@ async function runTask(
4747
inputs: any[],
4848
outputs: any[]
4949
) {
50-
return runPipeline(module, args, outputs, inputs, {
50+
const result = await runPipeline(module, args, outputs, inputs, {
5151
webWorker: getWorker(),
5252
pipelineBaseUrl: itkConfig.pipelinesUrl,
5353
pipelineWorkerUrl: itkConfig.pipelineWorkerUrl,
54+
}).catch((error) => {
55+
throw new Error(
56+
`itk-wasm pipeline "${module}" crashed (check browser console for details)`,
57+
{ cause: error }
58+
);
5459
});
60+
if (result.returnValue !== 0) {
61+
const detail = result.stderr?.trim() || 'unknown error';
62+
throw new Error(
63+
`itk-wasm pipeline "${module}" exited with code ${result.returnValue}: ${detail}`
64+
);
65+
}
66+
return result;
5567
}
5668

5769
/**

src/io/import/processors/handleDicomFile.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ReadDicomTagsFunction } from '@/src/core/streaming/dicom/dicomMetaLoade
66
import { ImportHandler, asIntermediateResult } from '@/src/io/import/common';
77
import { getWorker } from '@/src/io/itk/worker';
88
import { FILE_EXT_TO_MIME } from '@/src/io/mimeTypes';
9+
import { getErrorDetail } from '@/src/utils';
910
import { readDicomTags } from '@itk-wasm/dicom';
1011

1112
/**
@@ -22,8 +23,18 @@ const handleDicomFile: ImportHandler = async (dataSource) => {
2223
}
2324

2425
const readTags: ReadDicomTagsFunction = async (file) => {
25-
const result = await readDicomTags(file, { webWorker: getWorker() });
26-
return result.tags;
26+
try {
27+
const result = await readDicomTags(file, { webWorker: getWorker() });
28+
return result.tags;
29+
} catch (error) {
30+
const detail = getErrorDetail(
31+
error,
32+
'the file could not be parsed as valid DICOM (check browser console for details)'
33+
);
34+
throw new Error(`Failed to read DICOM tags: ${detail}`, {
35+
cause: error,
36+
});
37+
}
2738
};
2839

2940
const metaLoader = new DicomFileMetaLoader(dataSource.file, readTags);

src/io/import/processors/handleDicomStream.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
} from '@/src/io/import/common';
1515
import { getWorker } from '@/src/io/itk/worker';
1616
import { FILE_EXT_TO_MIME } from '@/src/io/mimeTypes';
17+
import { getErrorDetail } from '@/src/utils';
1718
import { readDicomTags } from '@itk-wasm/dicom';
1819
import { Tags } from '@/src/core/dicomTags';
1920
import { useMessageStore } from '@/src/store/messages';
@@ -34,8 +35,13 @@ const handleDicomStream: ImportHandler = async (dataSource) => {
3435
const result = await readDicomTags(file, { webWorker: getWorker() });
3536
return result.tags;
3637
} catch (error) {
38+
const detail = getErrorDetail(
39+
error,
40+
'the file could not be parsed as valid DICOM (check browser console for details)'
41+
);
3742
throw new Error(
38-
`Failed to read DICOM tags from ${dataSource.uri}: ${error}`
43+
`Failed to read DICOM tags from ${dataSource.uri}: ${detail}`,
44+
{ cause: error }
3945
);
4046
}
4147
};

src/utils/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ export function ensureError(e: unknown) {
215215
return e instanceof Error ? e : new Error(JSON.stringify(e));
216216
}
217217

218+
export function getErrorDetail(error: unknown, fallback: string): string {
219+
return error instanceof Error && error.message ? error.message : fallback;
220+
}
221+
218222
// remove undefined properties
219223
export function cleanUndefined(obj: Object) {
220224
return Object.entries(obj).reduce(

0 commit comments

Comments
 (0)