Skip to content

Commit

Permalink
fix(ex_handling): Don't let finally block mask ex
Browse files Browse the repository at this point in the history
We have a project with test code which utilizes collective.solr which
utilizes collective.indexing which pushes index operations to the
pre commit phase.
In our case an exception was then thrown during commit within
the zopeApp context.
closing the connection in the finally statement raised another exception
because of the unclean state left by the first exception.
This exception in the finally block totally masked the original
exception, hindering bug fixing.
  • Loading branch information
do3cc committed Aug 12, 2015
1 parent 109eba5 commit 2c7d1b4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Expand Up @@ -4,7 +4,8 @@ Changelog
4.0.15 (unreleased)
-------------------

- Nothing changed yet.
- Prevent exception masking in finally clause of zopeApp context
[do3cc]


4.0.14 (2015-07-29)
Expand Down
33 changes: 26 additions & 7 deletions src/plone/testing/z2.py
Expand Up @@ -253,18 +253,37 @@ def zopeApp(db=None, connection=None, environ=None):
if connection is None:
connection = app._p_jar

# exceptions in finally clauses can mask exceptions
# in the preceeding code block. So we catch
# every exception and throw it instead of the exception
# in the finally clause
inner_exception = None
try:
yield app
except:
transaction.abort()
except Exception, e:
inner_exception = e
try:
transaction.abort()
except Exception, e:
inner_exception = e
raise
raise
else:
transaction.commit()
try:
transaction.commit()
except Exception, e:
inner_exception = e
finally:
app.REQUEST.close()

if closeConn:
connection.close()
try:
app.REQUEST.close()

if closeConn:
connection.close()
except:
if inner_exception:
raise inner_exception
else:
raise

# Startup layer - you probably don't want to use this one directly

Expand Down

0 comments on commit 2c7d1b4

Please sign in to comment.