Skip to content

Commit 53539be

Browse files
committed
Report SvelteKit errors without changing core logging
1 parent c04b086 commit 53539be

12 files changed

Lines changed: 730 additions & 86 deletions

File tree

‎.agents/skills/exceptionless-javascript/SKILL.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ Read only the references needed for the request. Start with the runtime for setu
1515

1616
- `@exceptionless/core`: [references/client-core.md](references/client-core.md)
1717
- `@exceptionless/browser`: [references/client-browser.md](references/client-browser.md)
18-
- `@exceptionless/node`, Express, Next.js server, and SvelteKit server: [references/client-node.md](references/client-node.md)
18+
- `@exceptionless/node`, Express, and Next.js server: [references/client-node.md](references/client-node.md)
19+
- SvelteKit client and server: [references/client-sveltekit.md](references/client-sveltekit.md)
1920
- `@exceptionless/react`: [references/client-react.md](references/client-react.md)
2021
- `@exceptionless/react-native` and Expo: [references/client-react-native.md](references/client-react-native.md)
2122
- `@exceptionless/vue`: [references/client-vue.md](references/client-vue.md)

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# @exceptionless/node
22

3-
Use for Node.js scripts, CLIs, workers, Express, Next.js server runtime, SvelteKit server hooks, and serverless functions.
3+
Use for Node.js scripts, CLIs, workers, Express, Next.js server runtime, and serverless functions.
44

55
Docs:
66

@@ -67,10 +67,6 @@ 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`. Keep the client hook synchronous when it reports in the background so it also works during rendering. The complete example is in `example/svelte-kit`.
73-
7470
## Source Anchors
7571
7672
- `packages/node/README.md`
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# SvelteKit
2+
3+
Use the browser client in `hooks.client.js` and the Node client in `hooks.server.js` when running on Node. The complete, executable integration is in `example/svelte-kit`; configure the API key and server URL for your own installation.
4+
5+
SvelteKit 3 invokes `handleError` for application, framework, validation, and unknown errors. The example reports all of them to Exceptionless: `unknown` errors use `createUnhandledException`, while the expected categories use `createException` with their HTTP status. The SDK marks unhandled errors in the client-side event context and adds the submission method to event data; the context flag itself is not serialized. Validation issues are not sent by the example. Check your data-exclusion and privacy requirements before adding request details or custom error data.
6+
7+
The client and server hook types are exported by `@sveltejs/kit/hooks`. The example awaits reporting and catches submission failures so the hook does not throw; it enables `compilerOptions.experimental.async` in the SvelteKit Vite plugin so asynchronous client error hooks can be awaited during rendering. This option is experimental.
8+
9+
SvelteKit does not pass an error already handled on the server to the client hook again. The SDK also installs global browser error and rejection handlers on startup; errors outside SvelteKit can reach those handlers. Avoid reporting the same error manually in multiple places.
10+
11+
- [SvelteKit hooks](https://svelte.dev/docs/kit/hooks#handleError)
12+
- [SvelteKit 3 migration guide](https://svelte.dev/docs/kit/migrating-to-sveltekit-3)
13+
- `example/svelte-kit/src/hooks.client.js`
14+
- `example/svelte-kit/src/hooks.server.js`
15+
- `example/svelte-kit/vite.config.js`

‎example/svelte-kit/README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
## Exceptionless SvelteKit Example
22

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.
3+
This example uses `@exceptionless/browser` in the client and `@exceptionless/node` on the server. SvelteKit 3 passes application, framework, validation, and unknown errors to `hooks.client.js` and `hooks.server.js`. The hooks report all of them to Exceptionless, marking unknown errors as unhandled and preserving the HTTP status for expected errors.
44

5-
SvelteKit 3 requires Node.js 22.17 or later. The adapter is configured through `sveltekit({ adapter: adapter() })` in `vite.config.js`, `jsconfig.json` extends `$app/tsconfig`, 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.
5+
SvelteKit 3 requires Node.js 22.17 or later. The adapter is configured in `vite.config.js`, `jsconfig.json` extends `$app/tsconfig`, and the hook types come from `@sveltejs/kit/hooks`. The Vite configuration enables Svelte's experimental async compiler option so the client error hook can be awaited during rendering. See the [SvelteKit 3 migration guide](https://svelte.dev/docs/kit/migrating-to-sveltekit-3) for other requirements.
66

77
To run locally, follow these steps:
88

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,17 @@ Exceptionless.startup((c) => {
99
});
1010

1111
/** @type {import("@sveltejs/kit/hooks").HandleClientError} */
12-
export const handleError = ({ error, kind }) => {
13-
if (kind === "unknown") {
14-
Exceptionless.submitException(toError(error)).catch(console.error);
12+
export const handleError = async ({ error, event, kind }) => {
13+
console.warn({ error, event, kind, source: "client error handler" });
14+
try {
15+
const builder =
16+
kind === "unknown" ? Exceptionless.createUnhandledException(toError(error), "sveltekit.client") : Exceptionless.createException(toError(error.message));
17+
builder.setProperty("sveltekit.kind", kind);
18+
if (kind !== "unknown") {
19+
builder.setProperty("status", error.status);
20+
}
21+
await builder.submit();
22+
} catch (submissionError) {
23+
console.error("Exceptionless failed to report the client error", submissionError);
1524
}
1625
};

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,16 @@ Exceptionless.startup((c) => {
1010

1111
/** @type {import("@sveltejs/kit/hooks").HandleServerError} */
1212
export const handleError = async ({ error, kind }) => {
13-
if (kind === "unknown") {
14-
await Exceptionless.submitException(toError(error)).catch(console.error);
13+
console.log("server error handler");
14+
try {
15+
const builder =
16+
kind === "unknown" ? Exceptionless.createUnhandledException(toError(error), "sveltekit.server") : Exceptionless.createException(toError(error.message));
17+
builder.setProperty("sveltekit.kind", kind);
18+
if (kind !== "unknown") {
19+
builder.setProperty("status", error.status);
20+
}
21+
await builder.submit();
22+
} catch (submissionError) {
23+
console.error("Exceptionless failed to report the server error", submissionError);
1524
}
1625
};
Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,81 @@
1-
import { beforeEach, describe, expect, test, vi } from "vitest";
1+
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
22

3-
const mocks = vi.hoisted(() => ({
4-
startup: vi.fn(),
5-
clientSubmitException: vi.fn(),
6-
serverSubmitException: vi.fn()
7-
}));
3+
const mocks = vi.hoisted(() => {
4+
const client = { startup: vi.fn(), createException: vi.fn(), createUnhandledException: vi.fn(), submitException: vi.fn() };
5+
const server = { startup: vi.fn(), createException: vi.fn(), createUnhandledException: vi.fn(), submitException: vi.fn() };
6+
const builder = { setProperty: vi.fn(), submit: vi.fn() };
7+
return { client, server, builder };
8+
});
89

910
vi.mock("@exceptionless/browser", () => ({
10-
Exceptionless: { startup: mocks.startup, submitException: mocks.clientSubmitException },
11-
toError: (/** @type {unknown} */ error) => error
11+
Exceptionless: mocks.client,
12+
toError: (/** @type {unknown} */ value) => (value instanceof Error ? value : new Error(String(value)))
1213
}));
1314
vi.mock("@exceptionless/node", () => ({
14-
Exceptionless: { startup: mocks.startup, submitException: mocks.serverSubmitException },
15-
toError: (/** @type {unknown} */ error) => error
15+
Exceptionless: mocks.server,
16+
toError: (/** @type {unknown} */ value) => (value instanceof Error ? value : new Error(String(value)))
1617
}));
1718

1819
import { handleError as handleClientError } from "../src/hooks.client.js";
1920
import { handleError as handleServerError } from "../src/hooks.server.js";
2021

22+
/** @param {unknown} input */
23+
const runClient = (input) => handleClientError(/** @type {Parameters<typeof handleClientError>[0]} */ (input));
24+
/** @param {unknown} input */
25+
const runServer = (input) => handleServerError(/** @type {Parameters<typeof handleServerError>[0]} */ (input));
26+
2127
describe("SvelteKit error hooks", () => {
2228
beforeEach(() => {
2329
vi.clearAllMocks();
24-
mocks.clientSubmitException.mockResolvedValue(undefined);
25-
mocks.serverSubmitException.mockResolvedValue(undefined);
30+
vi.spyOn(console, "warn").mockImplementation(() => {});
31+
vi.spyOn(console, "log").mockImplementation(() => {});
32+
vi.spyOn(console, "error").mockImplementation(() => {});
33+
mocks.builder.setProperty.mockReturnValue(mocks.builder);
34+
mocks.builder.submit.mockResolvedValue(undefined);
35+
for (const client of [mocks.client, mocks.server]) {
36+
client.submitException.mockResolvedValue(undefined);
37+
client.createException.mockReturnValue(mocks.builder);
38+
client.createUnhandledException.mockReturnValue(mocks.builder);
39+
}
2640
});
2741

28-
test.each(["app", "framework"])("does not submit expected %s errors from either hook", async (kind) => {
42+
afterEach(() => vi.restoreAllMocks());
43+
44+
test.each(["app", "framework"])("reports %s errors as handled with their status", async (kind) => {
2945
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();
46+
await runClient(input);
47+
await runServer(input);
48+
for (const client of [mocks.client, mocks.server]) {
49+
expect(client.createException).toHaveBeenCalledWith(expect.objectContaining({ message: "Not found" }));
50+
expect(client.createUnhandledException).not.toHaveBeenCalled();
51+
}
52+
expect(mocks.builder.setProperty).toHaveBeenCalledWith("status", 404);
53+
expect(mocks.builder.submit).toHaveBeenCalledTimes(2);
54+
expect(console.warn).toHaveBeenCalled();
55+
expect(console.log).toHaveBeenCalled();
3456
});
3557

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();
58+
test("reports server validation errors as handled", async () => {
59+
await runServer({ kind: "validation", error: { status: 400, message: "Bad Request" }, issues: [], event: {} });
60+
expect(mocks.server.createException).toHaveBeenCalledWith(expect.objectContaining({ message: "Bad Request" }));
61+
expect(mocks.server.createUnhandledException).not.toHaveBeenCalled();
62+
expect(mocks.builder.setProperty).toHaveBeenCalledWith("status", 400);
63+
expect(mocks.builder.submit).toHaveBeenCalledOnce();
4064
});
4165

42-
test("submits unexpected errors from both hooks", async () => {
66+
test("reports unknown errors as unhandled", async () => {
4367
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);
68+
await runClient({ kind: "unknown", error, event: {} });
69+
await runServer({ kind: "unknown", error, event: {} });
70+
expect(mocks.client.createUnhandledException).toHaveBeenCalledWith(error, "sveltekit.client");
71+
expect(mocks.server.createUnhandledException).toHaveBeenCalledWith(error, "sveltekit.server");
72+
expect(mocks.builder.submit).toHaveBeenCalledTimes(2);
73+
});
74+
75+
test("does not throw when Exceptionless submission fails", async () => {
76+
mocks.builder.submit.mockRejectedValue(new Error("Storage unavailable"));
77+
await expect(runClient({ kind: "unknown", error: new Error("Failure"), event: {} })).resolves.toBeUndefined();
78+
await expect(runServer({ kind: "unknown", error: new Error("Failure"), event: {} })).resolves.toBeUndefined();
79+
expect(console.error).toHaveBeenCalledTimes(2);
4980
});
5081
});

‎example/svelte-kit/vite.config.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import { sveltekit } from "@sveltejs/kit/vite";
33
import { defineConfig } from "vite";
44

55
export default defineConfig({
6-
plugins: [sveltekit({ adapter: adapter() })]
6+
plugins: [sveltekit({ adapter: adapter(), compilerOptions: { experimental: { async: true } } })]
77
});

0 commit comments

Comments
 (0)