Skip to content

Commit 2244e33

Browse files
committed
Fix SvelteKit error handling and refresh dependencies
1 parent 4898672 commit 2244e33

8 files changed

Lines changed: 97 additions & 30 deletions

File tree

‎.agents/skills/exceptionless-javascript/references/client-node.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ import { Exceptionless } from "@exceptionless/node";
6767
await Exceptionless.processQueue();
6868
```
6969
70+
## SvelteKit 3
71+
72+
Configure the adapter through the `sveltekit` plugin in `vite.config.js`; hook types are exported from `@sveltejs/kit/hooks`. SvelteKit 3 calls error hooks for expected application and framework errors as well as unexpected errors, so inspect `kind` and report `unknown` errors as exceptions when preserving the previous behavior. Use `@exceptionless/browser` in `hooks.client.js` and `@exceptionless/node` in `hooks.server.js`. The complete example is in `example/svelte-kit`.
73+
7074
## Source Anchors
7175
7276
- `packages/node/README.md`

‎example/svelte-kit/README.md‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
## Exceptionless Svelte Kit Example
22

3-
This example shows how to use the `@exceptionless/browser` package for client side Svelte Kit and `@exceptionless/node` for server side Svelte Kit. These is both
4-
a client side error hook `hooks.client.js` and a server side error hook `hooks.server.js`.
3+
This example uses `@exceptionless/browser` in the client and `@exceptionless/node` on the server. SvelteKit 3 passes errors to `hooks.client.js` and `hooks.server.js`; the hooks report unexpected errors and leave expected application, framework, and validation errors to SvelteKit.
4+
5+
SvelteKit 3 requires Node.js 22.17 or later. The adapter is configured through `sveltekit({ adapter: adapter() })` in `vite.config.js`, and the hook types come from `@sveltejs/kit/hooks`. See the [SvelteKit 3 migration guide](https://svelte.dev/docs/kit/migrating-to-sveltekit-3) for other requirements.
56

67
To run locally, follow these steps:
78

89
1. `git clone https://github.com/exceptionless/Exceptionless.JavaScript`
9-
2. `cd Exceptionless.Javascript`
10-
3. `npm install`
11-
4. `cd example/svelte-kit`
12-
5. `npm run dev -- --open`
10+
2. `cd Exceptionless.JavaScript`
11+
3. `npm ci`
12+
4. `npm run build`
13+
5. `npm run dev --workspace=example/svelte-kit -- --open`
14+
15+
The sample is configured for a local test server. Update its API key and server URL in the client and server hooks to send events to your own instance.

‎example/svelte-kit/package.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
"@exceptionless/browser": "3.0.0-dev"
1414
},
1515
"devDependencies": {
16-
"@sveltejs/adapter-auto": "^8.0.0",
17-
"@sveltejs/kit": "^3.1.0",
16+
"@sveltejs/adapter-auto": "^9.0.0",
17+
"@sveltejs/kit": "^3.4.0",
1818
"@sveltejs/vite-plugin-svelte": "^7.3.1",
19-
"cookie": "^2.0.1",
19+
"cookie": "^2.0.2",
2020
"svelte": "^5.57.1",
2121
"svelte-check": "^4.7.6",
2222
"typescript": "^6.0.3",

‎example/svelte-kit/src/hooks.client.js‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ Exceptionless.startup((c) => {
1010

1111
/** @type {import('@sveltejs/kit/hooks').HandleClientError} */
1212
export const handleError = async ({ error, event, kind }) => {
13-
console.warn({ error, event, kind, source: "client error handler" });
14-
const builder = Exceptionless.createException(toError(error));
15-
if (kind !== "unknown" && error.status != null) {
16-
builder.setProperty("status", error.status);
13+
if (kind !== "unknown") {
14+
return;
1715
}
18-
await builder.submit();
16+
console.warn({ error, event, kind, source: "client error handler" });
17+
await Exceptionless.submitException(toError(error));
1918
};

‎example/svelte-kit/src/hooks.server.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ Exceptionless.startup((c) => {
99
});
1010

1111
/** @type {import("@sveltejs/kit/hooks").HandleServerError} */
12-
export const handleError = async ({ error, event }) => {
12+
export const handleError = async ({ error, kind }) => {
13+
if (kind !== "unknown") {
14+
return;
15+
}
1316
console.log("server error handler");
1417
await Exceptionless.submitException(toError(error));
1518
};
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { beforeEach, describe, expect, test, vi } from "vitest";
2+
3+
const mocks = vi.hoisted(() => ({
4+
startup: vi.fn(),
5+
clientSubmitException: vi.fn(),
6+
serverSubmitException: vi.fn()
7+
}));
8+
9+
vi.mock("@exceptionless/browser", () => ({
10+
Exceptionless: { startup: mocks.startup, submitException: mocks.clientSubmitException },
11+
toError: (/** @type {unknown} */ error) => error
12+
}));
13+
vi.mock("@exceptionless/node", () => ({
14+
Exceptionless: { startup: mocks.startup, submitException: mocks.serverSubmitException },
15+
toError: (/** @type {unknown} */ error) => error
16+
}));
17+
18+
import { handleError as handleClientError } from "../src/hooks.client.js";
19+
import { handleError as handleServerError } from "../src/hooks.server.js";
20+
21+
describe("SvelteKit error hooks", () => {
22+
beforeEach(() => {
23+
vi.clearAllMocks();
24+
mocks.clientSubmitException.mockResolvedValue(undefined);
25+
mocks.serverSubmitException.mockResolvedValue(undefined);
26+
});
27+
28+
test.each(["app", "framework"])("does not submit expected %s errors from either hook", async (kind) => {
29+
const input = { kind, error: { status: 404, message: "Not found" }, event: {} };
30+
await handleClientError(/** @type {Parameters<typeof handleClientError>[0]} */ (/** @type {unknown} */ (input)));
31+
await handleServerError(/** @type {Parameters<typeof handleServerError>[0]} */ (/** @type {unknown} */ (input)));
32+
expect(mocks.clientSubmitException).not.toHaveBeenCalled();
33+
expect(mocks.serverSubmitException).not.toHaveBeenCalled();
34+
});
35+
36+
test("does not submit server validation errors", async () => {
37+
const input = { kind: "validation", error: { status: 400, message: "Invalid" }, issues: [], event: {} };
38+
await handleServerError(/** @type {Parameters<typeof handleServerError>[0]} */ (/** @type {unknown} */ (input)));
39+
expect(mocks.serverSubmitException).not.toHaveBeenCalled();
40+
});
41+
42+
test("submits unexpected errors from both hooks", async () => {
43+
const error = new Error("Unexpected failure");
44+
const input = { kind: "unknown", error, event: {} };
45+
await handleClientError(/** @type {Parameters<typeof handleClientError>[0]} */ (/** @type {unknown} */ (input)));
46+
await handleServerError(/** @type {Parameters<typeof handleServerError>[0]} */ (/** @type {unknown} */ (input)));
47+
expect(mocks.clientSubmitException).toHaveBeenCalledWith(error);
48+
expect(mocks.serverSubmitException).toHaveBeenCalledWith(error);
49+
});
50+
});

‎package-lock.json‎

Lines changed: 16 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vitest.config.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ export default defineConfig({
106106
root: "example/nextjs",
107107
environment: "node"
108108
}
109+
},
110+
{
111+
test: {
112+
name: "svelte-kit-example",
113+
root: "example/svelte-kit",
114+
environment: "node"
115+
}
109116
}
110117
]
111118
}

0 commit comments

Comments
 (0)