Skip to content

wrapGetInitialPropsWithSentry throws an exception if getInitialProps returns undefined #9066

Closed
@Eszik

Description

@Eszik

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.

Metadata

Metadata

Assignees

Labels

Package: nextjsIssues related to the Sentry Nextjs SDK

Type

No type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions