Created
January 20, 2022 10:04
-
-
Save raphaeljolivet/e905b2e8ec0944b792a5c9b83b4fc789 to your computer and use it in GitHub Desktop.
ExceptionContext : adding context to exceptions with "with" statement
This file contains hidden or 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
from contextlib import AbstractContextManager | |
from future.utils import raise_from | |
class ExceptionContext(AbstractContextManager) : | |
def __init__(self, context): | |
self.context = context | |
def __exit__(self, exc_type, exc_val, exc_tb): | |
if exc_val != None : | |
raise_from(Exception("Context : %s" % str(self.context)), exc_val) | |
return True | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment