Connecting lambda-function with Stripe: ECONNRESET error

I am trying to connect an application to Stripe API. The app makes a call to a lambda-function and then this function touches the Stripe API. There are several different functions doing the same flow. It works fine locally, but when deployed to Netlify, I am keep getting a ECONNRESET error. And the errors apparently happen randomly - sometimes it work just fine. Below is an example of the error:
[Error]: An error occurred with our connection to Stripe.
at /var/task/src/getCardList.js:1:3796
at ClientRequest. (/var/task/src/getCardList.js:1:6801)
at ClientRequest.emit (events.js:315:20)
at TLSSocket.socketOnEnd (_http_client.js:453:9)
at TLSSocket.emit (events.js:327:22)
at endReadableNT (_stream_readable.js:1221:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
type: ‘’,
raw: {
message: ‘An error occurred with our connection to Stripe.’,
detail: Error: socket hang up
at connResetException (internal/errors.js:609:14)
at TLSSocket.socketOnEnd (_http_client.js:453:23)
at TLSSocket.emit (events.js:327:22)
at endReadableNT (_stream_readable.js:1221:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
code: ‘ECONNRESET’
}
},
rawType: undefined,
code: undefined,
doc_url: undefined,
param: undefined,
detail: Error: socket hang up
at connResetException (internal/errors.js:609:14)
at TLSSocket.socketOnEnd (_http_client.js:453:23)
at TLSSocket.emit (events.js:327:22)
at endReadableNT (_stream_readable.js:1221:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
code: ‘ECONNRESET’
},
headers: undefined,
requestId: undefined,
statusCode: undefined,
charge: undefined,
decline_code: undefined,
payment_intent: undefined,
payment_method: undefined,
setup_intent: undefined,
source: undefined
}
Does anybody have any idea how to debug it?

Hey @Fila! :wave:t2:
Welcome to the Community :slight_smile:

Can you check the logs for your Function and see what those reported? You should be able to find them in your site’s dashboard.

The log you posted looks like a client-side log and it’ll be tough to get any insight from it.

You can also feel free to share the Function code here as well :+1:t2:


Jon

Hi Jon! Thanks for writing back.
Below is one of the function that was getting the error. Others with a very similar approach were also getting the same error. And the errors were intermittent, hard to establish a pattern.

require("dotenv").config({ silent: true })
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY)

exports.handler = async event => {
  const { stripeCustomerId } = JSON.parse(event.body)
  try {
    const { data } = await stripe.paymentMethods.list({
      customer: stripeCustomerId,
      type: "card"
    })
    return {
      statusCode: 200,
      body: JSON.stringify(data),
      headers: {
        "content-type": "application/json"
      }
    }
  } catch (e) {
    return {
      statusCode: 500,
      body: JSON.stringify(e)
    }
  }
}

The error I posted was the object I captured inside the catch statement, and I got it from the function log pannel. Not sure where to find the logs for the request/response my lambda function was sending.

Update: This morning, I started getting a different error: Error: write EPIPE. I changed a few things in my code. One thing is a flag inside the stripe call that sets the numbers of retries. I set it for 3 attempts and apparently it is stable now. However, I am afraid the error is related to a memory leak or something and will be back without notice.

Thanks!
Fila

hi there, i am not the go-to functions person, but i do know we have some stripe examples in our functions playground repo - maybe there is something you can take a look at here?