Skip to content

Commit 886bf2a

Browse files
committed
Throw Error on malformed responses
1 parent eb64127 commit 886bf2a

5 files changed

Lines changed: 77 additions & 14 deletions

File tree

‎package-lock.json‎

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"devDependencies": {
3434
"@types/aws-lambda": "^8.10.111",
3535
"chai": "^4.3.7",
36+
"chai-as-promised": "^7.1.1",
3637
"chai-http": "^4.3.0",
3738
"eslint": "^7.32.0",
3839
"mocha": "^9.2.2",

‎server.js‎

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,23 +108,30 @@ function initServer(handler, port) {
108108
body = Buffer.from(body, 'base64');
109109
}
110110

111-
res.statusCode = status;
111+
if (status) {
112+
res.statusCode = status;
113+
}
112114

113115
res.end(body);
114116
};
115117

116-
// Run lambda-lambda-lambda
117-
if (isAsyncFunc(handler) || isPromise(handler)) {
118+
try {
118119

119-
// Asynchronous handling.
120-
handler(event)
121-
.then(function(response) {
122-
callback(null, response);
123-
});
124-
} else {
120+
// Run lambda-lambda-lambda
121+
if (isAsyncFunc(handler) || isPromise(handler)) {
125122

126-
// Synchronous handling.
127-
handler(event, null, callback);
123+
// Asynchronous handling.
124+
handler(event)
125+
.then(function(response) {
126+
callback(null, response);
127+
});
128+
} else {
129+
130+
// Synchronous handling.
131+
handler(event, null, callback);
132+
}
133+
} catch (err) {
134+
this.emit('error', Error('Malformed handler method. Exiting..'));
128135
}
129136
});
130137
}).listen(port, () => {

‎test/error/handler.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* @see AWS::Serverless::Function
3+
*/
4+
exports.handler = function(event, context, callback) {
5+
callback(null, {});
6+
};

‎test/server.js‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
'use strict';
22

3-
const chai = require('chai');
4-
const chaiHttp = require('chai-http');
5-
const resetCache = require('resnap')();
3+
const chai = require('chai');
4+
const chaiAsPromised = require('chai-as-promised');
5+
const chaiHttp = require('chai-http');
6+
const resetCache = require('resnap')();
67

8+
chai.use(chaiAsPromised);
79
chai.use(chaiHttp);
810

911
const {expect, request} = chai;
@@ -76,4 +78,29 @@ describe('Handler method', function() {
7678
expect(result.text).to.equal('Success');
7779
});
7880
});
81+
82+
describe('error', function() {
83+
let agent, result;
84+
85+
before(function() {
86+
resetCache();
87+
88+
process.env.TEST_HANDLER = './test/error/handler.js';
89+
90+
const server = require('../server');
91+
agent = request.agent(server);
92+
93+
result = async function() {
94+
await agent.get('/');
95+
};
96+
});
97+
98+
after(function() {
99+
agent.close();
100+
});
101+
102+
it('should throw error', function() {
103+
expect(result()).to.eventually.be.rejectedWith(Error, /Malformed handler method/);
104+
});
105+
});
79106
});

0 commit comments

Comments
 (0)