Last active
February 6, 2017 14:22
-
-
Save ragne/ca69c611cb38a8f940124e6bd3698d80 to your computer and use it in GitHub Desktop.
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
def handle_err(self): | |
"""It's a hack. Be careful! | |
By design it needs self for error storage. | |
Think of it like windows get_last_error""" | |
def wrapper(f): | |
from functools import update_wrapper | |
def new_func(*args, **kwargs): | |
try: | |
return f(*args, **kwargs) | |
except Exception as e: | |
try: | |
self._err.append(e) | |
except Exception: | |
print("cannot find self, print error instead!\n%r" % e) | |
return False | |
return update_wrapper(new_func, f) | |
return wrapper | |
import types | |
class StrictDB(DB): | |
def __init__(self, *args, **kwargs): | |
self._err = [] | |
super(StrictDB, self).__init__(*args, **kwargs) | |
for f_name in [func for func in dir(self) if callable(getattr(self, func)) and not func.startswith("_")]: | |
f_code = getattr(self, f_name) | |
setattr(self, f_name, handle_err(self)(f_code)) | |
def get_last_err(self): | |
return self._err[-1] if len(self._err) > 0 else None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment