Skip to content

Commit

Permalink
Merge 5522b15 into bc5ef6e
Browse files Browse the repository at this point in the history
  • Loading branch information
orsinium committed Oct 7, 2019
2 parents bc5ef6e + 5522b15 commit b92c294
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions deal/_decorators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .post import Post
from .pre import Pre
from .raises import Raises
from .reason import Reason
from .silent import Silent


Expand All @@ -14,5 +15,6 @@
'Post',
'Pre',
'Raises',
'Reason',
'Silent',
]
35 changes: 35 additions & 0 deletions deal/_decorators/reason.py
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
4 changes: 4 additions & 0 deletions deal/_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class RaisesContractError(ContractError):
pass


class ReasonContractError(ContractError):
pass


class OfflineContractError(ContractError):
pass

Expand Down

0 comments on commit b92c294

Please sign in to comment.