Created
August 18, 2016 10:07
-
-
Save rotemtam/8341c06c38455bf1acdf271db1f3e96c to your computer and use it in GitHub Desktop.
LambdaRunner - interface between ES6 generators and lambda functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const co = require('co'); | |
module.exports = function(lambda) { | |
return function(e, ctx, cb) { | |
co( | |
function* () { | |
let result = yield lambda(e, ctx); | |
if (cb) { | |
cb(null, result) | |
} | |
ctx.succeed(result); | |
} | |
) | |
.catch( | |
function(err){ | |
let re = /Not found:/, msg; | |
if( err && re.test(err)) { | |
msg = err.message || err || "Not found: could not find resource"; | |
} else { | |
msg = `Error: ${err.message || 'Internal error'}` | |
} | |
ctx.fail(msg); | |
if(cb) { | |
cb(err) | |
} | |
} | |
); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment