Skip to content

Commit

Permalink
Update handler-lib.js
Browse files Browse the repository at this point in the history
  • Loading branch information
jayair authored Aug 26, 2021
1 parent 449aaeb commit f64ad66
Showing 1 changed file with 26 additions and 24 deletions.
50 changes: 26 additions & 24 deletions libs/handler-lib.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
import * as debug from "./debug-lib";

export default function handler(lambda) {
return function (event, context) {
return Promise.resolve()
// Start debugger
.then(() => debug.init(event, context))
return async function (event, context) {
let body, statusCode;

// Start debugger
debug.init(event, context);

try {
// Run the Lambda
.then(() => lambda(event, context))
// On success
.then((responseBody) => [200, responseBody])
// On failure
.catch((e) => {
// Print debug messages
debug.flush(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),
}))
// Cleanup debugger
.finally(debug.end);
body = await lambda(event, context);
statusCode = 200;
} catch (e) {
// Print debug messages
debug.flush(e);

body = { error: e.message };
statusCode = 500;
}

// Return HTTP response
return {
statusCode,
body: JSON.stringify(body),
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true,
},
};
};
}

0 comments on commit f64ad66

Please sign in to comment.