Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made error page work #108

Merged
merged 3 commits into from
Nov 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
65 changes: 17 additions & 48 deletions packages/app-utils/src/renderer/render/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ type FetchOpts = {
export default async function render_page(
request: IncomingRequest,
context: any,
options: RenderOptions
options: RenderOptions,
status: number = 200,
error: Error | null = null
): Promise<{
status: number,
body: string,
Expand Down Expand Up @@ -192,14 +194,15 @@ export default async function render_page(
});

const props: Record<string, any> = {
status: 200,
error: null,
status,
error,
stores: {
page: readable({
host: request.host,
path: request.path,
query: request.query,
params
params,
error
}, noop),
preloading: readable(null, noop),
session: writable(session)
Expand Down Expand Up @@ -266,8 +269,8 @@ export default async function render_page(
<script>
__SVELTE__ = {
baseUrl: "${baseUrl}",
status: 200,
error: null,
status: ${status},
error: ${serialize_error(error)},
preloaded: ${serialized_preloads},
session: ${serialized_session}
};
Expand All @@ -283,52 +286,18 @@ export default async function render_page(
body: html,
dependencies
};
} catch (error) {
console.error(error.stack);

const status = error.status || 500;

try {
const rendered = options.root.default.render({ status, error });

const head = `${rendered.head}
<script type="module">
import { start } from '/_app/${options.client.entry}';
} catch (thrown) {
console.error(thrown.stack);

start({
target: document.body
});
</script>`.replace(/^\t\t\t/gm, '');

const body = `${rendered.html}
<script>
__SVELTE__ = {
baseUrl: "${baseUrl}",
status: ${status},
error: ${serialize_error(error)},
preloaded: null,
session: ${serialized_session}
};
</script>`.replace(/^\t\t\t/gm, '');

const html = options.template
.replace('%svelte.head%', head)
.replace('%svelte.body%', body);

return {
status,
headers: {
'content-type': 'text/html'
},
body: html,
dependencies: {}
};
} catch (error) {
if (!error) {
const status = thrown.status || 500;
return render_page(request, context, options, status, thrown);
} else {
// oh lawd now you've done it
return {
status: 500,
headers: {},
body: error.stack, // TODO probably not in prod?
body: thrown.stack, // TODO probably not in prod?
dependencies: {}
};
}
Expand All @@ -345,7 +314,7 @@ function try_serialize(data: any, fail?: (err: Error) => void) {
}

// Ensure we return something truthy so the client will not re-render the page over the error
function serialize_error(error: Error) {
function serialize_error(error?: Error) {
if (!error) return null;
let serialized = try_serialize(error);
if (!serialized) {
Expand Down