"""Helper functions to work with database.""" import functools from oto import response from raven import base as sentry_base from sqlalchemy import exc def wrap_errors(function): """Decorate the given function with logic to handle SQLAlchemy errors. If a SQLAlchemy exception is thrown, it will be caught and logged and the function will return a fatal response. Args: function (func): the function to decorate Returns: func: function decorated with error-handling logic """ @functools.wraps(function) def call_function_with_error_handling(*args, **kwargs): try: function_return = function(*args, **kwargs) except exc.SQLAlchemyError: if sentry_base.Raven: sentry_base.Raven.captureException() return response.create_fatal_response() return function_return return call_function_with_error_handling