Skip to content

Using wrapApiHandlerWithSentry after upgrading to @sentry/nextjs 8.x results in API routes failing with TypeError #12865

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
AaronFriel opened this issue Jul 11, 2024 · 7 comments
Labels
Package: nextjs Issues related to the Sentry Nextjs SDK

Comments

@AaronFriel
Copy link

AaronFriel commented Jul 11, 2024

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

8.15.0

Framework Version

Next 14.2.1

Link to Sentry event

n/a

SDK Setup/Reproduction Example

Install Sentry using @sentry/nextjs, and wrap an API route:

export const POST = wrapApiHandlerWithSentry(async (req: NextRequest): Promise<Response> => {
  // ...
}, "/my/route");

Steps to Reproduce

  1. Call API route via fetch in the browser.

  2. Observe:

    POST /my/route 500 in 17012ms
    ⨯ TypeError: Cannot create proxy with a non-object as target or handler
       at eval (webpack-internal:///(rsc)/../../node_modules/@sentry/nextjs/build/cjs/common/wrapApiHandlerWithSentry.js:78:29)
       at eval (webpack-internal:///(instrument)/../../node_modules/@sentry/node/node_modules/@sentry/opentelemetry/build/cjs/index.js:885:15)
       at Object.handleCallbackErrors (webpack-internal:///(instrument)/../../node_modules/@sentry/core/build/cjs/utils/handleCallbackErrors.js:26:26)
       at eval (webpack-internal:///(instrument)/../../node_modules/@sentry/node/node_modules/@sentry/opentelemetry/build/cjs/index.js:884:19)
    

Expected Result

Upgrading to @sentry/nextjs from 7.x to 8.x should not have resulted in 500 errors, especially after following the migration guide.

We previously used wrapApiHandlerWithSentry as Next.js 13 and 14 App Router route handlers were not automatically instrumented. The commit we added this call in linked to the tracking issue for App Router support, #6726. It looks like in upgrading our version of the Sentry SDK, this was fixed.

Actual Result

All API routes wrapped with wrapApiHandlerWithSentry return 500s and the type error above is thrown. It appears that this wrapping function and/or auto-instrumentation conflict.

This code appears relevant, but I'm not familiar with Sentry internals to say for sure:

// Make sure it's a function first, as we need to attach an empty prototype for `defineProperties` to work
// otherwise it'll throw "TypeError: Object.defineProperties called on non-object"
if (typeof wrapped === 'function') {
markFunctionWrapped(wrapped, original);
}

The error called out here is similar to the error we saw.

@lforst
Copy link
Contributor

lforst commented Jul 11, 2024

Hi, since you are using an App Router API route, you probably want to use wrapRouteHandlerWithSentry instead.

I opened a PR to make this more clear in the JSDoc.

@AaronFriel
Copy link
Author

@lforst should the migration guide call this out? It was surprising to see it go from working in 7.x to causing 500 errors on those routes in 8.x.

@getsantry getsantry bot moved this from Waiting for: Community to Waiting for: Product Owner in GitHub Issues with 👀 3 Jul 15, 2024
@lforst
Copy link
Contributor

lforst commented Jul 15, 2024

This cannot have worked in v7 I think, since this API has not been changed in v8. Unless something escapes me... There were a couple of renamings but those are in the migration guide. Are you sure you simply bumped and didnt change anything else?

@AaronFriel
Copy link
Author

The commit that bumped Sentry only bumped Sentry - though we were on a version prior to #6726 being closed, so there were some idiosyncracies with Next.js App Router support.

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Jul 16, 2024
@andreiborza
Copy link
Member

@AaronFriel is this still an issue for you after implementing @lforst's suggestion?

@AaronFriel
Copy link
Author

Removing the wrapper entirely worked, as we can rely on auto instrumentation now. It looks like the function might be double wrapped.

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Jul 16, 2024
@andreiborza
Copy link
Member

Closing this for now then, feel free to open up another issue for that if needed.

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

No branches or pull requests

3 participants