-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow setting response decorator also for class
- Loading branch information
Tuerke Erik
committed
Mar 24, 2020
1 parent
3d274c5
commit af9498f
Showing
6 changed files
with
56 additions
and
12 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
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,33 @@ | ||
import logging | ||
from typing import Type, Dict, Tuple, Union | ||
|
||
from restit.exception import HttpError | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
def exception_mapping(mapping: Dict[Type[Exception], Union[Tuple[Type[HttpError], str], Type[HttpError]]]): | ||
def decorator(func): | ||
def wrapper(*args, **kwargs): | ||
try: | ||
return func(*args, **kwargs) | ||
except Exception as exception: | ||
for source_exception_class, target_exception_tuple_or_class in mapping.items(): | ||
if isinstance(exception, source_exception_class): | ||
if isinstance(target_exception_tuple_or_class, tuple): | ||
LOGGER.debug( | ||
"Mapping exception class %s to %s with description: %s", | ||
type(exception), target_exception_tuple_or_class[0], target_exception_tuple_or_class[1] | ||
) | ||
raise target_exception_tuple_or_class[0](target_exception_tuple_or_class[1]) | ||
else: | ||
LOGGER.debug( | ||
"Mapping exception class %s to %s", type(exception), target_exception_tuple_or_class | ||
) | ||
raise target_exception_tuple_or_class(str(exception)) | ||
|
||
raise exception | ||
|
||
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
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