|
| 1 | +import { captureException } from '@sentry/core'; |
1 | 2 | import { getActiveTransaction } from '@sentry/tracing';
|
| 3 | +import { Span } from '@sentry/types'; |
2 | 4 |
|
3 | 5 | import { DataFetchingFunction } from './types';
|
4 | 6 |
|
@@ -31,17 +33,33 @@ export async function wrapperCore<T extends DataFetchingFunction>(
|
31 | 33 | // route's transaction
|
32 | 34 | const span = transaction.startChild({ op: 'nextjs.data', description: `${wrappedFunctionName} (${route})` });
|
33 | 35 |
|
34 |
| - // TODO: Can't figure out how to tell TS that the types are correlated - that a `GSPropsFunction` will only get passed |
35 |
| - // `GSPropsContext` and never, say, `GSSPContext`. That's what wrapping everything in objects and using the generic |
36 |
| - // and pulling the types from the generic rather than specifying them directly was supposed to do, but... no luck. |
37 |
| - // eslint-disable-next-line prefer-const, @typescript-eslint/no-explicit-any |
38 |
| - const props = await (origFunction as any)(context); |
| 36 | + const props = await callOriginal(origFunction, context, span); |
39 | 37 |
|
40 | 38 | span.finish();
|
41 | 39 |
|
42 | 40 | return props;
|
43 | 41 | }
|
44 | 42 |
|
45 | 43 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
46 |
| - return (origFunction as any)(context); |
| 44 | + return callOriginal(origFunction, context); |
| 45 | +} |
| 46 | + |
| 47 | +/** Call the original function, capturing any errors and finishing the span (if any) in case of error */ |
| 48 | +async function callOriginal<T extends DataFetchingFunction>( |
| 49 | + origFunction: T['fn'], |
| 50 | + context: T['context'], |
| 51 | + span?: Span, |
| 52 | +): Promise<T['result']> { |
| 53 | + try { |
| 54 | + // eslint-disable-next-line prefer-const, @typescript-eslint/no-explicit-any |
| 55 | + return (origFunction as any)(context); |
| 56 | + } catch (err) { |
| 57 | + if (span) { |
| 58 | + span.finish(); |
| 59 | + } |
| 60 | + |
| 61 | + // TODO Copy more robust error handling over from `withSentry` |
| 62 | + captureException(err); |
| 63 | + throw err; |
| 64 | + } |
47 | 65 | }
|
0 commit comments