-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent errors in rendering from crashing server (#10221)
* Prevent errors in rendering from crashing server * Add changeset * Make the reject an error * Simplify * Update .changeset/breezy-pears-admire.md Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev> --------- Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>
- Loading branch information
1 parent
84502b4
commit 4db82d9
Showing
7 changed files
with
78 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"astro": patch | ||
--- | ||
|
||
Prevents errors in templates from crashing the server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,64 @@ | ||
import assert from 'node:assert/strict'; | ||
import { after, before, describe, it } from 'node:test'; | ||
import { loadFixture } from './test-utils.js'; | ||
import testAdapter from './test-adapter.js'; | ||
|
||
describe('Errors in JavaScript', () => { | ||
/** @type {import('./test-utils').Fixture} */ | ||
let fixture; | ||
|
||
/** @type {import('./test-utils').DevServer} */ | ||
let devServer; | ||
|
||
before(async () => { | ||
fixture = await loadFixture({ | ||
output: 'server', | ||
adapter: testAdapter(), | ||
root: './fixtures/error-bad-js', | ||
vite: { | ||
logLevel: 'silent', | ||
}, | ||
}); | ||
devServer = await fixture.startDevServer(); | ||
}); | ||
|
||
after(async () => { | ||
await devServer.stop(); | ||
}); | ||
describe('dev', () => { | ||
/** @type {import('./test-utils').DevServer} */ | ||
let devServer; | ||
|
||
before(async () => { | ||
devServer = await fixture.startDevServer(); | ||
}); | ||
|
||
after(async () => { | ||
await devServer.stop(); | ||
}); | ||
|
||
it('Does not crash the dev server', async () => { | ||
let res = await fixture.fetch('/'); | ||
let html = await res.text(); | ||
it('Does not crash the dev server', async () => { | ||
let res = await fixture.fetch('/'); | ||
let html = await res.text(); | ||
|
||
assert.equal(html.includes('ReferenceError'), true); | ||
|
||
res = await fixture.fetch('/'); | ||
await res.text(); | ||
|
||
assert.equal(html.includes('ReferenceError'), true); | ||
}); | ||
}); | ||
|
||
assert.equal(html.includes('ReferenceError'), true); | ||
describe('build', () => { | ||
before(async () => { | ||
await fixture.build(); | ||
}); | ||
|
||
res = await fixture.fetch('/'); | ||
await res.text(); | ||
it('in nested components, does not crash server', async () => { | ||
const app = await fixture.loadTestAdapterApp(); | ||
const request = new Request('http://example.com/in-stream'); | ||
const response = await app.render(request); | ||
|
||
assert.equal(html.includes('ReferenceError'), true); | ||
try { | ||
await response.text(); | ||
assert.ok(false, 'error expected'); | ||
} catch { | ||
assert.ok(true, "error caught during render"); | ||
} | ||
}); | ||
}); | ||
}); |
6 changes: 6 additions & 0 deletions
6
packages/astro/test/fixtures/error-bad-js/src/components/Other.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
import other from '../other.js'; | ||
--- | ||
<div> | ||
{other()} | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
export default function() { | ||
return somethingNotExists(); | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/astro/test/fixtures/error-bad-js/src/pages/in-stream.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- | ||
import Other from '../components/Other.astro'; | ||
--- | ||
|
||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<link rel="icon" type="image/svg+xml" href="/favicon.svg" /> | ||
<meta name="viewport" content="width=device-width" /> | ||
<meta name="generator" content={Astro.generator} /> | ||
<title>Astro</title> | ||
</head> | ||
<body> | ||
<h1>Astro</h1> | ||
<Other /> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export var foo = bar; | ||
|
||
export default foo; |