Reputation: 571
I can't seem to figure this one out. I have a POST route that is creating a transaction through Stripe using Stripe's Node package.
It works fine until there is an error, say for an example a card with insufficient funds. When the error occurs it calls the catch()
block as it should. In the catch block I have return next(err)
. I am sure to use return
because there is other code after the promise that should only run upon success of stripe.charges.create
.
However, even with return next(err)
, it seems as if it continues to run, as illustrated by the console.log
I have in place. Additionally, the route returns a 200 status with the "Executed route" text.I also have a global error catcher right after this route. Please feel free to view the images attached below for more detail.
Anyone have any ideas as to why this may be occurring? I've done this structure plenty of times before with no issues; not quite sure what is going on.
Thanks for any input!
Upvotes: 0
Views: 198
Reputation: 82136
That's because catch
is not in the same scope as the outer code.
You already using async
/ await
here so just use try...catch
instead of the callback
try {
const charge = await stripe.charges.create({
amount: 100,
currency: 'usd'
source: req.body.cardSource
});
console.log('Running');
res.send('Executed route');
} catch (e) {
return next(e);
}
Upvotes: 1