Skip to content

Instantly share code, notes, and snippets.

@theburningmonk
Created August 28, 2017 13:15
Show Gist options
  • Save theburningmonk/c1eb912a113cad27e02d6244dad15f8f to your computer and use it in GitHub Desktop.
Save theburningmonk/c1eb912a113cad27e02d6244dad15f8f to your computer and use it in GitHub Desktop.
'use strict';
const co = require('co');
const log = require('./log');
const reqContext = require('./requestContext');
function setRequestContext (event, context) {
// extracts correlation IDs, User-Agent, etc. from HTTP headers
// and stores them in the requestContext
};
function OK (result) {
// return 200 response
};
function createApiHandler (f) {
return co.wrap(function* (event, context, cb) {
console.log(JSON.stringify(event));
reqContext.clearAll();
try {
setRequestContext(event, context);
} catch (err) {
log.warn(`couldn't set current request context: ${err}`, err.stack);
}
try {
let result = yield Promise.resolve(f(event, context));
result = result || {};
log.info('SUCCESS', JSON.stringify(result));
cb(null, OK(result));
} catch (err) {
log.error("Failed to process request", err);
cb(err);
}
});
}
module.exports = createApiHandler;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment