-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
./manage.py kafka_connect
exists now with CommandError in cas…
…e of any Exception. feat: introduce `substitute_error` decorator. refs #34
- Loading branch information
Showing
3 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from collections.abc import Iterable | ||
from functools import wraps | ||
from typing import Callable, Type | ||
|
||
|
||
def substitute_error(errors: Iterable[Type[Exception]], substitution: Type[Exception]) -> Callable: | ||
def decorator(func): | ||
@wraps(func) | ||
def wrapper(*args, **kwargs): | ||
try: | ||
return func(*args, **kwargs) | ||
except tuple(errors) as original_error: | ||
raise substitution from original_error | ||
return wrapper | ||
return decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from unittest.mock import Mock | ||
|
||
from django.core.management import CommandError | ||
from django.test import SimpleTestCase | ||
|
||
from django_kafka.management.commands.errors import substitute_error | ||
|
||
|
||
class SubstituteErrorTestCase(SimpleTestCase): | ||
def test_substitute(self): | ||
class CustomException(Exception): | ||
pass | ||
|
||
errors = [ValueError, KeyError, CustomException] | ||
decorator = substitute_error(errors, CommandError) | ||
|
||
for error in errors: | ||
func = Mock(side_effect=error) | ||
|
||
with self.assertRaises(CommandError): | ||
decorator(func)() |