Skip to content

Commit

Permalink
added comments and now we log tracebacks
Browse files Browse the repository at this point in the history
  • Loading branch information
bdzim committed May 21, 2014
1 parent a1a4fd4 commit bd2ea9e
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions netkes/account_mgr/accounts_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,32 @@ class Error(Exception):
pass


class ApiMeta(type):
class LogErrorMeta(type):
'''Decorate all functions so that they log all Errors.'''
def __new__(cls, name, bases, attrs):
for attr_name, attr_value in attrs.iteritems():
if isinstance(attr_value, types.FunctionType):
attrs[attr_name] = cls.log_exceptions(attr_value)

return super(ApiMeta, cls).__new__(cls, name, bases, attrs)
return super(LogErrorMeta, cls).__new__(cls, name, bases, attrs)

@classmethod
def log_exceptions(cls, func):
'''Log the name and arguments of any function that raises an Error.
Then raise the exception.
'''
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Error:
log = logging.getLogger('accounts_api')
log.error('%s - %s - %s' % (func.__name__, args, kwargs))
log.exception('%s - %s - %s' % (func.__name__, args, kwargs))
raise
return wrapper


class Api(object):
__metaclass__ = ApiMeta
__metaclass__ = LogErrorMeta

class BadParams(Error):
pass
Expand Down

0 comments on commit bd2ea9e

Please sign in to comment.