Skip to content

Commit 14dc7f5

Browse files
committed
Propagate exception trace on error requiring; accepts event payload as external file, solves #1, solves #3
1 parent 96c97ad commit 14dc7f5

5 files changed

Lines changed: 36 additions & 2 deletions

File tree

lambda-broken-js.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
exports.hello = function(event, context) {
2+
context.succeed('world');

lambda-sandbox-run.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ exports.run = function(lambdaName, handlerName, eventPayLoad){
2828
handlerName = handlerName || 'handler';
2929

3030
if( eventPayLoad ){
31-
eventPayLoad = JSON.parse(eventPayLoad);
31+
if( eventPayLoad.indexOf('file://') === 0 ){
32+
filePath = eventPayLoad.substring(7);
33+
eventPayLoad = require(process.cwd() + '/' + filePath);
34+
}else{
35+
eventPayLoad = JSON.parse(eventPayLoad);
36+
}
3237
}else{
3338
eventPayLoad = {};
3439
}
@@ -37,6 +42,8 @@ exports.run = function(lambdaName, handlerName, eventPayLoad){
3742
lambdaFunction = require(process.cwd() + '/' + lambdaName + '.js');
3843
}catch(exception){
3944
console.error('Invalid module: ' + lambdaName);
45+
console.log(exception);
46+
console.trace(exception);
4047
return null;
4148
}
4249

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aws-lambda-function-sandbox-runner",
3-
"version": "0.1.2",
3+
"version": "0.1.3",
44
"description": "Run AWS Lambda Functions on your local computers, for testing purposes",
55
"main": "lambda-sandbox-run.js",
66
"scripts": {

tests/fixtures/event.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"someProperty":"works"}

tests/lambda-sandbox-run-spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ describe('AWS Lambda Function', function (){
1919
});
2020
});
2121

22+
describe('send event pay load from a file', function () {
23+
it('should call succeed and parse JSON', function (){
24+
spyOn(lambdaRunner.aws, 'succeed').andCallThrough();
25+
var event = 'file://tests/fixtures/event.json';
26+
lambdaRunner.run('lambda-example', 'parseEvent', event);
27+
expect(lambdaRunner.aws.succeed).toHaveBeenCalledWith('works');
28+
});
29+
});
30+
31+
2232
describe('failed execution', function () {
2333
it('should call fail', function (){
2434
spyOn(lambdaRunner.aws, 'fail').andCallThrough();
@@ -38,9 +48,23 @@ describe('AWS Lambda Function', function (){
3848
describe('invalid lambda function', function () {
3949
it('should call console.error with invalid module', function (){
4050
spyOn(console, 'error');
51+
spyOn(console, 'trace');
4152
var lambdaName = 'random';
4253
lambdaRunner.run(lambdaName);
4354
expect(console.error).toHaveBeenCalledWith('Invalid module: ' + lambdaName);
55+
var exception = {code : 'MODULE_NOT_FOUND'};
56+
expect(console.trace).toHaveBeenCalledWith(exception);
57+
});
58+
});
59+
60+
describe('broken lambda function', function () {
61+
it('should call console.error with invalid module', function (){
62+
spyOn(console, 'error');
63+
spyOn(console, 'trace');
64+
var lambdaName = 'lambda-broken-js';
65+
lambdaRunner.run(lambdaName);
66+
expect(console.error).toHaveBeenCalledWith('Invalid module: ' + lambdaName);
67+
expect(console.trace).toHaveBeenCalled();
4468
});
4569
});
4670

0 commit comments

Comments
 (0)