Skip to content

Commit 96b6326

Browse files
committed
Added validation to action scope
Match Commander error output format
1 parent bb64a37 commit 96b6326

1 file changed

Lines changed: 30 additions & 6 deletions

File tree

‎server.js‎

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,44 @@ const program = new Command();
2121

2222
program
2323
.usage('[options]')
24+
2425
.option('--handler <path>', 'Lambda@Edge handler script.')
2526
.option('--port <number>', 'HTTP server port number.', 3000)
27+
2628
.action(function(opts) {
27-
const script = process.cwd() + '/' + opts.handler;
29+
const errors = [];
30+
31+
try {
32+
const script = process.cwd() + '/' + opts.handler;
33+
34+
// Validate option values.
35+
if (opts.handler && !fs.existsSync(script) || !opts.handler) {
36+
errors.push(" option '--handler <path>' allows path Lambda@Edge handler script");
37+
}
38+
39+
if (opts.port && !/^[0-9]{2,5}$/.test(opts.port) || !opts.port) {
40+
errors.push(" option '--port <number>' allows up to 5 numeric characters");
41+
}
42+
43+
if (errors.length) {
44+
console.error('error: Invalid script arguments');
45+
46+
throw new Error(errors.join('\n'));
47+
}
2848

29-
if (fs.existsSync(script)) {
3049
const {handler} = require(script);
3150

3251
if (isValidFunc(handler)) {
33-
initServer(handler, opts.port);
34-
} else {
35-
throw new Error('Invalid handler method');
52+
return initServer(handler, opts.port);
3653
}
37-
} else {
54+
55+
throw new Error('error: Invalid handler method');
56+
57+
} catch (err) {
58+
if (err instanceof Error) {
59+
console.error(`${err.message}\n`);
60+
}
61+
3862
this.outputHelp();
3963
}
4064
});

0 commit comments

Comments
 (0)