Skip to content

Commit f64ad66

Browse files
authored
Update handler-lib.js
1 parent 449aaeb commit f64ad66

1 file changed

Lines changed: 26 additions & 24 deletions

File tree

libs/handler-lib.js

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
import * as debug from "./debug-lib";
22

33
export default function handler(lambda) {
4-
return function (event, context) {
5-
return Promise.resolve()
6-
// Start debugger
7-
.then(() => debug.init(event, context))
4+
return async function (event, context) {
5+
let body, statusCode;
6+
7+
// Start debugger
8+
debug.init(event, context);
9+
10+
try {
811
// Run the Lambda
9-
.then(() => lambda(event, context))
10-
// On success
11-
.then((responseBody) => [200, responseBody])
12-
// On failure
13-
.catch((e) => {
14-
// Print debug messages
15-
debug.flush(e);
16-
return [500, { error: e.message }];
17-
})
18-
// Return HTTP response
19-
.then(([statusCode, body]) => ({
20-
statusCode,
21-
headers: {
22-
"Access-Control-Allow-Origin": "*",
23-
"Access-Control-Allow-Credentials": true,
24-
},
25-
body: JSON.stringify(body),
26-
}))
27-
// Cleanup debugger
28-
.finally(debug.end);
12+
body = await lambda(event, context);
13+
statusCode = 200;
14+
} catch (e) {
15+
// Print debug messages
16+
debug.flush(e);
17+
18+
body = { error: e.message };
19+
statusCode = 500;
20+
}
21+
22+
// Return HTTP response
23+
return {
24+
statusCode,
25+
body: JSON.stringify(body),
26+
headers: {
27+
"Access-Control-Allow-Origin": "*",
28+
"Access-Control-Allow-Credentials": true,
29+
},
30+
};
2931
};
3032
}

0 commit comments

Comments
 (0)