Skip to content

Commit

Permalink
add basic error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lobsterkatie committed Aug 16, 2022
1 parent 20abb1f commit 84cd837
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions packages/nextjs/src/config/wrappers/wrapperUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { captureException } from '@sentry/core';
import { getActiveTransaction } from '@sentry/tracing';
import { Span } from '@sentry/types';

import { DataFetchingFunction } from './types';

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

// TODO: Can't figure out how to tell TS that the types are correlated - that a `GSPropsFunction` will only get passed
// `GSPropsContext` and never, say, `GSSPContext`. That's what wrapping everything in objects and using the generic
// and pulling the types from the generic rather than specifying them directly was supposed to do, but... no luck.
// eslint-disable-next-line prefer-const, @typescript-eslint/no-explicit-any
const props = await (origFunction as any)(context);
const props = await callOriginal(origFunction, context, span);

span.finish();

return props;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (origFunction as any)(context);
return callOriginal(origFunction, context);
}

/** Call the original function, capturing any errors and finishing the span (if any) in case of error */
async function callOriginal<T extends DataFetchingFunction>(
origFunction: T['fn'],
context: T['context'],
span?: Span,
): Promise<T['result']> {
try {
// eslint-disable-next-line prefer-const, @typescript-eslint/no-explicit-any
return (origFunction as any)(context);
} catch (err) {
if (span) {
span.finish();
}

// TODO Copy more robust error handling over from `withSentry`
captureException(err);
throw err;
}
}

0 comments on commit 84cd837

Please sign in to comment.