Function Crash / 502 Gateway Error

Hello,

At 9pm (central) tonight, my 2 functions for my site started returning 502 errors. I haven’t changed anything in the functions for over a month.

My site is: crottyportfolio.netlify.app

Here are test URLs:

Both functions return this error:

TypeError: Class extends value #<Object> is not a constructor or null
    at Object.<anonymous> (/var/task/node_modules/fetch-blob/file.js:66:18)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at require (node:internal/modules/cjs/helpers:121:18)
    at Object.<anonymous> (/var/task/node_modules/formdata-polyfill/esm.min.js:36:27)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)

Here are the functions:

weather-fetch
import fetch from "node-fetch";

export async function handler(event) {
  try {
    const { lat, lon } = event.queryStringParameters;
    const apiKey = process.env.OWM_API_KEY;

    if (isNaN(lat)) {
      throw new Error("Invalid value for parameter: lat");
    }

    if (isNaN(lon)) {
      throw new Error("Invalid value for parameter: lon");
    }

    const url = `https://api.openweathermap.org/data/3.0/onecall?lat=${lat}&lon=${lon}&appid=${apiKey}&units=imperial`;

    const response = await fetch(url);

    if (!response.ok) {
      return { statusCode: response.status, body: response.statusText };
    }

    const data = await response.json();

    return {
      statusCode: 200,
      body: JSON.stringify(data),
    };
  } catch (error) {
    console.error(error);
    return {
      statusCode: 500,
      body: JSON.stringify({ error: error.message, stack: error.stack }),
    };
  }
}
geocoding-fetch
import fetch from "node-fetch";

export async function handler(event) {
  try {
    const { q, lat, lon } = event.queryStringParameters;
    const apiKey = process.env.MAPBOX_API_KEY;
    const types = "postcode,place,neighborhood,address,district,locality";

    let query = "";
    if (!q) {
      throw new Error("Missing required parameter: q");
    } else {
      query = encodeURIComponent(q);
    }

    let proximity = "";
    if (lat && lon) {
      proximity = `${lon},${lat}`;
    }

    const url = `https://api.mapbox.com/geocoding/v5/mapbox.places/${query}.json?proximity=${proximity}&types=${types}&access_token=${apiKey}`;

    const response = await fetch(url);

    if (!response.ok) {
      return { statusCode: response.status, body: response.statusText };
    }

    const data = await response.json();

    return {
      statusCode: 200,
      body: JSON.stringify(data),
    };
  } catch (error) {
    console.error(error);
    return {
      statusCode: 500,
      body: JSON.stringify({ error: error.message, stack: error.stack }),
    };
  }
}

Both functions still work fine on Postman and locally with netlify-cli.

Thanks,
Bobby

I just tried setting the AWS_LAMBDA_JS_RUNTIME variable as per here, but that didn’t fix anything.

Have you seen this topic? It has a couple of proposed solutions:

That worked. Thank you! It’s curious that it was working and then suddenly stopped.

1 Like

glad to hear you found your solution.