Skip to content

Commit a5a8fd6

Browse files
committed
make sure we are always returning original promise
1 parent ac43b2f commit a5a8fd6

File tree

1 file changed

+43
-29
lines changed
  • packages/sveltekit/src/server

1 file changed

+43
-29
lines changed

packages/sveltekit/src/server/load.ts

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,64 @@
11
import { captureException } from '@sentry/node';
2-
import { addExceptionMechanism, objectify } from '@sentry/utils';
2+
import { addExceptionMechanism, objectify, isThenable } from '@sentry/utils';
33
import type { HttpError, ServerLoad } from '@sveltejs/kit';
44

55
function isHttpError(err: unknown): err is HttpError {
66
return typeof err === 'object' && err !== null && 'status' in err && 'body' in err;
77
}
88

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+
939
/**
1040
* Wrap load function with Sentry
1141
*
1242
* @param origLoad SvelteKit user defined load function
1343
*/
1444
export function wrapLoadWithSentry(origLoad: ServerLoad): ServerLoad {
1545
return new Proxy(origLoad, {
16-
apply: async (wrappingTarget, thisArg, args: Parameters<ServerLoad>) => {
46+
apply: (wrappingTarget, thisArg, args: Parameters<ServerLoad>) => {
47+
let maybePromiseResult;
48+
1749
try {
18-
return await wrappingTarget.apply(thisArg, args);
50+
maybePromiseResult = wrappingTarget.apply(thisArg, args);
1951
} 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+
}
4554

46-
throw objectifiedErr;
55+
if (isThenable(maybePromiseResult)) {
56+
Promise.resolve(maybePromiseResult).then(null, e => {
57+
captureAndThrowError(e);
58+
});
4759
}
60+
61+
return maybePromiseResult;
4862
},
4963
});
5064
}

0 commit comments

Comments
 (0)