Skip to content

Commit

Permalink
fix(NodeApp): end with "Internal server error" on mid-stream error (#…
Browse files Browse the repository at this point in the history
…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 <[email protected]>
  • Loading branch information
lilnasy and natemoo-re authored Feb 1, 2024
1 parent 440bdff commit 2f6d1fa
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-roses-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Improves http behavior relating to errors encountered while streaming a response.
4 changes: 2 additions & 2 deletions packages/astro/src/core/app/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
19 changes: 17 additions & 2 deletions packages/integrations/node/test/errors.test.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 });
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
function * generator () {
yield 1
throw Error('ohnoes')
}
---
<h1>Astro</h1>
{generator()}
<footer>
Footer
</footer>

0 comments on commit 2f6d1fa

Please sign in to comment.