-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 500.astro improvements (#11134)
* feat: better error handling * feat: allow passing props to render context render * feat: work on tests * Update 500.astro * feat: test preview custom 500 * feat: test for custom 500 failing * feat: add changeset * Update rich-dolls-compete.md * Delete packages/astro/e2e/custom-500.test.js * Apply suggestions from code review Co-authored-by: Emanuele Stoppa <my.burning@gmail.com> * fix: merge * Update packages/astro/test/custom-500.test.js Co-authored-by: Emanuele Stoppa <my.burning@gmail.com> * Apply suggestions from code review Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * Update packages/astro/src/core/app/index.ts * feat: update --------- Co-authored-by: Emanuele Stoppa <my.burning@gmail.com> Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> Co-authored-by: Matthew Phillips <matthew@skypack.dev>
- Loading branch information
1 parent
c44f7f4
commit 9042be0
Showing
15 changed files
with
270 additions
and
10 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,22 @@ | ||
--- | ||
"astro": minor | ||
--- | ||
|
||
Improves the developer experience of the `500.astro` file by passing it a new `error` prop. | ||
|
||
When an error is thrown, the special `src/pages/500.astro` page now automatically receives the error as a prop. This allows you to display more specific information about the error on a custom 500 page. | ||
|
||
```astro | ||
--- | ||
// src/pages/500.astro | ||
interface Props { | ||
error: unknown | ||
} | ||
const { error } = Astro.props | ||
--- | ||
<div>{error instanceof Error ? error.message : 'Unknown error'}</div> | ||
``` | ||
|
||
If an error occurs rendering this page, your host's default 500 error page will be shown to your visitor in production, and Astro's default error overlay will be shown in development. |
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
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
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,122 @@ | ||
import assert from 'node:assert/strict'; | ||
import { afterEach, describe, it } from 'node:test'; | ||
import * as cheerio from 'cheerio'; | ||
import { loadFixture } from './test-utils.js'; | ||
import testAdapter from './test-adapter.js'; | ||
import { renameSync } from 'node:fs'; | ||
|
||
describe('Custom 500', () => { | ||
/** @type {Awaited<ReturnType<typeof loadFixture>>} */ | ||
let fixture; | ||
|
||
describe('dev', () => { | ||
/** @type {Awaited<ReturnType<(typeof fixture)["startDevServer"]>>} */ | ||
let devServer; | ||
|
||
afterEach(async () => { | ||
await devServer?.stop(); | ||
try { | ||
renameSync( | ||
new URL('./fixtures/custom-500/src/pages/_500.astro', import.meta.url), | ||
new URL('./fixtures/custom-500/src/pages/500.astro', import.meta.url) | ||
); | ||
} catch (_) {} | ||
}); | ||
|
||
it('renders default error overlay', async () => { | ||
renameSync( | ||
new URL('./fixtures/custom-500/src/pages/500.astro', import.meta.url), | ||
new URL('./fixtures/custom-500/src/pages/_500.astro', import.meta.url) | ||
); | ||
fixture = await loadFixture({ | ||
root: './fixtures/custom-500/', | ||
output: 'server', | ||
adapter: testAdapter(), | ||
}); | ||
devServer = await fixture.startDevServer(); | ||
|
||
const response = await fixture.fetch('/'); | ||
assert.equal(response.status, 500); | ||
|
||
const html = await response.text(); | ||
|
||
assert.equal(html, '<title>Error</title><script type="module" src="/@vite/client"></script>'); | ||
}); | ||
|
||
it('renders custom 500', async () => { | ||
fixture = await loadFixture({ | ||
root: './fixtures/custom-500/', | ||
output: 'server', | ||
adapter: testAdapter(), | ||
}); | ||
devServer = await fixture.startDevServer(); | ||
|
||
const response = await fixture.fetch('/'); | ||
assert.equal(response.status, 500); | ||
|
||
const html = await response.text(); | ||
const $ = cheerio.load(html); | ||
|
||
assert.equal($('h1').text(), 'Server error'); | ||
assert.equal($('p').text(), 'some error'); | ||
}); | ||
|
||
it('renders default error overlay if custom 500 throws', async () => { | ||
fixture = await loadFixture({ | ||
root: './fixtures/custom-500-failing/', | ||
output: 'server', | ||
adapter: testAdapter(), | ||
}); | ||
devServer = await fixture.startDevServer(); | ||
|
||
const response = await fixture.fetch('/'); | ||
assert.equal(response.status, 500); | ||
|
||
const html = await response.text(); | ||
|
||
assert.equal(html, '<title>Error</title><script type="module" src="/@vite/client"></script>'); | ||
}); | ||
}); | ||
|
||
describe('SSR', () => { | ||
/** @type {Awaited<ReturnType<(typeof fixture)["loadTestAdapterApp"]>>} */ | ||
let app; | ||
|
||
it('renders custom 500', async () => { | ||
fixture = await loadFixture({ | ||
root: './fixtures/custom-500/', | ||
output: 'server', | ||
adapter: testAdapter(), | ||
}); | ||
await fixture.build(); | ||
app = await fixture.loadTestAdapterApp(); | ||
|
||
const request = new Request('http://example.com/'); | ||
const response = await app.render(request); | ||
assert.equal(response.status, 500); | ||
|
||
const html = await response.text(); | ||
const $ = cheerio.load(html); | ||
|
||
assert.equal($('h1').text(), 'Server error'); | ||
assert.equal($('p').text(), 'some error'); | ||
}); | ||
|
||
it('renders nothing if custom 500 throws', async () => { | ||
fixture = await loadFixture({ | ||
root: './fixtures/custom-500-failing/', | ||
output: 'server', | ||
adapter: testAdapter(), | ||
}); | ||
await fixture.build(); | ||
app = await fixture.loadTestAdapterApp(); | ||
|
||
const request = new Request('http://example.com/'); | ||
const response = await app.render(request); | ||
assert.equal(response.status, 500); | ||
|
||
const html = await response.text(); | ||
assert.equal(html, ''); | ||
}); | ||
}); | ||
}); |
4 changes: 4 additions & 0 deletions
4
packages/astro/test/fixtures/custom-500-failing/astro.config.mjs
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 @@ | ||
import { defineConfig } from 'astro/config'; | ||
|
||
// https://astro.build/config | ||
export default defineConfig({}); |
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,8 @@ | ||
{ | ||
"name": "@test/custom-500-failing", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"astro": "workspace:*" | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
packages/astro/test/fixtures/custom-500-failing/src/pages/500.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,19 @@ | ||
--- | ||
interface Props { | ||
error: unknown | ||
} | ||
const { error } = Astro.props | ||
throw "custom 500 fail" | ||
--- | ||
|
||
<html lang="en"> | ||
<head> | ||
<title>Server error - Custom 500</title> | ||
</head> | ||
<body> | ||
<h1>Server error</h1> | ||
<p>{error}</p> | ||
</body> | ||
</html> |
12 changes: 12 additions & 0 deletions
12
packages/astro/test/fixtures/custom-500-failing/src/pages/index.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,12 @@ | ||
--- | ||
throw "some error" | ||
--- | ||
|
||
<html lang="en"> | ||
<head> | ||
<title>Custom 500</title> | ||
</head> | ||
<body> | ||
<h1>Home</h1> | ||
</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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { defineConfig } from 'astro/config'; | ||
|
||
// https://astro.build/config | ||
export default defineConfig({}); |
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,8 @@ | ||
{ | ||
"name": "@test/custom-500", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"astro": "workspace:*" | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/astro/test/fixtures/custom-500/src/pages/500.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 @@ | ||
--- | ||
interface Props { | ||
error: unknown | ||
} | ||
const { error } = Astro.props | ||
--- | ||
|
||
<html lang="en"> | ||
<head> | ||
<title>Server error - Custom 500</title> | ||
</head> | ||
<body> | ||
<h1>Server error</h1> | ||
<p>{error}</p> | ||
</body> | ||
</html> |
12 changes: 12 additions & 0 deletions
12
packages/astro/test/fixtures/custom-500/src/pages/index.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,12 @@ | ||
--- | ||
throw "some error" | ||
--- | ||
|
||
<html lang="en"> | ||
<head> | ||
<title>Custom 500</title> | ||
</head> | ||
<body> | ||
<h1>Home</h1> | ||
</body> | ||
</html> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.