Skip to content

Commit 1d4b96e

Browse files
marioneblLinusU
authored andcommitted
fix: harden adapter callback error handling (#314)
* 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
1 parent 2dd393f commit 1d4b96e

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

src/cli/strategies/git-cz.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,14 @@ function gitCz(rawGitArgs, environment, adapterConfig) {
6262
console.log(`cz-cli@${cliPackageJson.version}, ${adapterPackageJson.name}@${adapterPackageJson.version}\n`);
6363
commit(sh, inquirer, process.cwd(), prompter, {
6464
args: parsedGitCzArgs,
65-
disableAppendPaths:true,
66-
emitData:true,
67-
quiet:false,
65+
disableAppendPaths: true,
66+
emitData: true,
67+
quiet: false,
6868
retryLastCommit
6969
}, function(error) {
7070
if (error) {
7171
throw error;
7272
}
73-
// console.log('commit happened');
7473
});
7574
});
7675

src/commitizen/commit.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from 'path';
2+
23
import homeOrTmp from 'home-or-tmp';
34
import dedent from 'dedent';
45
import {commit as gitCommit, log} from '../git';
@@ -10,12 +11,10 @@ export default commit;
1011
* Takes all of the final inputs needed in order to make dispatch a git commit
1112
*/
1213
function dispatchGitCommit(sh, repoPath, template, options, overrideOptions, done) {
13-
1414
// Commit the user input -- side effect that we'll test
15-
gitCommit(sh, repoPath, template, { ...options, ...overrideOptions }, function() {
16-
done(template);
15+
gitCommit(sh, repoPath, template, { ...options, ...overrideOptions }, function(error) {
16+
done(error, template);
1717
});
18-
1918
}
2019

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

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

4140
} else {
4241
// Get user input -- side effect that is hard to test
43-
prompter(inquirer, function(template, overrideOptions) {
44-
42+
prompter(inquirer, function(error, template, overrideOptions) {
43+
// Allow adapters to error out
44+
// (error: Error?, template: String, overrideOptions: Object)
45+
if (!(error instanceof Error)) {
46+
overrideOptions = template;
47+
template = error;
48+
error = null;
49+
}
50+
51+
if (error) {
52+
return done(error);
53+
}
54+
4555
// We don't want to add retries to the cache, only actual commands
4656
cache.setCacheValueSync(cachePath, repoPath, { template, options, overrideOptions });
4757
dispatchGitCommit(sh, repoPath, template, options, overrideOptions, done);
48-
});
58+
});
4959
}
5060

51-
}
61+
}

src/git/commit.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function commit(sh, repoPath, message, options, done) {
3131
stdio: options.quiet ? 'ignore' : 'inherit'
3232
}, function(error, stdout, stderror) {
3333
if (error) {
34+
error.message = [error.message, stderror].filter(Boolean).join('\n');
3435
return done(error);
3536
}
3637
done();

0 commit comments

Comments
 (0)