0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-03-10 23:01:26 -05:00

Catch errors that occur within the stream in the Node adapter (#6935)

* Catch errors that occur within the stream in the Node adapter

* Adding a changeset

* Better error message on completely uncaught errors within the stream

* Update test
This commit is contained in:
Matthew Phillips 2023-05-01 10:08:18 -04:00 committed by GitHub
parent 1edc289839
commit 94dc1a15d0
4 changed files with 62 additions and 2 deletions

View file

@ -51,8 +51,13 @@ async function writeWebResponse(app: NodeApp, res: ServerResponse, webResponse:
res.writeHead(status, Object.fromEntries(headers.entries()));
if (webResponse.body) {
for await (const chunk of responseIterator(webResponse) as unknown as Readable) {
res.write(chunk);
try {
for await (const chunk of responseIterator(webResponse) as unknown as Readable) {
res.write(chunk);
}
} catch(err: any) {
console.error(err?.stack || err?.message || String(err))
res.write('Internal server error');
}
}
res.end();

View file

@ -0,0 +1,33 @@
import nodejs from '../dist/index.js';
import { loadFixture } from './test-utils.js';
import { expect } from 'chai';
import * as cheerio from 'cheerio';
describe('Errors', () => {
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/errors/',
output: 'server',
adapter: nodejs({ mode: 'standalone' }),
});
await fixture.build();
});
describe('Within the stream', async () => {
let devPreview;
before(async () => {
devPreview = await fixture.preview();
});
after(async () => {
await devPreview.stop();
});
it('when mode is standalone', async () => {
const res = await fixture.fetch('/in-stream');
const html = await res.text();
const $ = cheerio.load(html);
expect($('p').text().trim()).to.equal('Internal server error');
});
});
});

View file

@ -0,0 +1,9 @@
{
"name": "@test/nodejs-errors",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*",
"@astrojs/node": "workspace:*"
}
}

View file

@ -0,0 +1,13 @@
---
---
<html>
<head>
<title>One</title>
</head>
<body>
<h1>One</h1>
<p>
{Promise.reject('Error in the stream')}
</p>
</body>
</html>