Skip to content

Instantly share code, notes, and snippets.

@raphaeljolivet
Created January 20, 2022 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raphaeljolivet/e905b2e8ec0944b792a5c9b83b4fc789 to your computer and use it in GitHub Desktop.
Save raphaeljolivet/e905b2e8ec0944b792a5c9b83b4fc789 to your computer and use it in GitHub Desktop.
ExceptionContext : adding context to exceptions with "with" statement
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