Skip to content

Commit

Permalink
fix: avoid suppressing errors when loading pages for ssr fails (#8813)
Browse files Browse the repository at this point in the history
* fix: output errors if pages fail to compile

* stupid javascript try catch syntax

* changeset

* less blatant copy paste, more code reuse

* use superior casing and wrap a few more places

* why did I change this
  • Loading branch information
gtm-nayan committed Feb 1, 2023
1 parent 8b0b92b commit 33a8fad
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/curly-jobs-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: output errors properly if pages fail to compile
22 changes: 18 additions & 4 deletions packages/kit/src/exports/vite/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'node:path';
import { URL } from 'node:url';
import colors from 'kleur';
import sirv from 'sirv';
import { isCSSRequest, loadEnv } from 'vite';
import { isCSSRequest, loadEnv, buildErrorMessage } from 'vite';
import { getRequest, setResponse } from '../../../exports/node/index.js';
import { installPolyfills } from '../../../exports/node/polyfills.js';
import { coalesce_to_error } from '../../../utils/error.js';
Expand Down Expand Up @@ -50,11 +50,25 @@ export async function dev(vite, vite_config, svelte_config) {
/** @type {Error | null} */
let manifest_error = null;

/** @param {string} url */
async function loud_ssr_load_module(url) {
try {
return await vite.ssrLoadModule(url);
} catch (/** @type {any} */ err) {
const msg = buildErrorMessage(err, [colors.red(`Internal server error: ${err.message}`)]);

vite.config.logger.error(msg, { error: err });
vite.ws.send({ type: 'error', err: err });

throw err;
}
}

/** @param {string} id */
async function resolve(id) {
const url = id.startsWith('..') ? `/@fs${path.posix.resolve(id)}` : `/${id}`;

const module = await vite.ssrLoadModule(url);
const module = await loud_ssr_load_module(url);

const module_node = await vite.moduleGraph.getModuleByUrl(url);
if (!module_node) throw new Error(`Could not find node for ${url}`);
Expand Down Expand Up @@ -161,7 +175,7 @@ export async function dev(vite, vite_config, svelte_config) {
(query.has('svelte') && query.get('type') === 'style')
) {
try {
const mod = await vite.ssrLoadModule(dep.url);
const mod = await loud_ssr_load_module(dep.url);
styles[dep.url] = mod.default;
} catch {
// this can happen with dynamically imported modules, I think
Expand Down Expand Up @@ -191,7 +205,7 @@ export async function dev(vite, vite_config, svelte_config) {
endpoint: endpoint
? async () => {
const url = path.resolve(cwd, endpoint.file);
return await vite.ssrLoadModule(url);
return await loud_ssr_load_module(url);
}
: null,
endpoint_id: endpoint?.file
Expand Down

0 comments on commit 33a8fad

Please sign in to comment.