Skip to content

Commit e9aee89

Browse files
committed
Add logging to -W option
1 parent abb725b commit e9aee89

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/lambdalocal.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,31 @@ export function execute(opts) {
5252
};
5353

5454
export function watch(opts) {
55+
if (!opts.verboseLevel){
56+
opts.verboseLevel = 0;
57+
}
5558
const server = createServer(async function(req: IncomingMessage, res: ServerResponse) {
59+
var log_msg = `${req.method} ${req.headers.host} ${req.url}`;
60+
function handle_error(error){
61+
logger.log('warn', log_msg + ` -> ${error}`);
62+
res.statusCode = 500;
63+
return res.end(JSON.stringify({ error }));
64+
}
5665
try {
5766
if(req.headers['content-type'] !== 'application/json') throw 'Invalid header Content-Type (Expected application/json)';
58-
if(req.method !== 'POST') throw 'Invalid http method (Expected POST)';
5967
_getRequestPayload(req, async (error, result) => {
60-
if(error) throw error;
61-
const data = await execute({ ...opts, event: () => result })
62-
return res.end(JSON.stringify({ data }));
68+
try {
69+
if(error) throw error;
70+
const data = await execute({ ...opts, event: () => result });
71+
const ans = JSON.stringify({ data });
72+
logger.log('info', log_msg + ` -> OK (${ans.length * 2} bytes)`);
73+
return res.end(ans);
74+
} catch(error) {
75+
return handle_error(error);
76+
}
6377
});
6478
} catch(error) {
65-
res.statusCode = 500;
66-
return res.end(JSON.stringify({ error }));
79+
return handle_error(error);
6780
}
6881
});
6982
server.listen(opts.port, function() {

test/functs/test-func-echo.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*
2+
* Lambda function used for basic test.
3+
*/
4+
exports.handler = function(event, context) {
5+
context.succeed(event);
6+
};

0 commit comments

Comments
 (0)