|
1 | 1 | import * as debug from "./debug-lib"; |
2 | 2 |
|
3 | 3 | 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 { |
8 | 11 | // 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 | + }; |
29 | 31 | }; |
30 | 32 | } |
0 commit comments