feat(dev-tools): add switch user option#142
Conversation
BastiDood
left a comment
There was a problem hiding this comment.
Just a few more nitpicks and correctness issues.
| if (id === null) { | ||
| logger.error('failed to switch to another user'); | ||
| error(404); |
There was a problem hiding this comment.
🔴 Server uses error(404) instead of fail(404), so the client-side 'failure' toast is never triggered
When the target user email is not found, the server action calls error(404) at src/routes/dashboard/+page.server.js:316. In SvelteKit, error() throws an HttpError that produces result.type === 'error' in the use:enhance callback. However, the client-side form handler at src/lib/features/dev-tools/user-switcher/form.svelte:36 checks for result.type === 'failure' to display the toast.
Root Cause and Impact
SvelteKit distinguishes between two server-side error mechanisms:
error(code)→result.type === 'error'→ renders+error.sveltepagefail(code)→result.type === 'failure'→ stays on the page, accessible in the enhance callback
The form handler expects 'failure':
case 'failure':
toast.error('Failed to switch to user.');
break;But the server throws error(404), which produces 'error', falling through to the default: break case. Combined with await update() on line 30, this navigates the user to the error page instead of showing a friendly toast. The codebase itself uses fail() elsewhere for recoverable business-logic errors (e.g., src/routes/dashboard/users/+page.server.js:93).
Impact: When a developer enters a non-existent email in the "Switch to Another User" dialog, they are unexpectedly taken to a 404 error page instead of seeing a toast notification.
Prompt for agents
In src/routes/dashboard/+page.server.js, two changes are needed:
1. On line 3, add `fail` to the import from '@sveltejs/kit':
Change: import { error, redirect } from '@sveltejs/kit';
To: import { error, fail, redirect } from '@sveltejs/kit';
2. On line 316, change `error(404)` to `return fail(404)` so the client-side enhance callback receives `result.type === 'failure'` and can display the toast:
Change: error(404);
To: return fail(404);
Was this helpful? React with 👍 or 👎 to provide feedback.
BastiDood
left a comment
There was a problem hiding this comment.
Almost there! Sending in my preemptive approval.
This PR adds the "Switch to Another User" option in the Developer Tools.