Skip to content

Instantly share code, notes, and snippets.

@owfm
Created May 12, 2020 13:51
Show Gist options
  • Save owfm/d691dfdbc9aa0f52da1fb29514533982 to your computer and use it in GitHub Desktop.
Save owfm/d691dfdbc9aa0f52da1fb29514533982 to your computer and use it in GitHub Desktop.
// libs/handler-lib.js
export default function handler(lambda) {
return function (event, context) {
return Promise.resolve()
// Run the Lambda
.then(() => lambda(event, context))
// On success
.then((responseBody) => [200, responseBody])
// On failure
.catch((e) => {
return [500, { error: e.message }];
})
// Return HTTP response
.then(([statusCode, body]) => ({
statusCode,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true,
},
body: JSON.stringify(body),
}));
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment