0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

fix(NodeApp): end with "Internal server error" on mid-stream error (#9908)

* fix(NodeApp): end with "Internal server error" on mid-stream error

* add changeset

* add test

* Apply suggestions from code review

---------

Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
This commit is contained in:
Arsh 2024-02-01 07:02:40 +00:00 committed by GitHub
parent 440bdff8cc
commit 2f6d1faa6f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 35 additions and 4 deletions

View file

@ -0,0 +1,5 @@
---
"astro": patch
---
Improves http behavior relating to errors encountered while streaming a response.

View file

@ -118,12 +118,12 @@ export class NodeApp extends App {
destination.write(result.value);
result = await reader.read();
}
destination.end();
// the error will be logged by the "on end" callback above
} catch {
destination.write('Internal server error');
destination.end('Internal server error');
}
}
destination.end();
}
}

View file

@ -1,4 +1,4 @@
import * as assert from 'node:assert/strict';
import assert from 'node:assert/strict';
import { describe, it, before, after } from 'node:test';
import nodejs from '../dist/index.js';
import { loadFixture } from './test-utils.js';
@ -22,11 +22,26 @@ describe('Errors', () => {
after(async () => {
await devPreview.stop();
});
it('when mode is standalone', async () => {
it('rejected promise in template', async () => {
const res = await fixture.fetch('/in-stream');
const html = await res.text();
const $ = cheerio.load(html);
assert.equal($('p').text().trim(), 'Internal server error');
});
it('generator that throws called in template', async () => {
/** @type {Response} */
const res = await fixture.fetch('/generator');
const reader = res.body.getReader();
const decoder = new TextDecoder();
const expect = async ({ done, value }) => {
const result = await reader.read();
assert.equal(result.done, done);
if (!done) assert.equal(decoder.decode(result.value), value);
}
await expect({ done: false, value: "<!DOCTYPE html><h1>Astro</h1> 1Internal server error" });
await expect({ done: true });
});
});

View file

@ -0,0 +1,11 @@
---
function * generator () {
yield 1
throw Error('ohnoes')
}
---
<h1>Astro</h1>
{generator()}
<footer>
Footer
</footer>