{"message": "Internal server error"} <- I've read the docs, the forums...?

Here’s my yml file ->

service: presto-contact-service

plugins:

  • serverless-plugin-include-dependencies

provider:
name: aws
runtime: nodejs6.10
stage: dev
region: us-west-2

iamRoleStatements:
- Effect: “Allow”
Action:
- “ses:SendEmail”
Resource: “*”

functions:
newcontact:
handler: handler.newcontact
events:
- http:
path: /
method: get
cors: true

and my simple function.

‘use strict’;

const querystring = require(‘querystring’);
const AWS = require(‘aws-sdk’);
const emailer = require(‘ses-email-sender’);

module.exports.newcontact = (event, context, callback) => {

// get the data on the form in an array.
var formAttributes = querystring.parse(event.body);

console.log(formAttributes);

const response = {
statusCode: 200,
headers: {
“Access-Control-Allow-Origin” : “*”,
“Access-Control-Allow-Credentials” : true, // probably 100% unnecessary…
“Content-Type” : “application/json” // doesn’t matter if this is here or not on the response.
},
body: JSON.stringify({ message: ‘great!’, input: event }),
};

callback(null, response);
};

In looking at the log files generated by the lambda function, the function is working great. however, the gateway api is still barfing and spitting back a 502 error. honestly…i’ve seen a bit of comments around this and they generally fall into the category of “you aren’t using the right response object in the callback…”. but i am pretty sure I am here…

Of course a good night’s sleep solved my problem…my client code was still using GET in the .ajax call… event.body will be empty in that instance…so how about some error handling…for an error on a GET.

module.exports.newcontact = (event, context, callback) => {
console.log(event);
console.log(‘iam here!’);

if ( event.httpMethod === ‘GET’) {
const response = {
statusCode: 404,
headers: {
“Access-Control-Allow-Origin” : “*”,
“Access-Control-Allow-Credentials” : true,
“Content-Type” : “application/json”
},
body: JSON.stringify({
message: ‘GET generated an error’,
input: event,
}),
};

callback(null, response);

} else {
console.log(‘POST’);
console.log(event.body);
var formAttributes = querystring.parse(event.body);

console.log(formAttributes['fname']);
console.log(formAttributes['lname']);
console.log(formAttributes['email']);
console.log('iam here - 2!');

sendEmailWithOutAttachment(formAttributes['email'], companyEmail, 'Thank you', 'Thank you for your interest we got your request', function() {
  const response = {
    statusCode: 200,
    headers: {
      "Access-Control-Allow-Origin" : "*",
      "Access-Control-Allow-Credentials" : true,
      "Content-Type" : "application/json"
      },
      body: JSON.stringify({
      message: formAttributes['fname'] + ' Go Serverless v1.0! Your function executed successfully!',
      input: event,
    }),
  };

  callback(null, response);
});

};
};