Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored, improved error handling #295

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 additions & 19 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ Lambda.prototype._runHandler = function (handler, event, program, context) {

Lambda.prototype._params = function (program, buffer) {
var params = {
FunctionName: program.functionName
+ (program.environment ? '-' + program.environment : '')
+ (program.lambdaVersion ? '-' + program.lambdaVersion : ''),
FunctionName: program.functionName +
(program.environment ? '-' + program.environment : '') +
(program.lambdaVersion ? '-' + program.lambdaVersion : ''),
Code: {
ZipFile: buffer
},
Expand Down Expand Up @@ -206,10 +206,12 @@ Lambda.prototype._fileCopy = function (program, src, dest, excludeNodeModules, c
str = str.substr(0, str.length - 1);
dirBlobs.push(str);
}
if (str.charAt(0) === path.sep)
if (str.charAt(0) === path.sep) {
return path.join(srcAbsolutePath, str);
if (str.indexOf(path.sep) >= 0)
}
if (str.indexOf(path.sep) >= 0) {
return path.join(path.resolve('/**'), str);
}
return str;
}).join(',') + '}';
const dirPatternRegExp = new RegExp(`(${dirBlobs.join('|')})$`);
Expand All @@ -226,11 +228,13 @@ Lambda.prototype._fileCopy = function (program, src, dest, excludeNodeModules, c
return true;
}

if (!minimatch(src, pattern, { matchBase: true }))
if (!minimatch(src, pattern, { matchBase: true })) {
return true;
}
// Directory check. Even if `src` is a directory it will not end with '/'.
if (!dirPatternRegExp.test(src))
if (!dirPatternRegExp.test(src)) {
return false;
}
return !fs.statSync(src).isDirectory();
}
};
Expand All @@ -246,8 +250,8 @@ Lambda.prototype._fileCopy = function (program, src, dest, excludeNodeModules, c

// `_rsync` will be replaced by` _fileCopy`.
Lambda.prototype._rsync = function (program, src, dest, excludeNodeModules, callback) {
var excludes = ['.git*', '*.swp', '.editorconfig', '.lambda', 'deploy.env', '*.log', '/build/'],
excludeGlobs = [];
var excludes = ['.git*', '*.swp', '.editorconfig', '.lambda', 'deploy.env', '*.log', '/build/'];
var excludeGlobs = [];
if (program.excludeGlobs) {
excludeGlobs = program.excludeGlobs.split(' ');
}
Expand Down Expand Up @@ -286,9 +290,9 @@ Lambda.prototype._npmInstall = function (program, codeDirectory, callback) {
`--prefix ${codeDirectory}`,
process.platform === 'win32' ? `--cwd ${codeDirectory}` : null
].join(' ');
var command = program.dockerImage ?
'docker run --rm -v ' + codeDirectory + ':/var/task ' + program.dockerImage + ' npm -s install --production' :
`npm -s install --production ${installOptions}`;
var command = program.dockerImage
? 'docker run --rm -v ' + codeDirectory + ':/var/task ' + program.dockerImage + ' npm -s install --production'
: `npm -s install --production ${installOptions}`;
exec(command, {
maxBuffer: maxBufferSize,
env: process.env
Expand Down Expand Up @@ -317,7 +321,7 @@ Lambda.prototype._postInstallScript = function (program, codeDirectory, callback
maxBuffer: maxBufferSize
}, function (error, stdout, stderr) {
if (error) {
return callback(error + ' stdout: ' + stdout + ' stderr: ' + stderr);
return callback(new Error(`${error} stdout: ${stdout} stderr: ${stderr}`));
}
console.log('\t\t' + stdout);
callback(null);
Expand Down Expand Up @@ -438,9 +442,9 @@ Lambda.prototype._archive = function (program, archiveCallback) {
if (program.deployZipfile && fs.existsSync(program.deployZipfile)) {
return this._readArchive(program, archiveCallback);
}
return program.prebuiltDirectory ?
this._archivePrebuilt(program, archiveCallback) :
this._buildAndArchive(program, archiveCallback);
return program.prebuiltDirectory
? this._archivePrebuilt(program, archiveCallback)
: this._buildAndArchive(program, archiveCallback);
};

Lambda.prototype._archivePrebuilt = function (program, archiveCallback) {
Expand Down Expand Up @@ -632,13 +636,13 @@ Lambda.prototype._updateScheduleEvents = function (scheduleEvents, functionArn,
Lambda.prototype.package = function (program) {
var _this = this;
if (!program.packageDirectory) {
throw 'packageDirectory not specified!';
throw new Error('packageDirectory not specified!');
}
try {
var isDir = fs.lstatSync(program.packageDirectory).isDirectory();

if (!isDir) {
throw program.packageDirectory + ' is not a directory!';
throw new Error(program.packageDirectory + ' is not a directory!');
}
} catch (err) {
if (err.code === 'ENOENT') {
Expand Down Expand Up @@ -729,7 +733,7 @@ Lambda.prototype.deploy = function (program) {
function (_callback) {
// Updating event source(s)
_this._updateEventSources(lambda, params.FunctionName, [], eventSourceList.EventSourceMappings, function (err, results) {
_callback(null, results);
_callback(err, results);
});
},
function (_callback) {
Expand Down
4 changes: 3 additions & 1 deletion test/handler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ exports.handler = (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop =
!!event.callbackWaitsForEmptyEventLoop;

if (event.asyncTest)
if (event.asyncTest) {
setTimeout(() => console.log('sleep 3500 msec'), 3500);
}

/* eslint-disable no-eval */
eval(event.callbackCode);
};
6 changes: 4 additions & 2 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,9 @@ describe('lib/main', function () {
});
afterEach(function () {
hook.unhook();
if (fs.existsSync(postInstallScriptPath))
if (fs.existsSync(postInstallScriptPath)) {
fs.unlinkSync(postInstallScriptPath);
}
});

it('should not throw any errors if no script', function (done) {
Expand All @@ -395,7 +396,8 @@ describe('lib/main', function () {
it('should throw any errors if script fails', function (done) {
fs.writeFileSync(postInstallScriptPath, '___fails___');
lambda._postInstallScript(program, codeDirectory, function (err) {
assert.match(err, /^Error: Command failed:/);
assert.instanceOf(err, Error);
assert.match(err.message, /^Error: Command failed:/);
done();
});
});
Expand Down