Function started responding with 502s overnight

I have a simple form that takes data and subscribes someone to a Mailchimp list. It was working fine until someone reported errors overnight (built the form to handle errors and display them to the user).

Turns out the lambdas are still running fine, but started responding with 502 Bad Gateway overnight - seemlingly without any changes that I made.

I’ve tried redeploying a couple of times, but it’s not fixing the problem. I’m constructing responses with 200 status codes in the lambda and those are not making it through, however, the response messages are.

Able to reproduce via postman.

Thinking this is a Netlify issue, but maybe it’s something I’m doing. Pasted my function code below:

Function code below:

require('dotenv').config()

const Mailchimp = require('mailchimp-api-v3')

const mailChimpAPI = process.env.MAILCHIMP_API_KEY
const mailChimpListID = process.env.MAILCHIMP_LIST_ID

const mailchimp = new Mailchimp(mailChimpAPI)

exports.handler = function(event, context, callback) {
  const data = JSON.parse(event.body)

  console.log(data)

  if (data.email && data.selectedState) {
    mailchimp
      .post(
        '/lists/' + mailChimpListID + '/members?skip_merge_validation=true',
        {
          email_address: data.email,
          merge_fields: {
            STATE: data.selectedState
          },
          status: 'subscribed'
        }
      )
      .then(function(results) {
        console.log(results)
        const response = {
          statusCode: 200,
          headers: {
            'Access-Control-Allow-Origin': '*', 
            'Access-Control-Allow-Credentials': true 
          },
          body: JSON.stringify({ status: 'Success!' })
        }
        callback(null, response)
      })
      .catch(function(err) {
        console.log(err)

        if (err.title == 'Member Exists') {
          err.detail = 'This email address is already subscribed.'
        }

        const response = {
          statusCode: 200,
          headers: {
            'Access-Control-Allow-Origin': '*', 
            'Access-Control-Allow-Credentials': true 
          },
          body: JSON.stringify({
            status: 'Error',
            title: err.title,
            detail: err.detail
          })
        }
        callback(null, response)
      })
  } else {
    const response = {
      statusCode: 400,
      headers: {
        'Access-Control -Allow-Origin': '*', 
        'Access-Control-Allow-Credentials': true 
      },
      body: JSON.stringify({
        status: 'Error',
        message: 'One or more fields are missing'
      })
    }
    callback(null, response)
  }
}

Hi @pjatx and welcome to our community! Could you give me the exact URL you call the function with, so I can try to correlate with our internal logs? I don’t see any of the sites in your main “pjatx” account as using functions.

Definitely @fool! It’s under a team account that was provisioned for COVID-19 stuff.

https://www.tracetozero.org/.netlify/functions/subscribe

Just hit it with postman again and still getting 502’s back. Thanks for looking into it.

Hey @pjatx,
We dug into this a bit and are seeing this error in our logs:

“error decoding lambda response: json: cannot unmarshal bool into Go struct field lambdaInvokeResponse.headers of type string”

Some other people seem to have run across a similar error:

Checking out your response headers and that error message, I would try making true a string instead of a boolean. I’m not yet sure if this is an encoding error on our end (as suggested in that other post) but wanted to share the possible fix. Want to give it a shot and let us know how it goes?

Thanks @jen!

Changing true -> "true" in my response headers did the trick.

2 Likes

Update on this: we recently rolled out a change so that we’ll accept both integers and boolean values as header values :tada:

1 Like