from contextlib import contextmanager

from django.db import DatabaseError, IntegrityError, transaction


@contextmanager
def transaction_with_commit_on_errors():
    """Perform transaction and raise an error in any occurred."""
    error = None
    with transaction.atomic():
        try:
            yield
        except (DatabaseError, IntegrityError):
            raise
        except Exception as e:
            error = e
    if error:
        raise error

from contextlib import contextmanager

from django.db import DatabaseError, transaction


@contextmanager
def transaction_with_commit_on_errors():
    """Perform transaction and raise an error in any occurred."""
    error = None
    with transaction.atomic():
        try:
            yield
        except DatabaseError:
            raise
        except Exception as e:
            error = e
    if error:
        raise error

remove integrity error as it is a subclass of DatabaseError