create subscription stripe failing node.js

I have this code, copied directly from the stripe website:

app.post('/createSubscription',async (req, res) => {

let r = (Math.random() + 1).toString(36).substring(7);
console.log(r);

const subscription = await stripe.subscriptions.create({
  customer: 'cus_' + r,
  items: [
    {price: 'price_1InXJuIPT89VeZtCMeR3xQWf'},
  ],
});
})

however, when i run this code, it gives me an error in my console.

 to show where the warning was created)
(node:38025) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict`

i am not sure exactly what this means, because I have never seen thsi error for stripe before? What am i doing wrong within my subscription funciotn?

Answer

You should try/catch any async function which may throw an error. For example:

let subscription;
try {
  subscription = await stripe.subscriptions.create({
    customer: 'cus_' + r,
    items: [
      { price: 'price_1InXJuIPT89VeZtCMeR3xQWf' },
    ],
  });
} catch (e) {
  console.error(e);
  // Do something about the fact that you encountered an error, example:
  return res.status(500).send('Internal Error');
}

// Do stuff with subscription

This should at least log your error and prevent it from crashing the process. Once you can see the actual error causing the problem then you will be able to reference stripe’s documentation.