Skip to content

Commit

Permalink
More friendly error message from getControl() when index is out of bo…
Browse files Browse the repository at this point in the history
…unds.
  • Loading branch information
mgedmin committed Feb 7, 2012
1 parent 52dde2c commit 9acf8bb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGES.txt
Expand Up @@ -12,6 +12,10 @@ CHANGES
environment by setting it to ``None`` when ``Browser.handleErrors`` is
``True``. This makes it easier to test error pages.

- More friendly error message from getControl() et al when you specify
an index that is out of bounds.


4.0.2 (2011-05-25)
------------------

Expand Down
7 changes: 7 additions & 0 deletions src/zope/testbrowser/README.txt
Expand Up @@ -516,6 +516,13 @@ below.
<ListControl name='ambiguous-subcontrol' type='select'>
>>> browser.getControl('Sub-control Ambiguity', index=1).optionValue
'ambiguous'
>>> browser.getControl('Sub-control Ambiguity', index=2)
Traceback (most recent call last):
...
LookupError: label 'Sub-control Ambiguity'
Index 2 out of range, available choices are 0...1
0: <SelectControl(ambiguous-subcontrol=[*, ambiguous])>
1: <Item name='ambiguous' id=None contents='Sub-control Ambiguity Exemplified' value='ambiguous' label='Sub-control Ambiguity Exemplified'>

Label searches are against stripped, whitespace-normalized, no-tag versions of
the text. Text applied to searches is also stripped and whitespace normalized.
Expand Down
8 changes: 6 additions & 2 deletions src/zope/testbrowser/browser.py
Expand Up @@ -47,8 +47,12 @@ def disambiguate(intermediate, msg, index, choice_repr=None):
else:
try:
return intermediate[index]
except KeyError:
msg = '%s index %d' % (msg, index)
except IndexError:
msg = '%s\nIndex %d out of range, available choices are 0...%d' % (
msg, index, len(intermediate) - 1)
if choice_repr:
msg += ''.join(['\n %d: %s' % (n, choice_repr(choice))
for n, choice in enumerate(intermediate)])
raise LookupError(msg)

def control_form_tuple_repr((ctrl, form)):
Expand Down

0 comments on commit 9acf8bb

Please sign in to comment.