Skip to content

Commit d5e9569

Browse files
Add commons Exception (#13)
* Add CommonsException, CommonsRuntimeException, ConcurrentException * Fix format issues * Fix lint
1 parent d01a804 commit d5e9569

File tree

4 files changed

+126
-7
lines changed

4 files changed

+126
-7
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from ..exception import CommonsException
2+
3+
4+
class ConcurrentException(CommonsException):
5+
"""
6+
Raised for Concurrent Operations
7+
"""
Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,68 @@
1-
__all__ = ["NoSuchElementError", "IllegalStateException"]
1+
__all__ = [
2+
"CommonsException",
3+
"CommonsRuntimeException",
4+
"NoSuchElementError",
5+
"IllegalStateException",
6+
]
27

8+
import traceback
9+
from types import TracebackType
10+
from typing import Optional, Any
311

4-
class NoSuchElementError(RuntimeError):
12+
13+
class CommonsException(Exception):
14+
def __init__( # pylint: disable=W1113
15+
self, message: Optional[str] = None, cause: Optional[Exception] = None, *args: Any
16+
) -> None:
17+
super().__init__(*args)
18+
if cause is not None:
19+
self.__cause__ = cause
20+
self._message = message
21+
22+
def get_cause(self) -> Optional[BaseException]:
23+
return self.__cause__
24+
25+
def get_traceback(self) -> Optional[TracebackType]:
26+
return self.__traceback__
27+
28+
def get_message(self) -> Optional[str]:
29+
return self._message
30+
31+
def print_traceback(self) -> None:
32+
traceback.print_tb(self.get_traceback())
33+
34+
35+
class CommonsRuntimeException(RuntimeError):
36+
def __init__( # pylint: disable=W1113
37+
self, message: Optional[str] = None, cause: Optional[Exception] = None, *args: Any
38+
) -> None:
39+
super().__init__(*args)
40+
if cause is not None:
41+
self.__cause__ = cause
42+
self._message = message
43+
44+
def get_cause(self) -> Optional[BaseException]:
45+
return self.__cause__
46+
47+
def get_traceback(self) -> Optional[TracebackType]:
48+
return self.__traceback__
49+
50+
def get_message(self) -> Optional[str]:
51+
return self._message
52+
53+
def print_traceback(self) -> None:
54+
traceback.print_tb(self.get_traceback())
55+
56+
57+
class NoSuchElementError(CommonsRuntimeException):
558
"""
659
Raised when an object is expected to be present but is not in real. Extends RuntimeError as
760
this error happens during runtime.
861
"""
962

1063

11-
class IllegalStateException(RuntimeError):
64+
class IllegalStateException(CommonsRuntimeException):
1265
"""
13-
Runtime Error raised when the program's state is not in a expected state or the code execution
66+
Runtime Error raised when the program's state is not in an expected state or the code execution
1467
should never have reached this state
1568
"""

tests/parametrized.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import dataclasses
22
import functools
3-
from typing import Any, Optional, List
3+
from typing import Any, Optional
44

55

66
@dataclasses.dataclass
7-
class TestData:
7+
class CommonsTestData:
88
data: Any
99
expected: Any
1010

@@ -17,7 +17,7 @@ def get_message(self):
1717
)
1818

1919

20-
def cases(testcases: List[TestData]):
20+
def cases(*testcases: CommonsTestData):
2121
def decorator(f):
2222
@functools.wraps(f)
2323
def wrapped(self):
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from unittest import TestCase
2+
3+
from pycommons.base.exception import CommonsException, CommonsRuntimeException
4+
from tests.parametrized import CommonsTestData, cases
5+
6+
7+
class TestCommonsException(TestCase):
8+
@cases(
9+
CommonsTestData(data=CommonsException, expected=None),
10+
CommonsTestData(data=CommonsRuntimeException, expected=None),
11+
)
12+
def test_initialize_without_params(self, test_data: CommonsTestData):
13+
try:
14+
raise test_data.data()
15+
except test_data.data as exc:
16+
self.assertEqual(test_data.expected, exc.get_message())
17+
self.assertIsNone(exc.get_cause())
18+
self.assertIsNotNone(exc.get_traceback())
19+
exc.print_traceback()
20+
21+
@cases(
22+
CommonsTestData(data=CommonsException, expected="Some error occurred"),
23+
CommonsTestData(data=CommonsRuntimeException, expected="Some error occurred"),
24+
)
25+
def test_initialize_with_message(self, test_data: CommonsTestData):
26+
try:
27+
raise test_data.data(message="Some error occurred")
28+
except test_data.data as exc:
29+
self.assertEqual(test_data.expected, exc.get_message())
30+
self.assertIsNone(exc.get_cause())
31+
self.assertIsNotNone(exc.get_traceback())
32+
33+
@cases(
34+
CommonsTestData(data=CommonsException, expected="Some error occurred"),
35+
CommonsTestData(data=CommonsRuntimeException, expected="Some error occurred"),
36+
)
37+
def test_initialize_with_cause(self, test_data: CommonsTestData):
38+
_cause = Exception("cause exception")
39+
try:
40+
raise test_data.data(message="Some error occurred", cause=_cause)
41+
except test_data.data as exc:
42+
self.assertEqual(test_data.expected, exc.get_message())
43+
self.assertEqual(_cause, exc.get_cause())
44+
self.assertEqual(_cause, exc.__cause__)
45+
self.assertIsNotNone(exc.get_traceback())
46+
47+
@cases(
48+
CommonsTestData(data=CommonsException, expected="Some error occurred"),
49+
CommonsTestData(data=CommonsRuntimeException, expected="Some error occurred"),
50+
)
51+
def test_initialize_with_cause2(self, test_data: CommonsTestData):
52+
_cause = Exception("cause exception")
53+
try:
54+
raise test_data.data(message="Some error occurred") from _cause
55+
except test_data.data as exc:
56+
self.assertEqual(test_data.expected, exc.get_message())
57+
self.assertEqual(_cause, exc.get_cause())
58+
self.assertEqual(_cause, exc.__cause__)
59+
self.assertIsNotNone(exc.get_traceback())

0 commit comments

Comments
 (0)