nestjs icon indicating copy to clipboard operation
nestjs copied to clipboard

Issue with nestjs-stripe and Sentry integration

Open fdegmecic opened this issue 11 months ago • 4 comments

After adding Sentry to my NestJS project, when there is an error inside a function annotated with @StripeWebhookHandler, there is this error:

at async StripeWebhookController.handleWebhook (/opt/render/project/src/node_modules/@golevelup/nestjs-stripe/lib/stripe.webhook.controller.js:35:9)
    at async StripeWebhookService.handleWebhook (/opt/render/project/src/node_modules/@golevelup/nestjs-stripe/lib/stripe.module.js:122:17)
    at async Promise.all (index 0)
    at Object.handler (/opt/render/project/src/node_modules/@nestjs/core/helpers/external-proxy.js:14:42)
    at ExternalExceptionsHandler.next (/opt/render/project/src/node_modules/@nestjs/core/exceptions/external-exceptions-handler.js:14:29)
    at ExternalExceptionsHandler.invokeCustomFilters (/opt/render/project/src/node_modules/@nestjs/core/exceptions/external-exceptions-handler.js:31:32)
    at SentryGlobalFilter.catch (/opt/render/project/src/node_modules/@sentry/nestjs/build/cjs/setup.js:65:25)
    at SentryGlobalFilter.catch (/opt/render/project/src/node_modules/@nestjs/core/exceptions/base-exception-filter.js:27:29)
    at ExpressAdapter.isHeadersSent (/opt/render/project/src/node_modules/@nestjs/platform-express/adapters/express-adapter.js:83:25)
TypeError: Cannot read properties of undefined (reading 'headersSent')

With this being the exact line causing the error:

    isHeadersSent(response) {
        return response.headersSent;
    }

I've tried googling but there isn't much to find, from digging into the exact location of the error, I can see that indeed, headersSent is null, but I am not sure how to set it, since the location of the controller is in golevelup/stripe. https://github.com/golevelup/nestjs/blob/master/packages/stripe/src/stripe.webhook.controller.ts

Sentry version used: "@sentry/nestjs": "^8.30.0"

fdegmecic avatar Jan 19 '25 12:01 fdegmecic

@fdegmecic Are you able to provide me a reproduction that I can pull down and test?

underfisk avatar Jan 28 '25 10:01 underfisk

Sure, here it is: https://github.com/fdegmecic/sentry-stripe-test, you will just have to set the STRIPE_SECRET_WEBHOOK_SECRET_TEST in the .env file.

fdegmecic avatar Jan 28 '25 18:01 fdegmecic

@fdegmecic Thank you! I will try to follow up on this once I have some free time

underfisk avatar Jan 28 '25 21:01 underfisk

I'm seeing the same error. Also using Stripe & Sentry.

This blocks us from using this library.

Edit: this error only occurs for me when I use a global auth guard that I skip for the Stripe webhook controller as described here: https://github.com/nestjs/nest/issues/964#issuecomment-480834786

Edit 2: This appears to only be an issue for events that are processed by a StripeWebhookHandler. The decorators we can set on the controller via webhookConfig only affect events that don't match with a StripeWebhookHandler

homj avatar Apr 21 '25 20:04 homj