Site name: silly-banach-737501.netlify.app
I’ve got 2 lambda functions, both read a local ‘products.json’ file.
The first one works.
The second one suddenly stopped working when I modified the JSON file. (Which was still valid and didn’t break the first function.)
The developer experience was very poor:
It worked locally and I couldn’t find a way to reproduce the problem.
In production, the function would return 500 and not much else.
The logs for my broken function would only show a spinning slash.
I tried adding console.log()
s to the function but none got printed.
By looking at the git history I finally figured out that my not-broken JSON was somehow the problem, but I was in complete darkness: no logs or ways to reproduce the problem, just “500”.
Did I miss something about lambda function debugging?
My problematic function (very much inspired by this fun tutorial):
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
const inventory = require('./data/products.json');
exports.handler = async (event) => {
const { sku } = JSON.parse(event.body);
const product = inventory.find((p) => p.sku === sku);
const session = await stripe.checkout.sessions.create({
payment_method_types: ['card'],
billing_address_collection: 'auto',
shipping_address_collection: {
allowed_countries: ['US', 'CA'],
},
success_url: `${process.env.URL}/success`,
cancel_url: process.env.URL,
line_items: [
{
name: product.name,
description: product.description,
images: [product.image],
amount: product.amount,
currency: product.currency,
quantity: 1,
},
],
});
return {
statusCode: 200,
body: JSON.stringify({
sessionId: session.id,
publishableKey: process.env.STRIPE_PUBLISHABLE_KEY,
}),
};
};
My first function:
const products = require('./data/products.json');
exports.handler = async () => {
return {
statusCode: 200,
body: JSON.stringify(products),
};
};