Skip to content

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

Closed
@AaronFriel

Description

@AaronFriel

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Package: nextjsIssues related to the Sentry Nextjs SDK

    Type

    Projects

    Status

    Waiting for: Product Owner

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions