-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
137 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { generateUserErrorMessage, sendSvelteErrorLog } from "$lib/utils/error" | ||
import type { HandleClientError } from "@sveltejs/kit" | ||
|
||
export const handleError = (async ({ error, event, message, status }) => { | ||
const errorId = await sendSvelteErrorLog({ error, event, message, status }, "client") | ||
return { | ||
message: generateUserErrorMessage(errorId) | ||
} | ||
}) satisfies HandleClientError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const AXIOM_KEY = "xaat-c2ba1f52-0af4-4814-81d1-996c285912c8" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { browser } from "$app/environment" | ||
import { axiom } from "$lib/utils/axiom.ts" | ||
|
||
const browserErrorSessionId = browser ? crypto.randomUUID() : undefined | ||
|
||
export const generateUserErrorMessage = (errorId: string) => { | ||
return `An unexpected error occurred, please try again! Error ID: "${errorId}"` | ||
} | ||
|
||
export const sendSvelteErrorLog = async (svelteError: SvelteError, type: "client") => { | ||
return await sendErrorLog({ svelte: svelteError }, type) | ||
} | ||
|
||
export const sendWindowErrorLog = async (event: Event) => { | ||
const origin = event.target instanceof Window ? event.target.origin : undefined | ||
const location = event.target instanceof Window ? event.target.location.href : undefined | ||
const message = event instanceof ErrorEvent ? event.message : undefined | ||
const stack = event instanceof ErrorEvent ? event.error?.stack : undefined | ||
const windowEventError = { | ||
origin, | ||
location, | ||
message, | ||
stack, | ||
type: event.type, | ||
sessionId: browserErrorSessionId | ||
} | ||
return await sendErrorLog({ window: windowEventError }, "window") | ||
} | ||
|
||
export const sendWindowRejectionLog = async (event: PromiseRejectionEvent) => { | ||
const origin = event.target instanceof Window ? event.target.origin : undefined | ||
const location = event.target instanceof Window ? event.target.location.href : undefined | ||
const windowEventError = { | ||
origin, | ||
location, | ||
message: event.reason?.message, | ||
stack: event.reason?.stack, | ||
type: event.type, | ||
sessionId: browserErrorSessionId | ||
} | ||
return await sendErrorLog({ window: windowEventError }, "window") | ||
} | ||
|
||
const sendErrorLog = async (detail: ErrorDetail, type: "client" | "window") => { | ||
const errorId = crypto.randomUUID() | ||
const jsonBody = JSON.stringify( | ||
{ | ||
errorId, | ||
type, | ||
detail | ||
}, | ||
errorJsonReplacer | ||
) | ||
|
||
axiom.ingest("errors", [ | ||
{ | ||
errorId, | ||
type, | ||
detail | ||
}, | ||
errorJsonReplacer | ||
]) | ||
|
||
return errorId | ||
} | ||
|
||
const errorJsonReplacer = (key: string, value: unknown) => { | ||
try { | ||
if (key === "error" && !!value && typeof value === "object") { | ||
const sanitizedError: { [k: string]: unknown } = {} | ||
const errorValue = value as Record<string, unknown> | ||
for (const propertyName of Object.getOwnPropertyNames(errorValue)) { | ||
sanitizedError[propertyName] = errorValue[propertyName] | ||
} | ||
return sanitizedError | ||
} | ||
} catch (e) { | ||
console.error("Failed to sanitize error", e) | ||
} | ||
return value | ||
} | ||
|
||
type SvelteError = { | ||
error: unknown | ||
event: unknown | ||
message: string | ||
status: number | ||
} | ||
|
||
type WindowEventError = { | ||
origin: string | undefined | ||
location: string | undefined | ||
message: unknown | undefined | ||
stack: unknown | undefined | ||
type: string | ||
sessionId: string | undefined | ||
} | ||
|
||
type ErrorDetail = { | ||
svelte?: SvelteError | ||
window?: WindowEventError | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters