forked from paullessing/demo-env-slackbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
34 lines (32 loc) · 1.14 KB
/
handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const github = require('./github');
const environments = require('./environments');
const slackCommand = require('./slack-command');
function handleRequest(handler) {
return function(event, context, callback) {
console.log('Handling request');
return Promise.resolve()
.then(() => handler(event))
.then((result) => {
console.log('Success', result);
if (result && result.statusCode) {
callback(null, result);
} else if (result) {
callback(null, { statusCode: 200, body: JSON.stringify(result) });
} else {
callback(null, { statusCode: 204 });
}
}).catch((e) => {
console.log('Failure', e);
if (e.statusCode) {
callback(null, e);
} else {
console.log('Unhandled exeption:', e);
callback(null, { statusCode: 500, body: JSON.stringify(e) });
}
});
}
}
module.exports.post = handleRequest(github.onPush);
module.exports.getAll = handleRequest(environments.getActive);
module.exports.slackCommand = handleRequest(slackCommand.handle);
module.exports.autoclaim = handleRequest(environments.autoclaim);