-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathserverRoute.ts
67 lines (61 loc) · 2.3 KB
/
serverRoute.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, startSpan } from '@sentry/node';
import { addNonEnumerableProperty } from '@sentry/utils';
import type { RequestEvent } from '@sveltejs/kit';
import { flushIfServerless, sendErrorToSentry } from './utils';
type PatchedServerRouteEvent = RequestEvent & { __sentry_wrapped__?: boolean };
/**
* Wraps a server route handler for API or server routes registered in `+server.(js|js)` files.
*
* This function will automatically capture any errors that occur during the execution of the route handler
* and it will start a span for the duration of your route handler.
*
* @example
* ```js
* import { wrapServerRouteWithSentry } from '@sentry/sveltekit';
*
* const get = async event => {
* return new Response(JSON.stringify({ message: 'hello world' }));
* }
*
* export const GET = wrapServerRouteWithSentry(get);
* ```
*
* @param originalRouteHandler your server route handler
* @param httpMethod the HTTP method of your route handler
*
* @returns a wrapped version of your server route handler
*/
export function wrapServerRouteWithSentry<T extends RequestEvent>(
originalRouteHandler: (request: T) => Promise<Response>,
): (requestEvent: T) => Promise<Response> {
return new Proxy(originalRouteHandler, {
apply: async (wrappingTarget, thisArg, args) => {
const event = args[0] as PatchedServerRouteEvent;
if (event.__sentry_wrapped__) {
return wrappingTarget.apply(thisArg, args);
}
const routeId = event.route && event.route.id;
const httpMethod = event.request.method;
addNonEnumerableProperty(event as unknown as Record<string, unknown>, '__sentry_wrapped__', true);
try {
return await startSpan(
{
name: `${httpMethod} ${routeId || 'Server Route'}`,
op: `function.sveltekit.server.${httpMethod.toLowerCase()}`,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.sveltekit',
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
},
onlyIfParent: true,
},
() => wrappingTarget.apply(thisArg, args),
);
} catch (e) {
sendErrorToSentry(e, 'serverRoute');
throw e;
} finally {
await flushIfServerless();
}
},
});
}