diff --git a/accounting/decorators.py b/accounting/decorators.py new file mode 100644 index 0000000000000000000000000000000000000000..6e5c1de6e8bc1d9971931872d122afb8a01b963d --- /dev/null +++ b/accounting/decorators.py @@ -0,0 +1,20 @@ +from functools import wraps + +from flask import jsonify + +from accounting.exceptions import AccountingException + + +def jsonify_exceptions(func): + ''' + Wraps a Flask endpoint and catches any AccountingException-based + exceptions which are returned to the client as JSON. + ''' + @wraps(func) + def wrapper(*args, **kw): + try: + return func(*args, **kw) + except AccountingException as exc: + return jsonify(error=exc) + + return wrapper