Skip to content

Commit

Permalink
Show suggested registry keys instead of dumping all keys.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaroel committed Jun 23, 2015
1 parent e9ca5af commit c01c5ba
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
3 changes: 3 additions & 0 deletions docs/CHANGES.rst
Expand Up @@ -4,6 +4,9 @@ Changelog
1.3.3 (unreleased)
------------------

- plone.api.portal.get_registry_record now suggests look-alike records when no records is found. Refs #249.
[jaroel]

- Fixed tests for Plone 5. Refs #241.
[jaroel]

Expand Down
21 changes: 13 additions & 8 deletions src/plone/api/portal.py
Expand Up @@ -255,14 +255,19 @@ def get_registry_record(name=None):

registry = getUtility(IRegistry)
if name not in registry:

records = [_name for _name, record in registry.records.items()]

raise InvalidParameterError(
"Cannot find a record with name '{0}'.\n"
"Available records are:\n"
"{1}".format(name, '\n'.join(records))
)
# Show all records that 'look like' name.
# We don't dump the whole list, because it 1500+ items.
records = [key for key in registry.records.keys() if name in key]
if records:
raise InvalidParameterError(
"Cannot find a record with name '{0}'.\n"
"Did you mean?:\n"
"{1}".format(name, '\n'.join(records))
)
else:
raise InvalidParameterError(
"Cannot find a record with name '{0}'".format(name)
)

return registry[name]

Expand Down
40 changes: 24 additions & 16 deletions src/plone/api/tests/test_portal.py
Expand Up @@ -492,29 +492,37 @@ def test_get_invalid_registry_record(self):
def test_get_invalid_registry_record_msg(self):
"""Test that the error message from trying to get a
nonexistant registry record produces an error message which
lists known registry records.
lists suggested registry records.
"""
from plone.api.exc import InvalidParameterError

with self.assertRaises(InvalidParameterError) as cm:
portal.get_registry_record(name='nonexistent.sharepoint.power')
exc_str = str(cm.exception)

self.assertTrue(
str(cm.exception).startswith(
"Cannot find a record with name "
"'nonexistent.sharepoint.power'.\n"
)
)
# Check if there is an error message.
self.assertTrue(exc_str.startswith("Cannot find a record with name"))

# A selection of records which should exist in all plone versions
should_be_theres = (
"plone.app.discussion.interfaces.IDiscussionSettings.captcha",
"plone.app.querystring.field.Creator.title",
"plone.app.querystring.operation.int.largerThan.description",
"plone.app.querystring.operation.selection.is.widget",
)
def test_get_invalid_registry_record_suggestions(self):
from plone.api.exc import InvalidParameterError

for should_be_there in should_be_theres:
self.assertIn((should_be_there + '\n'), str(cm.exception))
# Check without suggestion
with self.assertRaises(InvalidParameterError) as cm:
portal.get_registry_record(name='a random unique string')
exc_str = str(cm.exception)

# Check for an error, but no suggestions.
self.assertTrue(exc_str.startswith("Cannot find a record with name"))
self.assertFalse('Did you mean?:' in exc_str)

# Check with suggestions
with self.assertRaises(InvalidParameterError) as cm:
portal.get_registry_record(name='querystring')
exc_str = str(cm.exception)

# Check for an error with suggestions.
self.assertTrue(exc_str.startswith("Cannot find a record with name"))
self.assertTrue('Did you mean?:' in exc_str)

def test_set_valid_registry_record(self):
"""Test that setting a valid registry record succeeds."""
Expand Down

0 comments on commit c01c5ba

Please sign in to comment.