Skip to content

Commit

Permalink
Merge branch 'handle-zope3-exceptions' into plonezope4
Browse files Browse the repository at this point in the history
  • Loading branch information
davisagli committed Feb 2, 2017
2 parents 357fe1f + eaad5ac commit 7463db9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
10 changes: 7 additions & 3 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ Bugs Fixed
- Use unicode transaction-notes to support ZODB 5.
[pbauer]

- Move _html and _error_html to HTTPBaseResponse to support views that return
a HTML `(title, body)` tuple in WSGIResponses.
[MatthewWilkes]
- Move _html and _error_html to HTTPBaseResponse to support views that return
a HTML `(title, body)` tuple in WSGIResponses.
[MatthewWilkes]

- Make the WSGIPublisher convert some exceptions from zope.publisher
and zope.security (Unauthorized, NotFound, BadRequest, Redirect)
into the corresponding exceptions from zExceptions. [davisagli]

Features Added
++++++++++++++
Expand Down
21 changes: 20 additions & 1 deletion src/ZPublisher/WSGIPublisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,22 @@
from zExceptions import (
HTTPOk,
HTTPRedirection,
BadRequest,
NotFound,
Redirect,
Unauthorized,
)
from ZODB.POSException import ConflictError
from zope.component import queryMultiAdapter
from zope.event import notify
from zope.security.management import newInteraction, endInteraction
from zope.security.interfaces import Unauthorized as zope_security_Unauthorized
from zope.publisher.skinnable import setDefaultSkin
from zope.publisher.interfaces import (
NotFound as zope_publisher_NotFound,
BadRequest as zope_publisher_BadRequest,
Redirect as zope_publisher_Redirect,
)

from ZPublisher.HTTPRequest import WSGIRequest
from ZPublisher.HTTPResponse import WSGIResponse
Expand Down Expand Up @@ -169,6 +178,16 @@ def _publish_response(request, response, module_info, _publish=publish):
with transaction_pubevents(request):
response = _publish(request, module_info)
except Exception as exc:
# Convert zope 3 exceptions to zExceptions exceptions
if isinstance(exc, zope_security_Unauthorized):
exc = Unauthorized(exc.message)
elif isinstance(exc, zope_publisher_Redirect):
exc = Redirect(exc.location)
elif isinstance(exc, zope_publisher_BadRequest):
exc = BadRequest(exc.message)
elif isinstance(exc, zope_publisher_NotFound):
exc = NotFound(exc.message)

if request.environ.get('wsgi.handleErrors', True):
response.setStatus(exc.__class__)
if isinstance(exc, HTTPRedirection):
Expand All @@ -187,7 +206,7 @@ def _publish_response(request, response, module_info, _publish=publish):
if isinstance(exc, (HTTPRedirection, Unauthorized)):
return response

raise
raise exc

return response

Expand Down

0 comments on commit 7463db9

Please sign in to comment.