Skip to content

Commit f43184d

Browse files
committed
client: return promise explicitly
1 parent a6bf3e0 commit f43184d

File tree

1 file changed

+36
-22
lines changed
  • packages/sveltekit/src/client

1 file changed

+36
-22
lines changed

packages/sveltekit/src/client/load.ts

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,53 @@
11
import { captureException } from '@sentry/svelte';
2-
import { addExceptionMechanism, objectify } from '@sentry/utils';
2+
import { addExceptionMechanism, isThenable, objectify } from '@sentry/utils';
33
import type { ServerLoad } from '@sveltejs/kit';
44

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+
528
/**
629
* Wrap load function with Sentry
730
*
831
* @param origLoad SvelteKit user defined load function
932
*/
1033
export function wrapLoadWithSentry(origLoad: ServerLoad): ServerLoad {
1134
return new Proxy(origLoad, {
12-
apply: async (wrappingTarget, thisArg, args: Parameters<ServerLoad>) => {
35+
apply: (wrappingTarget, thisArg, args: Parameters<ServerLoad>) => {
36+
let maybePromiseResult;
37+
1338
try {
14-
return await wrappingTarget.apply(thisArg, args);
39+
maybePromiseResult = wrappingTarget.apply(thisArg, args);
1540
} 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+
}
3443

35-
throw objectifiedErr;
44+
if (isThenable(maybePromiseResult)) {
45+
Promise.resolve(maybePromiseResult).then(null, e => {
46+
captureAndThrowError(e);
47+
});
3648
}
49+
50+
return maybePromiseResult;
3751
},
3852
});
3953
}

0 commit comments

Comments
 (0)