Skip to content

Commit ffb6dde

Browse files
committed
refactor(exception): implement message handling mechanism for CommitizenException
1 parent ad9d803 commit ffb6dde

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

commitizen/cli.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@
243243

244244
def commitizen_excepthook(type, value, tracekback):
245245
if isinstance(value, CommitizenException):
246+
if value.message:
247+
value.output_method(value.message)
246248
sys.exit(value.exit_code)
247249
else:
248250
original_excepthook(type, value, tracekback)

commitizen/exceptions.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,18 @@ class ExitCode(enum.IntEnum):
2626

2727

2828
class CommitizenException(Exception):
29-
pass
29+
def __init__(self, *args, **kwargs):
30+
self.output_method = kwargs.get("output_method") or out.error
31+
self.exit_code = self.__class__.exit_code
32+
if args:
33+
self.message = args[0]
34+
elif hasattr(self.__class__, "message"):
35+
self.message = self.__class__.message
36+
else:
37+
self.message = ""
38+
39+
def __str__(self):
40+
return self.message
3041

3142

3243
class ExpectedExit(CommitizenException):
@@ -43,18 +54,12 @@ class NoCommitizenFoundException(CommitizenException):
4354

4455
class NotAGitProjectError(CommitizenException):
4556
exit_code = ExitCode.NOT_A_GIT_PROJECT
46-
47-
def __init__(self, *args, **kwargs):
48-
out.error(
49-
"fatal: not a git repository (or any of the parent directories): .git"
50-
)
57+
message = "fatal: not a git repository (or any of the parent directories): .git"
5158

5259

5360
class MissingConfigError(CommitizenException):
5461
exit_code = ExitCode.MISSING_CONFIG
55-
56-
def __init__(self, *args, **kwargs):
57-
out.error("fatal: customize is not set in configuration file.")
62+
message = "fatal: customize is not set in configuration file."
5863

5964

6065
class NoCommitsFoundError(CommitizenException):

0 commit comments

Comments
 (0)