|
1 | 1 | import { captureException } from '@sentry/node'; |
2 | | -import { addExceptionMechanism, objectify } from '@sentry/utils'; |
| 2 | +import { addExceptionMechanism, objectify, isThenable } from '@sentry/utils'; |
3 | 3 | import type { HttpError, ServerLoad } from '@sveltejs/kit'; |
4 | 4 |
|
5 | 5 | function isHttpError(err: unknown): err is HttpError { |
6 | 6 | return typeof err === 'object' && err !== null && 'status' in err && 'body' in err; |
7 | 7 | } |
8 | 8 |
|
| 9 | +function captureAndThrowError(e: unknown): void { |
| 10 | + // In case we have a primitive, wrap it in the equivalent wrapper class (string -> String, etc.) so that we can |
| 11 | + // store a seen flag on it. |
| 12 | + const objectifiedErr = objectify(e); |
| 13 | + |
| 14 | + // The error() helper is commonly used to throw errors in load functions: https://kit.svelte.dev/docs/modules#sveltejs-kit-error |
| 15 | + // If we detect a thrown error that is an instance of HttpError, we don't want to capture 4xx errors as they |
| 16 | + // could be noisy. |
| 17 | + if (isHttpError(objectifiedErr) && objectifiedErr.status < 500 && objectifiedErr.status >= 400) { |
| 18 | + throw objectifiedErr; |
| 19 | + } |
| 20 | + |
| 21 | + captureException(objectifiedErr, scope => { |
| 22 | + scope.addEventProcessor(event => { |
| 23 | + addExceptionMechanism(event, { |
| 24 | + type: 'sveltekit', |
| 25 | + handled: false, |
| 26 | + data: { |
| 27 | + function: 'load', |
| 28 | + }, |
| 29 | + }); |
| 30 | + return event; |
| 31 | + }); |
| 32 | + |
| 33 | + return scope; |
| 34 | + }); |
| 35 | + |
| 36 | + throw objectifiedErr; |
| 37 | +} |
| 38 | + |
9 | 39 | /** |
10 | 40 | * Wrap load function with Sentry |
11 | 41 | * |
12 | 42 | * @param origLoad SvelteKit user defined load function |
13 | 43 | */ |
14 | 44 | export function wrapLoadWithSentry(origLoad: ServerLoad): ServerLoad { |
15 | 45 | return new Proxy(origLoad, { |
16 | | - apply: async (wrappingTarget, thisArg, args: Parameters<ServerLoad>) => { |
| 46 | + apply: (wrappingTarget, thisArg, args: Parameters<ServerLoad>) => { |
| 47 | + let maybePromiseResult; |
| 48 | + |
17 | 49 | try { |
18 | | - return await wrappingTarget.apply(thisArg, args); |
| 50 | + maybePromiseResult = wrappingTarget.apply(thisArg, args); |
19 | 51 | } catch (e) { |
20 | | - // In case we have a primitive, wrap it in the equivalent wrapper class (string -> String, etc.) so that we can |
21 | | - // store a seen flag on it. |
22 | | - const objectifiedErr = objectify(e) as unknown; |
23 | | - |
24 | | - // The error() helper is commonly used to throw errors in load functions: https://kit.svelte.dev/docs/modules#sveltejs-kit-error |
25 | | - // If we detect a thrown error that is an instance of HttpError, we don't want to capture 4xx errors as they |
26 | | - // could be noisy. |
27 | | - if (isHttpError(objectifiedErr) && objectifiedErr.status < 500 && objectifiedErr.status >= 400) { |
28 | | - throw objectifiedErr; |
29 | | - } |
30 | | - |
31 | | - captureException(objectifiedErr, scope => { |
32 | | - scope.addEventProcessor(event => { |
33 | | - addExceptionMechanism(event, { |
34 | | - type: 'sveltekit', |
35 | | - handled: false, |
36 | | - data: { |
37 | | - function: 'load', |
38 | | - }, |
39 | | - }); |
40 | | - return event; |
41 | | - }); |
42 | | - |
43 | | - return scope; |
44 | | - }); |
| 52 | + captureAndThrowError(e); |
| 53 | + } |
45 | 54 |
|
46 | | - throw objectifiedErr; |
| 55 | + if (isThenable(maybePromiseResult)) { |
| 56 | + Promise.resolve(maybePromiseResult).then(null, e => { |
| 57 | + captureAndThrowError(e); |
| 58 | + }); |
47 | 59 | } |
| 60 | + |
| 61 | + return maybePromiseResult; |
48 | 62 | }, |
49 | 63 | }); |
50 | 64 | } |
0 commit comments