Skip to content

wrapGetInitialPropsWithSentry throws an exception if getInitialProps returns undefined #9066

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
3 tasks done
Eszik opened this issue Sep 20, 2023 · 4 comments · Fixed by #9342
Closed
3 tasks done
Assignees
Labels
Package: nextjs Issues related to the Sentry Nextjs SDK

Comments

@Eszik
Copy link

Eszik commented Sep 20, 2023

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/nextjs

SDK Version

7.69.0

Framework Version

React 17.0, NextJS 12.1.0

Link to Sentry event

https://ardian.sentry.io/issues/4480381245/

SDK Setup

No response

Steps to Reproduce

If a page's getInitialProps function does not return an object, but rather undefined or null, Sentry will throw an exception on that page. This is because of this piece of code in wrapGetInitialPropsWithSentry.ts :

const initialProps: {
          _sentryTraceData?: string;
          _sentryBaggage?: string;
        } = await tracedGetInitialProps.apply(thisArg, args);

        const requestTransaction = getTransactionFromRequest(req) ?? hub.getScope().getTransaction();
        if (requestTransaction) {
          initialProps._sentryTraceData = requestTransaction.toTraceparent();

          const dynamicSamplingContext = requestTransaction.getDynamicSamplingContext();
          initialProps._sentryBaggage = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext);
        }

        return initialProps;

The specific error is TypeError: Cannot set properties of undefined (setting '_sentryTraceData')

For context, I had a page's getInitialProps returning undefined because I only used getInitialProps to handle server-side redirections.

I don't know if this can easliy modified whilde still keeping the instrumentation, but the exception could at least be caught to not bubble up and crash the whole page.

Expected Result

Sentry doesn't throw an exception if getInitialProps returns undefined

Actual Result

Sentry throws an exception if getInitialProps returns undefined, crashing the whole page.

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 Sep 20, 2023
@github-actions github-actions bot added the Package: nextjs Issues related to the Sentry Nextjs SDK label Sep 20, 2023
@AbhiPrasad
Copy link
Member

This is what the TypeScript type being emitted say: https://github.com/vercel/next.js/blob/628a19393bea4e40af3ef73b101669e1c2fdd672/packages/next/src/shared/lib/utils.ts#L21C62-L21C74

We can try and guard around this, but we generally expect the TS types from Next.js to be respected and it can be resolved by easily adjusting your app's getInitialProps function.

I'll defer to @lforst to make the call though.

@lforst
Copy link
Contributor

lforst commented Sep 21, 2023

Thanks for raising this. I'd say if Next.js itself doesn't throw, the SDK shouldn't either. We'll guard for this.

@lforst lforst self-assigned this Sep 21, 2023
@Eszik
Copy link
Author

Eszik commented Sep 25, 2023

Thanks for the quick response! I'll add that the typing from nextjs is very weak, as undefined is assignable to type { }. The return value should probably be typed as some sort of Record, but as of right now returning undefined from getInitialProps compiles just fine

@AbhiPrasad
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: nextjs Issues related to the Sentry Nextjs SDK
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants