Error Report from Stripe.ts and Relevant Questions

Posting enough to show error for privacy reasons:
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client.

at stripeWebhook (/path/app/src/server/webhooks/stripe.ts:253:26)
[ Server!] at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
[ Server!] at <anonymous> (/path/app/.wasp/out/sdk/wasp/server/utils.ts:26:5)
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client.

at stripeWebhook (/path/app/src/server/webhooks/stripe.ts:253:26)
[ Server!] at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
[ Server!] at <anonymous> (/path/app/.wasp/out/sdk/wasp/server/utils.ts:26:5)
And this is line 253 from Stripe.ts:
response.status(400).send(`Webhook Error: ${err?.message}`);
response.status(400).send(`Webhook Error: ${err?.message}`);
When I log the error right before (it is inside a catch block), it is the same as 'Cannot set headers after they are sent to the client.' Couple other notes, I did some changes to webhook but I am not sending any extra responses, just capturing extra events to do some updates and record customer payments:
else if (event.type === 'charge.succeeded')
else if (event.type === 'charge.succeeded')
The work goes through successfully (payment is recorded in the db) and nothing is functionally affected by this error but figure this should still be considered.
5 Replies
kapa.ai
kapa.ai3w ago
Hi @neogeofun. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Hmm, I don't know enough to give you a confident answer yet. However, the error message you're seeing typically occurs when you're trying to modify the HTTP response after it has already been sent. This could be due to multiple response.send() or response.json() calls, or due to trying to modify headers after a response has been sent. In your case, it seems like the error is being thrown at line 253 in your stripe.ts file, where you're trying to set the status and send a response. If there's another part of your code that's already sending a response before this, it could be causing this error. As for debugging, some community members have suggested adding more console.log lines or running Wasp with more debug messages. You can find more about this in the Discord support thread (https://discord.com/channels/686873244791210014/1215945587841831033). If you're still having trouble, I would recommend reaching out to the Wasp community on their Discord server (https://discord.com/channels/686873244791210014/1200182362449907813) for more specific help. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @neogeofun, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
miho
miho3w ago
@Vinny (@Wasp) one for you
Vinny (@Wasp)
Vinny (@Wasp)2w ago
Are you somehow sending a response twice within your webhook? After which event does this error occur?
neogeofun
neogeofun2w ago
@Vinny (@Wasp) I just added some other events in the if/else section of webhook listener. Inside those blocks, I am just updating records to save the payment details in my database. It is for one-time payments so events are "payment_intent.created", "charge.succeeded" etc. but the only one with I do anything other than console.log right now is charge.succeeded. Stripe sends multiple events so the webhook is active more than once so it may be the issue. Will the default webhook only gets 1 event from Stripe? Regardless, I am not sending responses from within those event listener conditional blocks, they merely do a database update. If the events are printed in correct order, this is happening after receiving "payment_intent.created" but it is also printed after charge.succeeded event not sure if Stripe would send intent created after charge.succeeded. However, I don't think this is related to order of events in any case because I am not writing a new response from those blocks, only response is the existing ones:
response.json({ received: true });
response.json({ received: true });
and the one of errors after that. One thing I noticed is that, in printing events, there is a field called "pending_webhooks" and it is 2 in all events I received, if it is relevant at all.
Vinny (@Wasp)
Vinny (@Wasp)2w ago
Hmm. I'm not really sure I understand. The best would be to send a github gist of your webhook code so I could take a look
Want results from more Discord servers?
Add your server