Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .agents/skills/exceptionless-javascript/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ Read only the references needed for the request. Start with the runtime for setu

- `@exceptionless/core`: [references/client-core.md](references/client-core.md)
- `@exceptionless/browser`: [references/client-browser.md](references/client-browser.md)
- `@exceptionless/node`, Express, Next.js server, and SvelteKit server: [references/client-node.md](references/client-node.md)
- `@exceptionless/node`, Express, and Next.js server: [references/client-node.md](references/client-node.md)
- SvelteKit client and server: [references/client-sveltekit.md](references/client-sveltekit.md)
- `@exceptionless/react`: [references/client-react.md](references/client-react.md)
- `@exceptionless/react-native` and Expo: [references/client-react-native.md](references/client-react-native.md)
- `@exceptionless/vue`: [references/client-vue.md](references/client-vue.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# @exceptionless/node

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

Docs:

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# SvelteKit

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.

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.

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.

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.

- [SvelteKit hooks](https://svelte.dev/docs/kit/hooks#handleError)
- [SvelteKit 3 migration guide](https://svelte.dev/docs/kit/migrating-to-sveltekit-3)
- `example/svelte-kit/src/hooks.client.js`
- `example/svelte-kit/src/hooks.server.js`
- `example/svelte-kit/vite.config.js`
17 changes: 10 additions & 7 deletions example/svelte-kit/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
## Exceptionless Svelte Kit Example
## Exceptionless SvelteKit Example

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
a client side error hook `hooks.client.js` and a server side error hook `hooks.server.js`.
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.

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.

To run locally, follow these steps:

1. `git clone https://github.com/exceptionless/Exceptionless.JavaScript`
2. `cd Exceptionless.Javascript`
3. `npm install`
4. `cd example/svelte-kit`
5. `npm run dev -- --open`
2. `cd Exceptionless.JavaScript`
3. `npm ci`
4. `npm run build --workspace=packages/core --workspace=packages/browser --workspace=packages/node`
5. `npm run dev --workspace=example/svelte-kit -- --open`

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.
9 changes: 3 additions & 6 deletions example/svelte-kit/jsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "./.svelte-kit/tsconfig.json",
"extends": "$app/tsconfig",
"compilerOptions": {
"allowJs": true,
"checkJs": true,
Expand All @@ -9,9 +9,6 @@
"skipLibCheck": true,
"sourceMap": true,
"strict": true
}
// Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias and https://kit.svelte.dev/docs/configuration#files
//
// If you want to overwrite includes/excludes, make sure to copy over the relevant includes/excludes
// from the referenced tsconfig.json - TypeScript does not merge them in
},
"include": ["src", "test", "*.js"]
}
8 changes: 5 additions & 3 deletions example/svelte-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./jsconfig.json --watch"
},
"dependencies": {
"@exceptionless/browser": "3.0.0-dev"
"@exceptionless/browser": "3.0.0-dev",
"@exceptionless/node": "3.0.0-dev",
"cookie": "^2.0.2"
},
"devDependencies": {
"@sveltejs/adapter-auto": "^7.0.1",
"@sveltejs/kit": "^2.70.3",
"@sveltejs/adapter-auto": "^9.0.0",
"@sveltejs/kit": "^3.4.0",
"@sveltejs/vite-plugin-svelte": "^7.3.1",
"svelte": "^5.57.1",
"svelte-check": "^4.7.6",
Expand Down
22 changes: 15 additions & 7 deletions example/svelte-kit/src/hooks.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@ Exceptionless.startup((c) => {
c.defaultTags.push("Example", "svelte-kit", "client");
});

/** @type {import('@sveltejs/kit').HandleClientError} */
export async function handleError({ error, event, message, status }) {
console.warn({ error, event, message, source: "client error handler", status });
await Exceptionless.createException(toError(error ?? message))
.setProperty("status", status)
.submit();
}
/** @type {import("@sveltejs/kit/hooks").HandleClientError} */
export const handleError = async ({ error, event, kind }) => {
console.warn({ error, event, kind, source: "client error handler" });
try {
const builder =
kind === "unknown" ? Exceptionless.createUnhandledException(toError(error), "sveltekit.client") : Exceptionless.createException(toError(error.message));
builder.setProperty("sveltekit.kind", kind);
if (kind !== "unknown") {
builder.setProperty("status", error.status);
}
await builder.submit();
} catch (submissionError) {
console.error("Exceptionless failed to report the client error", submissionError);
}
};
18 changes: 14 additions & 4 deletions example/svelte-kit/src/hooks.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,18 @@ Exceptionless.startup((c) => {
c.defaultTags.push("Example", "svelte-kit", "server");
});

/** @type {import("@sveltejs/kit").HandleServerError} */
export async function handleError({ error, event }) {
/** @type {import("@sveltejs/kit/hooks").HandleServerError} */
export const handleError = async ({ error, kind }) => {
console.log("server error handler");
await Exceptionless.submitException(toError(error));
}
try {
const builder =
kind === "unknown" ? Exceptionless.createUnhandledException(toError(error), "sveltekit.server") : Exceptionless.createException(toError(error.message));
builder.setProperty("sveltekit.kind", kind);
if (kind !== "unknown") {
builder.setProperty("status", error.status);
}
await builder.submit();
} catch (submissionError) {
console.error("Exceptionless failed to report the server error", submissionError);
}
};
10 changes: 0 additions & 10 deletions example/svelte-kit/svelte.config.js

This file was deleted.

81 changes: 81 additions & 0 deletions example/svelte-kit/test/hooks.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";

const mocks = vi.hoisted(() => {
const client = { startup: vi.fn(), createException: vi.fn(), createUnhandledException: vi.fn(), submitException: vi.fn() };
const server = { startup: vi.fn(), createException: vi.fn(), createUnhandledException: vi.fn(), submitException: vi.fn() };
const builder = { setProperty: vi.fn(), submit: vi.fn() };
return { client, server, builder };
});

vi.mock("@exceptionless/browser", () => ({
Exceptionless: mocks.client,
toError: (/** @type {unknown} */ value) => (value instanceof Error ? value : new Error(String(value)))
}));
vi.mock("@exceptionless/node", () => ({
Exceptionless: mocks.server,
toError: (/** @type {unknown} */ value) => (value instanceof Error ? value : new Error(String(value)))
}));

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

/** @param {unknown} input */
const runClient = (input) => handleClientError(/** @type {Parameters<typeof handleClientError>[0]} */ (input));
/** @param {unknown} input */
const runServer = (input) => handleServerError(/** @type {Parameters<typeof handleServerError>[0]} */ (input));

describe("SvelteKit error hooks", () => {
beforeEach(() => {
vi.clearAllMocks();
vi.spyOn(console, "warn").mockImplementation(() => {});
vi.spyOn(console, "log").mockImplementation(() => {});
vi.spyOn(console, "error").mockImplementation(() => {});
mocks.builder.setProperty.mockReturnValue(mocks.builder);
mocks.builder.submit.mockResolvedValue(undefined);
for (const client of [mocks.client, mocks.server]) {
client.submitException.mockResolvedValue(undefined);
client.createException.mockReturnValue(mocks.builder);
client.createUnhandledException.mockReturnValue(mocks.builder);
}
});

afterEach(() => vi.restoreAllMocks());

test.each(["app", "framework"])("reports %s errors as handled with their status", async (kind) => {
const input = { kind, error: { status: 404, message: "Not found" }, event: {} };
await runClient(input);
await runServer(input);
for (const client of [mocks.client, mocks.server]) {
expect(client.createException).toHaveBeenCalledWith(expect.objectContaining({ message: "Not found" }));
expect(client.createUnhandledException).not.toHaveBeenCalled();
}
expect(mocks.builder.setProperty).toHaveBeenCalledWith("status", 404);
expect(mocks.builder.submit).toHaveBeenCalledTimes(2);
expect(console.warn).toHaveBeenCalled();
expect(console.log).toHaveBeenCalled();
});

test("reports server validation errors as handled", async () => {
await runServer({ kind: "validation", error: { status: 400, message: "Bad Request" }, issues: [], event: {} });
expect(mocks.server.createException).toHaveBeenCalledWith(expect.objectContaining({ message: "Bad Request" }));
expect(mocks.server.createUnhandledException).not.toHaveBeenCalled();
expect(mocks.builder.setProperty).toHaveBeenCalledWith("status", 400);
expect(mocks.builder.submit).toHaveBeenCalledOnce();
});

test("reports unknown errors as unhandled", async () => {
const error = new Error("Unexpected failure");
await runClient({ kind: "unknown", error, event: {} });
await runServer({ kind: "unknown", error, event: {} });
expect(mocks.client.createUnhandledException).toHaveBeenCalledWith(error, "sveltekit.client");
expect(mocks.server.createUnhandledException).toHaveBeenCalledWith(error, "sveltekit.server");
expect(mocks.builder.submit).toHaveBeenCalledTimes(2);
});

test("does not throw when Exceptionless submission fails", async () => {
mocks.builder.submit.mockRejectedValue(new Error("Storage unavailable"));
await expect(runClient({ kind: "unknown", error: new Error("Failure"), event: {} })).resolves.toBeUndefined();
await expect(runServer({ kind: "unknown", error: new Error("Failure"), event: {} })).resolves.toBeUndefined();
expect(console.error).toHaveBeenCalledTimes(2);
});
});
5 changes: 3 additions & 2 deletions example/svelte-kit/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import adapter from "@sveltejs/adapter-auto";
import { sveltekit } from "@sveltejs/kit/vite";
import { defineConfig } from "vitest/config";
import { defineConfig } from "vite";

export default defineConfig({
plugins: [sveltekit()]
plugins: [sveltekit({ adapter: adapter(), compilerOptions: { experimental: { async: true } } })]
});
Loading
Loading