Skip to content

Instantly share code, notes, and snippets.

@ragne
Created February 28, 2017 20:17
Show Gist options
  • Save ragne/76bcc44173ca0b8c4fb132a498ddf90b to your computer and use it in GitHub Desktop.
Save ragne/76bcc44173ca0b8c4fb132a498ddf90b to your computer and use it in GitHub Desktop.
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:
res = f(*args, **kwargs)
if isinstance(res, records.RecordCollection):
try:
res = res.all()
except ResourceClosedError as e:
res = True
return res
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.pop() 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