Netlify function returns 500 only in production and no logs

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),
  };
};
1 Like

hey there, thanks for writing in.

i’ve asked the person who knows most about functions on our team to weigh in when he gets a chance - we havent forgotten about you!

2 Likes

I’m having a very similar issue. Not reading from a config file but I have two functions, one of which isn’t loggin and returning a 500 status. Note : I also have this same repo deployed for 8 other sites, all of which work fine. I’ve tried clearing cache and re-triggering the deploy a number of times. Thanks!

Update : It’s working! (kind of) I just added a console.log statement and that seemed to trigger a rebuilding of the function (or something like that). But then when I reverted it and triggered a re-deploy, it seemed to have used the file that had already been built into Netlify’s cache (seems like smart/aggressive caching, but killin my vibe right now).

Is there a way to force the rebuild/transpiling of a lambda function without actually altering the code?

1 Like

Hi @Resto, our system does a hash on your function to check if it has changed. If it hasn’t, our system won’t deploy a new one since the previous one is identical according to the hash. You can add some whitespace or change around some code comments to change the hash. Let me know if that works for you.

@Dennis the problem is I’m using Vue.js w/the webpack compiler so it’s going to scrape out any comments or whitespace. I guess I’ll have to add an // eslint-disable comment or something and have a dummy console.log to force a hash update. Would be helpful if there was a way to force a cachebust on a specfic file. Is there? Thanks!

A comment would change the hash of your function. Does your eslint config disallow comments as well? As far as forcing a new function deploy, changing the content of the file slightly (even without any functional change) is the method. Let me know how it goes.

Yeah… I truly can’t say much because I’ve been meaning to generate a test repo to prove out what I believe I’ve felt in my live projects, but I believe something breaks when you deploy a site with site content and functions, then re-trigger a deploy from the web UI. Something disconnects in that workflow and the function no longer works correctly from the site. I should’ve written down more when it happened but I believe I was feeling this specifically with event-trigger functions rather than direct-called ones. Not sure if that fits here but that’s my 2c. I tend to follow the workflow of: always trigger a commit-based deploy when wanting to make any changes to functions or site, even if just wanting to change ENV vars in the web UI. Change them then push a new commit on the site :thinking: Just my 2c. Will be watching this thread closely.


Jon

Hmm. Builds triggered in the UI triggers a build the same way as a buildhook-triggered build would be and we don’t treat it any differently. Is the issue only with functions deployed that way? Perhaps if you run into that issue again, you can start a new topic so we can look into that specific issue?

It seems like it’s related strictly to Functions, yes. Don’t have time to dig into this any more at the moment unfortunately!

No problem, @Resto. Please get back in touch when you run in to it!