Prevent server hang in MDX when code throws

This commit is contained in:
Matthew Phillips 2022-10-07 15:48:17 -04:00
parent e5c64c51c1
commit 25643a0beb
8 changed files with 72 additions and 0 deletions

View file

@ -85,6 +85,7 @@ Did you forget to import the component or is it possible there is a typo?`);
}
} catch (e) {
skipAstroJSXCheck.add(vnode.type);
throw e;
} finally {
finishUsingConsoleFilter();
}

View file

@ -0,0 +1,5 @@
import mdx from '@astrojs/mdx';
export default {
integrations: [mdx()]
}

View file

@ -0,0 +1,7 @@
{
"name": "@test/mdx-throw-error",
"dependencies": {
"@astrojs/mdx": "workspace:*",
"astro": "workspace:*"
}
}

View file

@ -0,0 +1,5 @@
import { throwError } from '../js/throwerror';
This will probably result in a stack overflow. :(
<pre>{ throwError() }</pre>

View file

@ -0,0 +1,5 @@
export function throwError() {
console.log(`I'm going to throw an error. The server will just keep spinning until it runs out of memory...`);
throw new Error('Oh no');
}

View file

@ -0,0 +1,16 @@
---
import Component from '../components/component.mdx';
---
<html>
<head>
<title>Testing</title>
</head>
<body>
<main>
<h1>Astro MDX Errors</h1>
<Component />
</main>
</body>
</html>

View file

@ -0,0 +1,25 @@
import mdx from '@astrojs/mdx';
import { expect } from 'chai';
import { loadFixture } from '../../../astro/test/test-utils.js';
describe('MDX errors', () => {
/** @type {import('../../../astro/test/test-utils.js').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
root: new URL('./fixtures/mdx-throw-error/', import.meta.url),
integrations: [mdx()],
});
});
it('throws error during the build (does not lock up)', async () => {
try {
await fixture.build();
expect(false).to.equal(true);
} catch(err) {
expect(err.message).to.equal('Oh no');
}
});
});

View file

@ -2821,6 +2821,14 @@ importers:
react: 18.2.0
react-dom: 18.2.0_react@18.2.0
packages/integrations/mdx/test/fixtures/mdx-throw-error:
specifiers:
'@astrojs/mdx': workspace:*
astro: workspace:*
dependencies:
'@astrojs/mdx': link:../../..
astro: link:../../../../../astro
packages/integrations/mdx/test/fixtures/mdx-vite-env-vars:
specifiers:
'@astrojs/mdx': workspace:*