Skip to content

Commit

Permalink
Merge pull request #2564 from mmerickel/fix/invoke_exception_view_ret…
Browse files Browse the repository at this point in the history
…urn_value

ensure invoke_exception_view always returns a response
  • Loading branch information
mmerickel committed May 10, 2016
2 parents 7b0a89a + e045cfa commit 56cf0f4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
25 changes: 25 additions & 0 deletions pyramid/tests/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,31 @@ def exc_view(exc, request):
else: # pragma: no cover
self.fail()

def test_it_raises_if_not_found(self):
from pyramid.httpexceptions import HTTPNotFound
request = self._makeOne()
dummy_exc = RuntimeError()
try:
raise dummy_exc
except RuntimeError:
self.assertRaises(HTTPNotFound, request.invoke_exception_view)
else: # pragma: no cover
self.fail()

def test_it_raises_predicate_mismatch(self):
from pyramid.exceptions import PredicateMismatch
def exc_view(exc, request): pass
self.config.add_view(exc_view, context=Exception, request_method='POST')
request = self._makeOne()
request.method = 'GET'
dummy_exc = RuntimeError()
try:
raise dummy_exc
except RuntimeError:
self.assertRaises(PredicateMismatch, request.invoke_exception_view)
else: # pragma: no cover
self.fail()

class ExceptionResponse(Exception):
status = '404 Not Found'
app_iter = ['Not Found']
Expand Down
8 changes: 6 additions & 2 deletions pyramid/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from pyramid.httpexceptions import (
HTTPFound,
HTTPNotFound,
default_exceptionresponse_view,
)

Expand Down Expand Up @@ -589,8 +590,9 @@ def invoke_exception_view(
object that this method is attached to as the ``request``, and
``True`` for ``secure``.
This method returns a :term:`response` object or ``None`` if no
matching exception view can be found."""
This method returns a :term:`response` object or raises
:class:`pyramid.httpexceptions.HTTPNotFound` if a matching view cannot
be found."""

if request is None:
request = self
Expand Down Expand Up @@ -623,4 +625,6 @@ def invoke_exception_view(
secure=secure,
request_iface=request_iface.combined,
)
if response is None:
raise HTTPNotFound
return response

0 comments on commit 56cf0f4

Please sign in to comment.