Skip to content

Commit 81feefb

Browse files
authored
Merge pull request commitizen-tools#51 from Lee-W/general-expection-for-customize-commit
General expection for customize commit
2 parents 090b56c + dbd299f commit 81feefb

File tree

5 files changed

+47
-5
lines changed

5 files changed

+47
-5
lines changed

commitizen/commands/commit.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
import questionary
66

77
from commitizen import factory, git, out
8+
from commitizen.cz.exceptions import CzException
89

910
NO_ANSWERS = 5
1011
COMMIT_ERROR = 6
1112
NO_COMMIT_BACKUP = 7
1213
NOTHING_TO_COMMIT = 8
14+
CUSTOM_ERROR = 9
1315

1416

1517
class Commit:
@@ -41,7 +43,15 @@ def __call__(self):
4143
# Prompt user for the commit message
4244
cz = self.cz
4345
questions = cz.questions()
44-
answers = questionary.prompt(questions, style=cz.style)
46+
try:
47+
answers = questionary.prompt(questions, style=cz.style)
48+
except ValueError as err:
49+
root_err = err.__context__
50+
if isinstance(root_err, CzException):
51+
out.error(root_err.__str__())
52+
raise SystemExit(CUSTOM_ERROR)
53+
raise err
54+
4555
if not answers:
4656
raise SystemExit(NO_ANSWERS)
4757
m = cz.message(answers)

commitizen/cz/conventional_commits/conventional_commits.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
from commitizen import defaults
44
from commitizen.cz.base import BaseCommitizen
5+
from commitizen.cz.exceptions import CzException
56

67
__all__ = ["ConventionalCommitsCz"]
78

89

9-
class NoSubjectException(Exception):
10+
class NoSubjectException(CzException):
1011
...
1112

1213

@@ -26,7 +27,7 @@ def parse_subject(text):
2627
text = text.strip(".").strip()
2728

2829
if not text:
29-
raise NoSubjectException
30+
raise NoSubjectException("Subject is required.")
3031

3132
return text
3233

commitizen/cz/exceptions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class CzException(Exception):
2+
...

docs/customization.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ The basic steps are:
77
3. expose the class at the end of your file assigning it to `discover_this`
88
4. Create a python package starting with `cz_` using `setup.py`, `poetry`, etc
99

10-
1110
Check an [example](convcomms) on how to configure `BaseCommitizen`.
1211

1312
## Custom commit rules
@@ -122,4 +121,15 @@ That's it, your commitizen now supports custom rules and you can run
122121
cz -n cz_strange bump
123122
```
124123

125-
[convcomms]: https://github.com/Woile/commitizen/blob/master/commitizen/cz/conventional_commits/conventional_commits.py
124+
[convcomms]: https://github.com/Woile/commitizen/blob/master/commitizen/cz/conventional_commits/conventional_commits.py
125+
126+
## Raise Customize Exception
127+
128+
If you wannt `commitizen` to catch your exception and print the message, you'll have to inherit `CzException`.
129+
130+
```python
131+
from commitizen.cz.exception import CzException
132+
133+
class NoSubjectProvidedException(CzException):
134+
...
135+
```

tests/test_commands.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55

66
from commitizen import cmd, commands, defaults
7+
from commitizen.cz.exceptions import CzException
78

89
config = {"name": defaults.name}
910

@@ -92,6 +93,24 @@ def test_commit_when_nothing_to_commit(mocker):
9293
assert err.value.code == commands.commit.NOTHING_TO_COMMIT
9394

9495

96+
def test_commit_when_customized_expected_raised(mocker, capsys):
97+
_err = ValueError()
98+
_err.__context__ = CzException("This is the root custom err")
99+
100+
prompt_mock = mocker.patch("questionary.prompt")
101+
prompt_mock.side_effect = _err
102+
103+
with pytest.raises(SystemExit) as err:
104+
commit_cmd = commands.Commit(config, {})
105+
commit_cmd()
106+
107+
assert err.value.code == commands.commit.CUSTOM_ERROR
108+
109+
# Assert only the content in the formatted text
110+
captured = capsys.readouterr()
111+
assert "This is the root custom err" in captured.err
112+
113+
95114
def test_example():
96115
with mock.patch("commitizen.out.write") as write_mock:
97116
commands.Example(config)()

0 commit comments

Comments
 (0)