Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion runtime/src/server/middleware/get_page_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export function get_page_handler(
session: session && try_serialize(session, err => {
throw new Error(`Failed to serialize session data: ${err.message}`);
}),
error: error && try_serialize(props.error)
error: error && serialize_error(props.error)
};

let script = `__SAPPER__={${[
Expand Down Expand Up @@ -366,6 +366,20 @@ function try_serialize(data: any, fail?: (err) => void) {
}
}

// Ensure we return something truthy so the client will not re-render the page over the error
function serialize_error(error: Error | { message: string }) {
if (!error) return null;
let serialized = try_serialize(error);
if (!serialized) {
const { name, message, stack } = error as Error;
serialized = try_serialize({ name, message, stack });
}
if (!serialized) {
serialized = '{}';
}
return serialized;
}

function escape_html(html: string) {
const chars: Record<string, string> = {
'"' : 'quot',
Expand Down
3 changes: 2 additions & 1 deletion test/apps/errors/src/routes/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

<a href="nope">nope</a>
<a href="blog/nope">blog/nope</a>
<a href="throw">throw</a>
<a href="throw">throw</a>
<a href="preload-reject">preload-reject</a>
7 changes: 7 additions & 0 deletions test/apps/errors/src/routes/preload-reject.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script context="module">
export function preload() {
return Promise.reject(new Error('boom'))
}
</script>

<h1>Test has failed</h1>
10 changes: 10 additions & 0 deletions test/apps/errors/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ describe('errors', function() {
);
});

it('does not replace server side rendered error', async () => {
await r.load('/preload-reject');
await r.sapper.start();

assert.equal(
await r.text('h1'),
'500'
);
});

it('does not serve error page for explicit non-page errors', async () => {
await r.load('/nope.json');

Expand Down