Skip to content

feat: Improve server error logging #13990

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hamboomger
Copy link
Contributor

@hamboomger hamboomger commented Jul 12, 2025

Partly resolves #13862.

This PR sanitizes stack trace for server errors by finding the last occurrence of "+page.", "+server.", or "+layout." in the stack trace, and removing everything that comes after that line.

Error logs on the server before:

Error: test
    at load (/Users/hamboomger/apps/selectra/src/routes/admin/(app)/normalize-html/+page.server.ts:2:11)
    at Module.load_server_data (/Users/hamboomger/apps/selectra/node_modules/@sveltejs/kit/src/runtime/server/page/load_data.js:61:41)
    at /Users/hamboomger/apps/selectra/node_modules/@sveltejs/kit/src/runtime/server/data/index.js:62:13
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async Promise.all (index 3)
    at async Module.render_data (/Users/hamboomger/apps/selectra/node_modules/@sveltejs/kit/src/runtime/server/data/index.js:99:17)
    at async resolve (/Users/hamboomger/apps/selectra/node_modules/@sveltejs/kit/src/runtime/server/respond.js:441:17)
    at async Module.respond (/Users/hamboomger/apps/selectra/node_modules/@sveltejs/kit/src/runtime/server/respond.js:327:20)
    at async file:///Users/hamboomger/apps/selectra/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:525:22

After:

[500] POST /force-error
Error: test
    at load (/Users/hamboomger/apps/selectra/src/routes/admin/(app)/normalize-html/+page.server.ts:2:11)

I also colored the top line red, because why not:

image image

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

As for the tests, I couldn't find a way to write Playwrite tests for the server logs. I dug up some similar tests, but they only test the error object itself, not the server log messages.

It seems like you can't easily listen on the server logs in Playwright, so if we really need it, I'd have to intercept the console.log(...) statements with something like this:

  const originalError = console.error;
  console.error = (...args) => {
    globalThis.__collected_errors = globalThis.__collected_errors || [];
    globalThis.__collected_errors.push(args);
    originalError(...args);
  };
  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

Copy link

changeset-bot bot commented Jul 12, 2025

⚠️ No Changeset found

Latest commit: f878bdb

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@hamboomger
Copy link
Contributor Author

Please tell me if I should delete the changes from the playground directory 🙂

@svelte-docs-bot
Copy link

@benmccann
Copy link
Member

Can you please update the PR title to more specifically describe how error logging is being improved?

@hamboomger
Copy link
Contributor Author

@benmccann yes, of course, sorry 😅 I just added some extra context.

@hamboomger hamboomger force-pushed the improve_server_error_logging branch from 8ba9932 to 96cc344 Compare July 15, 2025 10:05
@hamboomger hamboomger marked this pull request as draft July 15, 2025 10:05
@hamboomger hamboomger force-pushed the improve_server_error_logging branch 2 times, most recently from f387b9d to 8ba9932 Compare July 15, 2025 10:07
@hamboomger hamboomger force-pushed the improve_server_error_logging branch from 8ba9932 to f878bdb Compare July 15, 2025 10:09
@hamboomger hamboomger marked this pull request as ready for review July 15, 2025 10:30
@hamboomger hamboomger changed the title Improve server error logging feat: Improve server error logging Jul 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

improve server-side error logging
2 participants