Skip to content

Commit 99f2f18

Browse files
authored
feat: Add custom exception messages (#13)
- Raise ValueError instead of generic Exception - Create Custom Exceptions for each validation - Update folder structure
1 parent 1b4a811 commit 99f2f18

File tree

14 files changed

+203
-63
lines changed

14 files changed

+203
-63
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,23 @@ import checkarg.none_type as NoneType
4343
import checkarg.number as Number
4444
import checkarg.text as Text
4545

46-
from checkarg.exceptions import ArgumentNoneException, ArgumentException, ArgumentOutOfRangeException
46+
from checkarg.exceptions import ArgumentNoneError, ArgumentError, ArgumentOutOfRangeError
4747

4848

4949
def lookup_name(mapping, key: str, default: int):
5050
try:
5151
Number.is_greater(default, 0)
52-
except ArgumentOutOfRangeException:
52+
except ArgumentOutOfRangeError:
5353
return None
5454

5555
try:
5656
NoneType.is_not_none(mapping)
57-
except ArgumentNoneException:
57+
except ArgumentNoneError:
5858
return default
5959

6060
try:
6161
Text.is_not_empty(key)
62-
except (ArgumentException, ArgumentNoneException) as e:
62+
except (ArgumentError, ArgumentNoneError) as e:
6363
return default
6464

6565
return mapping[key]

checkarg/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
__doc__ = "__init__"
2-
__version__ = "0.0.2"
2+
__version__ = "0.1.0"

checkarg/exceptions.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

checkarg/exceptions/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from .error_messages import (
2+
DefaultErrorMessages,
3+
NoneTypeErrorMessages,
4+
NumberErrorMessages,
5+
TextErrorMessages,
6+
)
7+
from .exceptions import ArgumentError, ArgumentNoneError, ArgumentOutOfRangeError

checkarg/exceptions/error_messages.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
class DefaultErrorMessages:
2+
@staticmethod
3+
def default_argument_message(argument_name):
4+
return f"Argument name: {argument_name}"
5+
6+
7+
class NoneTypeErrorMessages:
8+
@staticmethod
9+
def not_none_message(argument_name):
10+
return f"Argument {argument_name} shouldn't be None"
11+
12+
13+
class NumberErrorMessages:
14+
@staticmethod
15+
def is_greater_message(argument_name, value, condition_value):
16+
return f"Argument {argument_name} with value {value} should be greater than {condition_value}"
17+
18+
@staticmethod
19+
def is_lower_message(argument_name, value, condition_value):
20+
return f"Argument {argument_name} with value {value} should be lower than {condition_value}"
21+
22+
@staticmethod
23+
def is_greater_or_equals_message(argument_name, value, condition_value):
24+
return f"Argument {argument_name} with value {value} should be greater than or equal to {condition_value}"
25+
26+
@staticmethod
27+
def is_lower_or_equals_message(argument_name, value, condition_value):
28+
return f"Argument {argument_name} with value {value} should be lower than or equal to {condition_value}"
29+
30+
31+
class TextErrorMessages:
32+
@staticmethod
33+
def is_not_whitespace_message(argument_name):
34+
return f"Argument {argument_name} shouldn't be only whitespaces"
35+
36+
@staticmethod
37+
def is_not_empty_message(argument_name):
38+
return f"Argument {argument_name} shouldn't be empty"
39+
40+
@staticmethod
41+
def is_alphanumeric_message(argument_name, value):
42+
return f"Argument {argument_name} should be alphanumeric instead of {value}"
43+
44+
@staticmethod
45+
def is_alphabetic_message(argument_name, value):
46+
return f"Argument {argument_name} should be alphabetic instead of {value}"
47+
48+
@staticmethod
49+
def is_number_message(argument_name, value):
50+
return f"Argument {argument_name} should be a number instead of {value}"
51+
52+
@staticmethod
53+
def is_integer_message(argument_name, value):
54+
return f"Argument {argument_name} should be an integer instead of {value}"
55+
56+
@staticmethod
57+
def is_lowercase_message(argument_name, value):
58+
return f"Argument {argument_name} should be lowercase instead of {value}"
59+
60+
@staticmethod
61+
def is_uppercase_message(argument_name, value):
62+
return f"Argument {argument_name} should be uppercase instead of {value}"
63+
64+
@staticmethod
65+
def has_length_message(argument_name, lenght_value, conditional_lenght):
66+
return f"Argument {argument_name} should have lenght of {conditional_lenght} instead of {lenght_value}"
67+
68+
@staticmethod
69+
def has_length_between_message(
70+
argument_name, lenght_value, min_conditional_lenght, max_conditional_lenght
71+
):
72+
return f"""
73+
Argument {argument_name} should have lenght between
74+
{min_conditional_lenght} and {max_conditional_lenght}
75+
instead of {lenght_value}"""
76+
77+
@staticmethod
78+
def is_equal_to_message(argument_name, value, conditional_value):
79+
return f"Argument {argument_name} with value {value} should be equal to {conditional_value}"
80+
81+
@staticmethod
82+
def is_not_equal_to_message(argument_name, value):
83+
return f"Argument {argument_name} shouldn't be equal to {value}"

checkarg/exceptions/exceptions.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from .error_messages import DefaultErrorMessages
2+
3+
4+
class ArgumentError(ValueError):
5+
def __init__(self, message, argument_name):
6+
self.message = (
7+
message
8+
if message
9+
else DefaultErrorMessages.default_argument_message(argument_name)
10+
)
11+
12+
13+
class ArgumentNoneError(ArgumentError):
14+
pass
15+
16+
17+
class ArgumentOutOfRangeError(ArgumentError):
18+
pass

checkarg/none_type.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
from checkarg.exceptions import ArgumentNoneException
1+
from checkarg.exceptions import ArgumentNoneError, NoneTypeErrorMessages
22

33

44
def is_not_none(value, argument_name: str = None, exception: Exception = None):
55
if value is None:
6-
raise ArgumentNoneException(argument_name) if exception is None else exception
6+
raise ArgumentNoneError(
7+
NoneTypeErrorMessages.not_none_message(argument_name), argument_name
8+
) if exception is None else exception

checkarg/number.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Union
22

3-
from checkarg.exceptions import ArgumentOutOfRangeException
3+
from checkarg.exceptions import ArgumentOutOfRangeError, NumberErrorMessages
44

55

66
def is_greater(
@@ -10,8 +10,11 @@ def is_greater(
1010
exception: Exception = None,
1111
):
1212
if value < condition_value:
13-
raise ArgumentOutOfRangeException(
14-
argument_name
13+
raise ArgumentOutOfRangeError(
14+
NumberErrorMessages.is_greater_message(
15+
argument_name, value, condition_value
16+
),
17+
argument_name,
1518
) if exception is None else exception
1619

1720

@@ -22,8 +25,9 @@ def is_lower(
2225
exception: Exception = None,
2326
):
2427
if value > condition_value:
25-
raise ArgumentOutOfRangeException(
26-
argument_name
28+
raise ArgumentOutOfRangeError(
29+
NumberErrorMessages.is_lower_message(argument_name, value, condition_value),
30+
argument_name,
2731
) if exception is None else exception
2832

2933

@@ -34,8 +38,11 @@ def is_greater_or_equals(
3438
exception: Exception = None,
3539
):
3640
if value < condition_value:
37-
raise ArgumentOutOfRangeException(
38-
argument_name
41+
raise ArgumentOutOfRangeError(
42+
NumberErrorMessages.is_greater_or_equals_message(
43+
argument_name, value, condition_value
44+
),
45+
argument_name,
3946
) if exception is None else exception
4047

4148

@@ -46,6 +53,9 @@ def is_lower_or_equals(
4653
exception: Exception = None,
4754
):
4855
if value > condition_value:
49-
raise ArgumentOutOfRangeException(
50-
argument_name
56+
raise ArgumentOutOfRangeError(
57+
NumberErrorMessages.is_lower_or_equals_message(
58+
argument_name, value, condition_value
59+
),
60+
argument_name,
5161
) if exception is None else exception

checkarg/text.py

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
from typing import List, Union
22

33
import checkarg.none_type as NoneType
4-
from checkarg.exceptions import ArgumentException
4+
from checkarg.exceptions import ArgumentError, TextErrorMessages
55

66

77
def is_not_whitespace(
88
value: str, argument_name: str = None, exception: Exception = None
99
) -> None:
1010
NoneType.is_not_none(value, argument_name, exception)
1111
if len(value.strip()) <= 0:
12-
raise ArgumentException(argument_name) if exception is None else exception
12+
raise ArgumentError(
13+
TextErrorMessages.is_not_whitespace_message(argument_name), argument_name
14+
) if exception is None else exception
1315

1416

1517
def is_not_empty(
@@ -19,23 +21,30 @@ def is_not_empty(
1921
if isinstance(value, str):
2022
value = value.strip()
2123
if len(value) <= 0:
22-
raise ArgumentException(argument_name) if exception is None else exception
24+
raise ArgumentError(
25+
TextErrorMessages.is_not_empty_message(argument_name), argument_name
26+
) if exception is None else exception
2327

2428

2529
def is_alphanumeric(
2630
value: str, argument_name: str = None, exception: Exception = None
2731
) -> None:
2832
NoneType.is_not_none(value, argument_name, exception)
2933
if not value.isalnum():
30-
raise ArgumentException(argument_name) if exception is None else exception
34+
raise ArgumentError(
35+
TextErrorMessages.is_alphanumeric_message(argument_name, value),
36+
argument_name,
37+
) if exception is None else exception
3138

3239

3340
def is_alphabetic(
3441
value: str, argument_name: str = None, exception: Exception = None
3542
) -> None:
3643
NoneType.is_not_none(value, argument_name, exception)
3744
if not value.isalpha():
38-
raise ArgumentException(argument_name) if exception is None else exception
45+
raise ArgumentError(
46+
TextErrorMessages.is_alphabetic_message(argument_name, value), argument_name
47+
) if exception is None else exception
3948

4049

4150
def is_number(
@@ -45,7 +54,9 @@ def is_number(
4554
try:
4655
float(value)
4756
except Exception:
48-
raise ArgumentException(argument_name) if exception is None else exception
57+
raise ArgumentError(
58+
TextErrorMessages.is_number_message(argument_name, value), argument_name
59+
) if exception is None else exception
4960

5061

5162
def is_integer(
@@ -55,31 +66,40 @@ def is_integer(
5566
try:
5667
int(value)
5768
except Exception:
58-
raise ArgumentException(argument_name) if exception is None else exception
69+
raise ArgumentError(
70+
TextErrorMessages.is_integer_message(argument_name, value), argument_name
71+
) if exception is None else exception
5972

6073

6174
def is_lowercase(
6275
value: str, argument_name: str = None, exception: Exception = None
6376
) -> None:
6477
NoneType.is_not_none(value, argument_name, exception)
6578
if not value.islower():
66-
raise ArgumentException(argument_name) if exception is None else exception
79+
raise ArgumentError(
80+
TextErrorMessages.is_lowercase_message(argument_name, value), argument_name
81+
) if exception is None else exception
6782

6883

6984
def is_uppercase(
7085
value: str, argument_name: str = None, exception: Exception = None
7186
) -> None:
7287
NoneType.is_not_none(value, argument_name, exception)
7388
if not value.isupper():
74-
raise ArgumentException(argument_name) if exception is None else exception
89+
raise ArgumentError(
90+
TextErrorMessages.is_uppercase_message(argument_name, value), argument_name
91+
) if exception is None else exception
7592

7693

7794
def has_length(
7895
value: str, length: int, argument_name: str = None, exception: Exception = None
7996
) -> None:
8097
NoneType.is_not_none(value, argument_name, exception)
8198
if len(value) != length:
82-
raise ArgumentException(argument_name) if exception is None else exception
99+
raise ArgumentError(
100+
TextErrorMessages.has_length_message(argument_name, len(value), length),
101+
argument_name,
102+
) if exception is None else exception
83103

84104

85105
def has_length_between(
@@ -92,20 +112,31 @@ def has_length_between(
92112
NoneType.is_not_none(value, argument_name, exception)
93113
value_length = len(value)
94114
if value_length < min_lenght or value_length > max_lenght:
95-
raise ArgumentException(argument_name) if exception is None else exception
115+
raise ArgumentError(
116+
TextErrorMessages.has_length_between_message(
117+
argument_name, len(value), min_lenght, max_lenght
118+
),
119+
argument_name,
120+
) if exception is None else exception
96121

97122

98123
def is_equal_to(
99124
value: str, expected: str, argument_name: str = None, exception: Exception = None
100125
) -> None:
101126
NoneType.is_not_none(value, argument_name, exception)
102127
if value != expected:
103-
raise ArgumentException(argument_name) if exception is None else exception
128+
raise ArgumentError(
129+
TextErrorMessages.is_equal_to_message(argument_name, value, expected),
130+
argument_name,
131+
) if exception is None else exception
104132

105133

106134
def is_not_equal_to(
107135
value: str, expected: str, argument_name: str = None, exception: Exception = None
108136
) -> None:
109137
NoneType.is_not_none(value, argument_name, exception)
110138
if value == expected:
111-
raise ArgumentException(argument_name) if exception is None else exception
139+
raise ArgumentError(
140+
TextErrorMessages.is_not_equal_to_message(argument_name, value),
141+
argument_name,
142+
) if exception is None else exception

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 0.0.2
2+
current_version = 0.1.0
33

44
[bumpversion:file:checkarg/__init__.py]
55

0 commit comments

Comments
 (0)