Skip to content

Commit

Permalink
content.get_view may not swallow exceptions. Refs #230
Browse files Browse the repository at this point in the history
  • Loading branch information
jaroel committed Jun 9, 2015
1 parent 8511bdd commit 5ecc283
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
3 changes: 3 additions & 0 deletions docs/CHANGES.rst
Expand Up @@ -4,6 +4,9 @@ Changelog
1.3.3 (unreleased)
------------------

- plone.api.content.get_view no longer swallows exceptions.
[jaroel]

- Add plone.api.content.find. Refs #210
[jaroel]

Expand Down
29 changes: 16 additions & 13 deletions src/plone/api/content.py
Expand Up @@ -440,24 +440,27 @@ def get_view(name=None, context=None, request=None):
:class:`~plone.api.exc.InvalidParameterError`
:Example: :ref:`content_get_view_example`
"""
# We do not use exceptionhandling to detect if the requested view is
# available, because the __init__ of said view will contain
# errors in client code.

# Get all available views...
sm = getSiteManager()
available_views = sm.adapters.lookupAll(
required=(providedBy(context), providedBy(request)),
provided=Interface,
)
# and get their names.
available_view_names = [view[0] for view in available_views]

try:
return getMultiAdapter((context, request), name=name)
except:
# get a list of all views so we can display their names in the error
# msg
sm = getSiteManager()
views = sm.adapters.lookupAll(
required=(providedBy(context), providedBy(request)),
provided=Interface,
)
views_names = [view[0] for view in views]

# Raise an error if the requested view is not available.
if name not in available_view_names:
raise InvalidParameterError(
"Cannot find a view with name '{0}'.\n"
"Available views are:\n"
"{1}".format(name, '\n'.join(sorted(views_names)))
"{1}".format(name, '\n'.join(sorted(available_view_names)))
)
return getMultiAdapter((context, request), name=name)


@required_parameters('obj')
Expand Down

0 comments on commit 5ecc283

Please sign in to comment.