|
1 | 1 | import { captureException } from '@sentry/svelte'; |
2 | | -import { addExceptionMechanism, objectify } from '@sentry/utils'; |
| 2 | +import { addExceptionMechanism, isThenable, objectify } from '@sentry/utils'; |
3 | 3 | import type { ServerLoad } from '@sveltejs/kit'; |
4 | 4 |
|
| 5 | +function captureAndThrowError(e: unknown): void { |
| 6 | + // In case we have a primitive, wrap it in the equivalent wrapper class (string -> String, etc.) so that we can |
| 7 | + // store a seen flag on it. |
| 8 | + const objectifiedErr = objectify(e); |
| 9 | + |
| 10 | + captureException(objectifiedErr, scope => { |
| 11 | + scope.addEventProcessor(event => { |
| 12 | + addExceptionMechanism(event, { |
| 13 | + type: 'sveltekit', |
| 14 | + handled: false, |
| 15 | + data: { |
| 16 | + function: 'load', |
| 17 | + }, |
| 18 | + }); |
| 19 | + return event; |
| 20 | + }); |
| 21 | + |
| 22 | + return scope; |
| 23 | + }); |
| 24 | + |
| 25 | + throw objectifiedErr; |
| 26 | +} |
| 27 | + |
5 | 28 | /** |
6 | 29 | * Wrap load function with Sentry |
7 | 30 | * |
8 | 31 | * @param origLoad SvelteKit user defined load function |
9 | 32 | */ |
10 | 33 | export function wrapLoadWithSentry(origLoad: ServerLoad): ServerLoad { |
11 | 34 | return new Proxy(origLoad, { |
12 | | - apply: async (wrappingTarget, thisArg, args: Parameters<ServerLoad>) => { |
| 35 | + apply: (wrappingTarget, thisArg, args: Parameters<ServerLoad>) => { |
| 36 | + let maybePromiseResult; |
| 37 | + |
13 | 38 | try { |
14 | | - return await wrappingTarget.apply(thisArg, args); |
| 39 | + maybePromiseResult = wrappingTarget.apply(thisArg, args); |
15 | 40 | } catch (e) { |
16 | | - // In case we have a primitive, wrap it in the equivalent wrapper class (string -> String, etc.) so that we can |
17 | | - // store a seen flag on it. |
18 | | - const objectifiedErr = objectify(e) as unknown; |
19 | | - |
20 | | - captureException(objectifiedErr, scope => { |
21 | | - scope.addEventProcessor(event => { |
22 | | - addExceptionMechanism(event, { |
23 | | - type: 'sveltekit', |
24 | | - handled: false, |
25 | | - data: { |
26 | | - function: 'load', |
27 | | - }, |
28 | | - }); |
29 | | - return event; |
30 | | - }); |
31 | | - |
32 | | - return scope; |
33 | | - }); |
| 41 | + captureAndThrowError(e); |
| 42 | + } |
34 | 43 |
|
35 | | - throw objectifiedErr; |
| 44 | + if (isThenable(maybePromiseResult)) { |
| 45 | + Promise.resolve(maybePromiseResult).then(null, e => { |
| 46 | + captureAndThrowError(e); |
| 47 | + }); |
36 | 48 | } |
| 49 | + |
| 50 | + return maybePromiseResult; |
37 | 51 | }, |
38 | 52 | }); |
39 | 53 | } |
0 commit comments