Skip to content

Commit bb57072

Browse files
committed
add basic error handling
1 parent 3a27533 commit bb57072

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

packages/nextjs/src/config/wrappers/wrapperUtils.ts

+24-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { captureException } from '@sentry/node';
12
import { getActiveTransaction } from '@sentry/tracing';
3+
import { Span } from '@sentry/types';
24

35
import { DataFetchingFunction } from './types';
46

@@ -31,17 +33,33 @@ export async function wrapperCore<T extends DataFetchingFunction>(
3133
// route's transaction
3234
const span = transaction.startChild({ op: 'nextjs.data', description: `${wrappedFunctionName} (${route})` });
3335

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);
3937

4038
span.finish();
4139

4240
return props;
4341
}
4442

4543
// 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+
}
4765
}

0 commit comments

Comments
 (0)