"""Error handler functions for specific types of failures.""" import functools import sentry_sdk from flask import g from oto.response import create_fatal_response from sqlalchemy import exc def log_db_exception(exception): """Log a DB exception to Sentry and Loggly.""" sentry_sdk.capture_exception(exception) g.ows.log.error('DB error: {}'.format(exception)) def wrap_db_errors(func): """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: func (func): the function to decorate Returns: func: function decorated with error-handling logic """ @functools.wraps(func) def call_function_with_error_handling(*args, **kwargs): try: function_return = func(*args, **kwargs) except exc.SQLAlchemyError as exception: log_db_exception(exception) return create_fatal_response() return function_return return call_function_with_error_handling