@@ -3,7 +3,7 @@ import { trace } from '@sentry/core';
3
3
import { captureException } from '@sentry/node' ;
4
4
import type { TransactionContext } from '@sentry/types' ;
5
5
import { addExceptionMechanism , objectify } from '@sentry/utils' ;
6
- import type { HttpError , Load , ServerLoad } from '@sveltejs/kit' ;
6
+ import type { HttpError , LoadEvent , ServerLoadEvent } from '@sveltejs/kit' ;
7
7
8
8
import { getTracePropagationData } from './utils' ;
9
9
@@ -42,19 +42,18 @@ function sendErrorToSentry(e: unknown): unknown {
42
42
}
43
43
44
44
/**
45
- * Wrap a universal load function (e.g. +page.js or +layout.js) with Sentry functionality
46
- *
47
- * Usage:
48
- * ```js
49
- * import { }
50
- * ```
51
- *
52
- * @param origLoad SvelteKit user defined load function
45
+ * @inheritdoc
53
46
*/
54
- export function wrapLoadWithSentry < T extends Load > ( origLoad : T ) : T {
47
+ // The liberal generic typing of `T` is necessary because we cannot let T extend `Load`.
48
+ // This function needs to tell TS that it returns exactly the type that it was called with
49
+ // because SvelteKit generates the narrowed down `PageLoad` or `LayoutLoad` types
50
+ // at build time for every route.
51
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
52
+ export function wrapLoadWithSentry < T extends ( ...args : any ) => any > ( origLoad : T ) : T {
55
53
return new Proxy ( origLoad , {
56
54
apply : ( wrappingTarget , thisArg , args : Parameters < T > ) => {
57
- const [ event ] = args ;
55
+ // Type casting here because `T` cannot extend `Load` (see comment above function signature)
56
+ const event = args [ 0 ] as LoadEvent ;
58
57
const routeId = event . route && event . route . id ;
59
58
60
59
const traceLoadContext : TransactionContext = {
@@ -73,20 +72,33 @@ export function wrapLoadWithSentry<T extends Load>(origLoad: T): T {
73
72
74
73
/**
75
74
* Wrap a server-only load function (e.g. +page.server.js or +layout.server.js) with Sentry functionality
76
- * TODO: usage
75
+ *
76
+ * Usage:
77
+ *
78
+ * ```js
79
+ * // +page.serverjs
80
+ *
81
+ * import { wrapServerLoadWithSentry }
82
+ *
83
+ * export const load = wrapServerLoadWithSentry((event) => {
84
+ * // your load code
85
+ * });
86
+ * ```
77
87
*
78
88
* @param origServerLoad SvelteKit user defined server-only load function
79
89
*/
80
- export function wrapServerLoadWithSentry < T extends ServerLoad > ( origServerLoad : T ) : T {
90
+ // The liberal generic typing of `T` is necessary because we cannot let T extend `ServerLoad`.
91
+ // This function needs to tell TS that it returns exactly the type that it was called with
92
+ // because SvelteKit generates the narrowed down `PageServerLoad` or `LayoutServerLoad` types
93
+ // at build time for every route.
94
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
95
+ export function wrapServerLoadWithSentry < T extends ( ...args : any ) => any > ( origServerLoad : T ) : T {
81
96
return new Proxy ( origServerLoad , {
82
97
apply : ( wrappingTarget , thisArg , args : Parameters < T > ) => {
83
- const [ event ] = args ;
98
+ // Type casting here because `T` cannot extend `ServerLoad` (see comment above function signature)
99
+ const event = args [ 0 ] as ServerLoadEvent ;
84
100
const routeId = event . route && event . route . id ;
85
101
86
- // Usually, the `handleWithSentry` hook handler should already create a transaction and store
87
- // traceparent and DSC on that transaction before the server-only load function is called.
88
- // However, since we have access to `event.request` we can still pass it to `trace`
89
- // in case our handler isn't called or for some reason the handle hook is bypassed.
90
102
const { dynamicSamplingContext, traceparentData } = getTracePropagationData ( event ) ;
91
103
92
104
const traceLoadContext : TransactionContext = {
0 commit comments