Skip to content

Commit 2dcd739

Browse files
committed
Include the underlying exception in ESM import exceptions
1 parent d182a92 commit 2dcd739

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

lib/loader.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,10 @@ function fixupImportException(e, importedPath) {
138138
|| e.stack.match(anyWindowsPathFirstLineRegex)) {
139139
return e;
140140
} else {
141-
return new Error(`While loading ${importedPath}: ${e.constructor.name}: ${e.message}`);
141+
return new Error(
142+
`While loading ${importedPath}: ${e.constructor.name}: ${e.message}`,
143+
{cause: e}
144+
);
142145
}
143146
}
144147

spec/loader_spec.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('loader', function() {
2020
esModuleSharedExamples('js', true);
2121
});
2222

23-
describe('When the extnesion is not supported by import()', function() {
23+
describe('When the extension is not supported by import()', function() {
2424
it('falls back to require()', async function() {
2525
const error = new TypeError();
2626
error.code = 'ERR_UNKNOWN_FILE_EXTENSION';
@@ -155,9 +155,15 @@ function esModuleSharedExamples(extension, alwaysImport) {
155155
const underlyingError = new SyntaxError('some details but no filename, not even in the stack trace');
156156
const loader = new Loader({importShim: () => Promise.reject(underlyingError)});
157157

158-
await expectAsync(loader.load(`foo.${extension}`, alwaysImport)).toBeRejectedWithError(
159-
`While loading foo.${extension}: SyntaxError: some details but no filename, not even in the stack trace`
160-
);
158+
try {
159+
await loader.load(`foo.${extension}`, alwaysImport);
160+
fail('Expected loader to throw but it did not');
161+
} catch (thrown) {
162+
expect(thrown.message).toEqual(
163+
`While loading foo.${extension}: SyntaxError: some details but no filename, not even in the stack trace`
164+
);
165+
expect(thrown.cause).toBe(underlyingError);
166+
}
161167
});
162168

163169
it('does not modify errors that are not SyntaxError instances', async function() {

0 commit comments

Comments
 (0)