From 6089bc183dbaa7cb4bf4359dfb52ed85ac60d8b2 Mon Sep 17 00:00:00 2001 From: Tee Ming Date: Mon, 25 Sep 2023 01:26:41 +0800 Subject: [PATCH] fix: correct line numbers when console logging stack traces (#10769) Co-authored-by: Nayan Gautam --- .changeset/clean-panthers-remain.md | 5 +++++ packages/kit/src/exports/vite/dev/index.js | 22 +++++++++++++--------- packages/kit/src/runtime/server/utils.js | 10 +--------- packages/kit/src/runtime/shared-server.js | 6 +++--- packages/kit/src/types/internal.d.ts | 2 +- 5 files changed, 23 insertions(+), 22 deletions(-) create mode 100644 .changeset/clean-panthers-remain.md diff --git a/.changeset/clean-panthers-remain.md b/.changeset/clean-panthers-remain.md new file mode 100644 index 000000000000..80ce832da8ef --- /dev/null +++ b/.changeset/clean-panthers-remain.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +fix: correct line numbers in stack trace diff --git a/packages/kit/src/exports/vite/dev/index.js b/packages/kit/src/exports/vite/dev/index.js index d8e2c7b40102..a70be05c9cc8 100644 --- a/packages/kit/src/exports/vite/dev/index.js +++ b/packages/kit/src/exports/vite/dev/index.js @@ -54,11 +54,14 @@ export async function dev(vite, vite_config, svelte_config) { /** @param {string} url */ async function loud_ssr_load_module(url) { try { - return await vite.ssrLoadModule(url); + return await vite.ssrLoadModule(url, { fixStacktrace: true }); } catch (/** @type {any} */ err) { const msg = buildErrorMessage(err, [colors.red(`Internal server error: ${err.message}`)]); - vite.config.logger.error(msg, { error: err }); + if (!vite.config.logger.hasErrorLogged(err)) { + vite.config.logger.error(msg, { error: err }); + } + vite.ws.send({ type: 'error', err: { @@ -233,7 +236,7 @@ export async function dev(vite, vite_config, svelte_config) { for (const key in manifest_data.matchers) { const file = manifest_data.matchers[key]; const url = path.resolve(cwd, file); - const module = await vite.ssrLoadModule(url); + const module = await vite.ssrLoadModule(url, { fixStacktrace: true }); if (module.match) { matchers[key] = module.match; @@ -248,9 +251,10 @@ export async function dev(vite, vite_config, svelte_config) { }; } - /** @param {string} stack */ - function fix_stack_trace(stack) { - return stack ? vite.ssrRewriteStacktrace(stack) : stack; + /** @param {Error} error */ + function fix_stack_trace(error) { + vite.ssrFixStacktrace(error); + return error.stack; } await update_manifest(); @@ -393,7 +397,7 @@ export async function dev(vite, vite_config, svelte_config) { } catch (e) { const error = coalesce_to_error(e); res.statusCode = 500; - res.end(fix_stack_trace(/** @type {string} */ (error.stack))); + res.end(fix_stack_trace(error)); } }); @@ -454,7 +458,7 @@ export async function dev(vite, vite_config, svelte_config) { // we have to import `Server` before calling `set_assets` const { Server } = /** @type {import('types').ServerModule} */ ( - await vite.ssrLoadModule(`${runtime_base}/server/index.js`) + await vite.ssrLoadModule(`${runtime_base}/server/index.js`, { fixStacktrace: true }) ); const { set_fix_stack_trace } = await vite.ssrLoadModule( @@ -523,7 +527,7 @@ export async function dev(vite, vite_config, svelte_config) { } catch (e) { const error = coalesce_to_error(e); res.statusCode = 500; - res.end(fix_stack_trace(/** @type {string} */ (error.stack))); + res.end(fix_stack_trace(error)); } }); }; diff --git a/packages/kit/src/runtime/server/utils.js b/packages/kit/src/runtime/server/utils.js index ec5831b7e4de..6e5a4ee1b6ad 100644 --- a/packages/kit/src/runtime/server/utils.js +++ b/packages/kit/src/runtime/server/utils.js @@ -99,15 +99,7 @@ export async function handle_error_and_jsonify(event, options, error) { return error.body; } else { if (__SVELTEKIT_DEV__ && typeof error == 'object') { - error = new Proxy(error, { - get: (target, property) => { - if (property === 'stack') { - return fix_stack_trace(target.stack); - } - - return Reflect.get(target, property, target); - } - }); + fix_stack_trace(error); } return ( diff --git a/packages/kit/src/runtime/shared-server.js b/packages/kit/src/runtime/shared-server.js index ed1c5efcb472..778e31aad481 100644 --- a/packages/kit/src/runtime/shared-server.js +++ b/packages/kit/src/runtime/shared-server.js @@ -4,8 +4,8 @@ export let private_env = {}; /** @type {Record} */ export let public_env = {}; -/** @param {string} stack */ -export let fix_stack_trace = (stack) => stack; +/** @param {any} error */ +export let fix_stack_trace = (error) => error?.stack; /** @type {(environment: Record) => void} */ export function set_private_env(environment) { @@ -17,7 +17,7 @@ export function set_public_env(environment) { public_env = environment; } -/** @param {(stack: string) => string} value */ +/** @param {(error: Error) => string} value */ export function set_fix_stack_trace(value) { fix_stack_trace = value; } diff --git a/packages/kit/src/types/internal.d.ts b/packages/kit/src/types/internal.d.ts index 5dba7f1aa8d0..84ffcd4bc4ef 100644 --- a/packages/kit/src/types/internal.d.ts +++ b/packages/kit/src/types/internal.d.ts @@ -32,7 +32,7 @@ export interface ServerInternalModule { set_private_env(environment: Record): void; set_public_env(environment: Record): void; set_version(version: string): void; - set_fix_stack_trace(fix_stack_trace: (stack: string) => string): void; + set_fix_stack_trace(fix_stack_trace: (error: unknown) => string): void; } export interface Asset {