Skip to content

Commit

Permalink
fix: harden adapter callback error handling (#314)
Browse files Browse the repository at this point in the history
*  minor code cleanups (cli/strategies/git-cz.js)
*  pass error as first argument into `dispatchGitCommit` callback
*  handle errors raised by commitizen adapters via optional error argument
*  attach stderr to error message in case of `git commit` errors

Fix #308
  • Loading branch information
marionebl authored and LinusU committed Aug 6, 2016
1 parent 2dd393f commit 1d4b96e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
7 changes: 3 additions & 4 deletions src/cli/strategies/git-cz.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,14 @@ function gitCz(rawGitArgs, environment, adapterConfig) {
console.log(`cz-cli@${cliPackageJson.version}, ${adapterPackageJson.name}@${adapterPackageJson.version}\n`);
commit(sh, inquirer, process.cwd(), prompter, {
args: parsedGitCzArgs,
disableAppendPaths:true,
emitData:true,
quiet:false,
disableAppendPaths: true,
emitData: true,
quiet: false,
retryLastCommit
}, function(error) {
if (error) {
throw error;
}
// console.log('commit happened');
});
});

Expand Down
28 changes: 19 additions & 9 deletions src/commitizen/commit.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path';

import homeOrTmp from 'home-or-tmp';
import dedent from 'dedent';
import {commit as gitCommit, log} from '../git';
Expand All @@ -10,12 +11,10 @@ export default commit;
* Takes all of the final inputs needed in order to make dispatch a git commit
*/
function dispatchGitCommit(sh, repoPath, template, options, overrideOptions, done) {

// Commit the user input -- side effect that we'll test
gitCommit(sh, repoPath, template, { ...options, ...overrideOptions }, function() {
done(template);
gitCommit(sh, repoPath, template, { ...options, ...overrideOptions }, function(error) {
done(error, template);
});

}

/**
Expand All @@ -31,7 +30,7 @@ function commit(sh, inquirer, repoPath, prompter, options, done) {

// We want to use the last commit instead of the current commit,
// so lets override some options using the values from cache.
let {
let {
options: retryOptions,
overrideOptions: retryOverrideOptions,
template: retryTemplate
Expand All @@ -40,12 +39,23 @@ function commit(sh, inquirer, repoPath, prompter, options, done) {

} else {
// Get user input -- side effect that is hard to test
prompter(inquirer, function(template, overrideOptions) {

prompter(inquirer, function(error, template, overrideOptions) {
// Allow adapters to error out
// (error: Error?, template: String, overrideOptions: Object)
if (!(error instanceof Error)) {
overrideOptions = template;
template = error;
error = null;
}

if (error) {
return done(error);
}

// We don't want to add retries to the cache, only actual commands
cache.setCacheValueSync(cachePath, repoPath, { template, options, overrideOptions });
dispatchGitCommit(sh, repoPath, template, options, overrideOptions, done);
});
});
}

}
}
1 change: 1 addition & 0 deletions src/git/commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function commit(sh, repoPath, message, options, done) {
stdio: options.quiet ? 'ignore' : 'inherit'
}, function(error, stdout, stderror) {
if (error) {
error.message = [error.message, stderror].filter(Boolean).join('\n');
return done(error);
}
done();
Expand Down

0 comments on commit 1d4b96e

Please sign in to comment.