Ensure raising of IntegrityError on unique constraint violations.

    In earlier versions of hdbcli it doesn't raise the hdbcli.dbapi.IntegrityError
    exception for unique constraint violations. To support also older versions
    of hdbcli this decorator inspects the raised exception and will rewrite the
    exception based on HANA's error code.

def _fix_integrity_error(f):
    """Ensure raising of IntegrityError on unique constraint violations.

    In earlier versions of hdbcli it doesn't raise the hdbcli.dbapi.IntegrityError
    exception for unique constraint violations. To support also older versions
    of hdbcli this decorator inspects the raised exception and will rewrite the
    exception based on HANA's error code.
    """

    @wraps(f)
    def wrapper(dialect, *args, **kwargs):
        try:
            return f(dialect, *args, **kwargs)
        except dialect.dbapi.Error as exc:
            if exc.errorcode == 301 and not isinstance(exc, dialect.dbapi.IntegrityError):
                raise dialect.dbapi.IntegrityError(exc)
            raise
    return wrapper