Repository: django/django
File: django/db/models/sql/compiler.py (Python)

Code before:
        - SINGLE: Retrieves a single row using fetchone().
        - ROW_COUNT: Retrieves the number of rows in the result.
        - CURSOR: Runs the query, and returns the cursor object. It is the
           caller's responsibility to close the cursor.
        """
        result_type = result_type or NO_RESULTS
        try:
            sql, params = self.as_sql()
            if not sql:
                raise EmptyResultSet
        except EmptyResultSet:
            if result_type == MULTI:
                return iter([])
            else:
                return
        if chunked_fetch:
            cursor = self.connection.chunked_cursor()
        else:
            cursor = self.connection.cursor()
        try:
            cursor.execute(sql, params)
        except Exception as e:
            # Might fail for server-side cursors (e.g. connection closed)
            try:
                cursor.close()
            except Exception:
                # If closing fails, surface only the
                # original execute() exception.
                raise e from None
            # If close() succeeds, re-raise normally.
            raise

        if result_type == ROW_COUNT:
            try:
                return cursor.rowcount
            finally:
                cursor.close()
        if result_type == CURSOR:
            # Give the caller the cursor to process and close.
            return cursor
        if result_type == SINGLE:
            try:
                val = cursor.fetchone()
                if val:
                    return val[0 : self.col_count]
                return val
            finally:
                # done with the cursor
                cursor.close()
        if result_type == NO_RESULTS:
            cursor.close()

Review comment (suggestion):
```suggestion
            except DatabaseError:
```

Code after:
    ):
        """
        Run the query against the database and return the result(s). The
        return value depends on the value of result_type.

        When result_type is:
        - MULTI: Retrieves all rows using fetchmany(). Wraps in an iterator for
           chunked reads when supported.
        - SINGLE: Retrieves a single row using fetchone().
        - ROW_COUNT: Retrieves the number of rows in the result.
        - CURSOR: Runs the query, and returns the cursor object. It is the
           caller's responsibility to close the cursor.
        """
        result_type = result_type or NO_RESULTS
        try:
            sql, params = self.as_sql()
            if not sql:
                raise EmptyResultSet
        except EmptyResultSet:
            if result_type == MULTI:
                return iter([])
            else:
                return
        if chunked_fetch:
            cursor = self.connection.chunked_cursor()
        else:
            cursor = self.connection.cursor()
        try:
            cursor.execute(sql, params)
        except Exception as e:
            # Might fail for server-side cursors (e.g. connection closed)
            try:
                cursor.close()
            except DatabaseError:
                raise e from None
            raise

        if result_type == ROW_COUNT:
            try:
                return cursor.rowcount
            finally:
                cursor.close()
        if result_type == CURSOR:
            # Give the caller the cursor to process and close.
            return cursor
        if result_type == SINGLE:
            try:
                val = cursor.fetchone()
                if val:
                    return val[0 : self.col_count]
                return val