-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
41 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
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,35 @@ | ||
from typing import Callable | ||
|
||
from .base import Base | ||
from .._types import ExceptionType | ||
from .._exceptions import ReasonContractError | ||
|
||
|
||
class Reason(Base): | ||
exception: ExceptionType = ReasonContractError | ||
|
||
def __init__(self, trigger: Exception, validator: Callable, *, | ||
message: str = None, exception: ExceptionType = None, debug: bool = False): | ||
""" | ||
Step 1. Set allowed exceptions list. | ||
""" | ||
self.trigger = trigger | ||
super().__init__( | ||
validator=validator, | ||
message=message, | ||
exception=exception, | ||
debug=debug, | ||
) | ||
|
||
def patched_function(self, *args, **kwargs): | ||
""" | ||
Step 3. Wrapped function calling. | ||
""" | ||
try: | ||
return self.function(*args, **kwargs) | ||
except self.trigger as origin: | ||
try: | ||
self.validate(*args, **kwargs) | ||
except self.exception: | ||
raise self.exception from origin | ||
raise |
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