question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AWS Lambda callback response format is not correct

See original GitHub issue

I’m using a basic Hello World app to test the Actions SDK v2.0.1 in the Simulator using Bespoken Tools’ Lambda proxy. Note that I had to use node-mocks-http to build a fake event object similar to what the SDK’s Lambda framework expects from an API Gateway event.

The Lambda callback is incorrect in this scenario, leading to a parse error when the Assistant service receives the response from the fullfillment app.

'use strict';

const {actionssdk} = require('actions-on-google');
const expressMockery = require("node-mocks-http");

const app = actionssdk({debug: true});

// intent handler
app.intent('actions.intent.MAIN', (conv) => {
  conv.close('Hello world!');
});

// When using API Gateway it should be as easy as this, but it doesn't work like that with BST proxy
// exports.handler = app;

exports.handler = async (event, context, callback) => {
    try {
        // mock an API Gateway request
        let request = expressMockery.createRequest({
            body: JSON.stringify(event),
            headers: []
        });

        await app(request, context, callback);
    } catch(err) {
        callback(err);
    }
}

The Actions Console Simulator returns the following error:

UnparseableJsonResponse API Version 2: Failed to parse JSON response string with 'INVALID_ARGUMENT' error: "body: Cannot find field.".

As far as I can tell, it is saying that the body field in the repsonse does not exist in the expected response schema.

The Problem appears to be caused how the Lambda framework in the SDK calls the callback. Current implementation (in src/framework/lambda.ts):

const { status, body } = result
callback(null, {
    statusCode: status,
    body: JSON.stringify(body),
})

There is a trivial fix for this but I’m reluctant to post it here until I can establish my employer’s legal position. There is a consensus that raising the issue is acceptable.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Canaincommented, Apr 19, 2018

The current lambda framework implementation builtin to the library expects the handler to be from lambda’s API Gateway. We can explore being able to customize it in the future.

For now, you don’t have to necessarily mock the request as long as you know how to extract the JSON body and headers.

Just call app.handler which is a StandardHandler and is the most basic handler that’s not dependent on any framework.

It accepts a JSON body as the first argument, a JSON map of header key to header values (as a string or string array), and returns a Promise with the result as a StandardResponse which contains the response body as a JSON and the status as a number.

Using this method, you should be able to use whatever input and output format you like.

0reactions
sakthisgcommented, Jul 13, 2019

No.I have not tried. Is this method working for app?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Handle Lambda errors in API Gateway - AWS Documentation
Handle custom Lambda errors in API Gateway. This template translates the integration response body that contains the custom error JSON string to the...
Read more >
Not able to send updated response from callback function in ...
Two things for you to investigate: First check the format of response.body. It must be an object and maybe the third party api...
Read more >
REST API (API Gateway v1) - Serverless Framework
callback (null, response); ... is omitted when not explicitly set to true . ... If not otherwise specified integration type will be AWS...
Read more >
Why are callbacks not being working in AWS Lambda ... - Quora
If your Lambda is on NodeJS 8.10, you might have added the async keyword to your handler function. So if you have the...
Read more >
Comparing async Javascript functions and callbacks on AWS ...
Callbacks are the past, use async functions for new Lambda ... In this case, the response does not wait for the putObject to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found