Skip to content

Commit

Permalink
fix: correct line numbers when console logging stack traces (#10769)
Browse files Browse the repository at this point in the history
Co-authored-by: Nayan Gautam <[email protected]>
  • Loading branch information
eltigerchino and gtm-nayan authored Sep 24, 2023
1 parent f9dbd96 commit 6089bc1
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-panthers-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: correct line numbers in stack trace
22 changes: 13 additions & 9 deletions packages/kit/src/exports/vite/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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));
}
});

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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));
}
});
};
Expand Down
10 changes: 1 addition & 9 deletions packages/kit/src/runtime/server/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
6 changes: 3 additions & 3 deletions packages/kit/src/runtime/shared-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ export let private_env = {};
/** @type {Record<string, string>} */
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<string, string>) => void} */
export function set_private_env(environment) {
Expand All @@ -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;
}
2 changes: 1 addition & 1 deletion packages/kit/src/types/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface ServerInternalModule {
set_private_env(environment: Record<string, string>): void;
set_public_env(environment: Record<string, string>): 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 {
Expand Down

0 comments on commit 6089bc1

Please sign in to comment.