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

feat: show vite error overlay for unhandled errors during load #9733

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
22 changes: 21 additions & 1 deletion packages/kit/src/exports/vite/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,27 @@ export async function dev(vite, vite_config, svelte_config) {
const { set_assets } = await vite.ssrLoadModule('__sveltekit/paths');
set_assets(assets);

const server = new Server(manifest);
const server = new Server(
{
manifest
},
// @ts-expect-error internal second argument
/** @type {import('types').ServerDevOptions} */ ({
on_error: (route_id, e) => {
const error = coalesce_to_error(e);
// In a timeout, else might be sent too soon before SvelteKit reloads and therefore "deletes" the dialog
setTimeout(() => {
vite.ws.send({
type: 'error',
err: {
message: `Error while loading data for route ${route_id}: ${error.message}`,
stack: error.stack || ''
}
});
}, 500);
}
})
);

await server.init({ env });

Expand Down
19 changes: 17 additions & 2 deletions packages/kit/src/runtime/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ export class Server {
/** @type {import('types').SSRManifest} */
#manifest;

/** @param {import('types').SSRManifest} manifest */
constructor(manifest) {
/** @type {import('types').ServerDevOptions | undefined} */
#dev;

/**
* @param {import('types').SSRManifest} manifest
* @param {import('types').ServerDevOptions} [dev]
*/
constructor(manifest, dev) {
/** @type {import('types').SSROptions} */
this.#options = options;
this.#manifest = manifest;
this.#dev = dev;
}

/**
Expand Down Expand Up @@ -45,6 +52,14 @@ export class Server {
handleError: module.handleError || (({ error }) => console.error(error?.stack)),
handleFetch: module.handleFetch || (({ request, fetch }) => fetch(request))
};
if (DEV && this.#dev) {
const handle_error = this.#options.hooks.handleError;
const on_error = this.#dev.on_error;
this.#options.hooks.handleError = (event) => {
on_error(event.event.route.id, event.error);
return handle_error(event);
};
}
} catch (error) {
if (DEV) {
this.#options.hooks = {
Expand Down
4 changes: 4 additions & 0 deletions packages/kit/types/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,10 @@ export interface SSROptions {
version_hash: string;
}

export interface ServerDevOptions {
on_error: (route_id: string | null, error: unknown) => void;
}

export interface PageNodeIndexes {
errors: Array<number | undefined>;
layouts: Array<number | undefined>;
Expand Down