Skip to content

Commit

Permalink
Add error handling for missing regulations
Browse files Browse the repository at this point in the history
- Addresses issue eregs#485: Missing data leads to an explosion
- Also addresses FEC issue: fecgov/fec-eregs#385

- Throw a 404 error when navigating to a regulation that doesn't exist
- Previously, the `utils.regulation_meta` function was returning `{}`, so accessing the 'meta' data was resulting in a 500 error
  • Loading branch information
lbeaufort committed May 14, 2018
1 parent b7fb257 commit dc509f9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
14 changes: 14 additions & 0 deletions regulations/tests/views_chrome_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ class FakeView(chrome.ChromeView):
assert response.status_code == 404


def test_chrome_empty_meta(monkeypatch, rf):
"""Return 404 when trying to access missing regulation.
In this situation, `regulation_meta` returns {} """

chrome_view = chrome.ChromeView()

monkeypatch.setattr(chrome, 'fetch_grouped_history', Mock())
monkeypatch.setattr(chrome, 'fetch_toc', Mock(return_value=[]))
monkeypatch.setattr(chrome.utils, 'regulation_meta', Mock(return_value={}))

with pytest.raises(error_handling.MissingContentException):
chrome_view.set_chrome_context({}, '2', 'version')


def test_chrome_error_propagation(monkeypatch, rf):
"""While we don't rely on this sort of propagation for the main content
(much), test it in the sidebar"""
Expand Down
5 changes: 5 additions & 0 deletions regulations/views/chrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ def set_chrome_context(self, context, reg_part, version):
context['TOC'] = toc

context['meta'] = utils.regulation_meta(reg_part, version)

# Throw 404 if regulation doesn't exist
if not context['meta']:
raise error_handling.MissingContentException()

context['version_span'] = version_span(
context['history'], context['meta']['effective_date'])
context['version_switch_view'] = self.version_switch_view
Expand Down

0 comments on commit dc509f9

Please sign in to comment.